
批改狀態(tài):合格
老師批語:唯一亮點(diǎn)可能就是登錄窗口比較好看
<?php
define('DB_HOST','localhost');//主機(jī)名
define('DB_USER','root');//鏈接數(shù)據(jù)庫用戶名
define('DB_PWD','142536');//鏈接數(shù)據(jù)庫密碼
define('DB_NAME','mysqli');//數(shù)據(jù)庫名稱
define('DB_PORT','3306');//數(shù)據(jù)庫端口
define('DB_TYPE','mysql');//數(shù)據(jù)庫型號
define('DB_CHARSET','utf8');//數(shù)據(jù)庫編碼方式
define('DB_DSN',DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET);
//定義PDO的DSN,數(shù)據(jù)源名,包括主機(jī)名,端口號和數(shù)據(jù)庫名稱
$dsn='mysql:host=localhost;dbname=mysqli';
//PDO驅(qū)動程序的名稱,后面為一個(gè)冒號,再后面是可選的驅(qū)動程序鏈接數(shù)據(jù)庫變量信息,如主機(jī)名,端口和數(shù)據(jù)名
try
{
$pdo =new PDO(DB_DSN,DB_USER,DB_PWD);
}
catch(PDOException $e)
{
echo $e->getMessage();
//捕捉特定于數(shù)據(jù)庫信息的PDOException 異常
}
catch(Throwable $e)
{
echo $e->getMessage();
//捕捉擁有Throwable 接口的錯(cuò)誤或者其他異常
}
代碼示例
<?
require"config.php";
if($_GET["action"] == "logOut")
{
if(isset($_COOKIE['auto']))
{
exit("
<script>
alert('期待您的下次光臨:".$_COOKIE['username']."');
window.close();
</script>
");
}
else
{
setcookie("username"," ",time()-3600);
setcookie("auth"," ",time()-3600);
}
}
else
{
$auth = $_COOKIE['auth'];
$res=explode("%",$auth);
$id = end($res);
$sql ="SELECT `username`,`password`,`id` FROM `user` WHERE `id`=?";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(1,$id);
$stmt->execute();
$res = $stmt->fetch(PDO::FETCH_ASSOC);
$rs = $stmt->rowCount();
if($rs==1)
{
exit("
<script>
alert('歡迎您的歸來:".$_COOKIE['username']."');
location.href = 'index.php';
</script>
");
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>用戶登錄</title>
<link href="../cookie/lx/css/style.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="../cookie/lx/js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(c) {
$('.alert-close').on('click', function(c){
$('.message').fadeOut('slow', function(c){
$('.message').remove();
});
});
});
</script>
</head>
<body>
<div class="message warning">
<div class="inset">
<div class="login-head">
<h1>用戶登錄</h1>
<div class="alert-close"></div>
</div>
<form action="check.php" method="POST" name="form">
<ul>
<li><input type="text" name="username" class="text" value="用戶名" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = '用戶名';}"><a href="#" class=" icon user"></a></li>
<li><input type="password" name="password" value="******" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = '******';}"> <a href="#" class="icon lock"></a></li>
</ul>
<div class="submit">
<input type="submit" onClick="myFunction()" value="登錄" >
<h4><input type="checkbox" name="auto">記住密碼</h4>
<div class="clear"> </div>
</div>
</form>
</div>
</div>
</body>
</html>
示例圖
代碼示例
<?php
require "config.php";
$username = $_POST['username'];
$password = md5($_POST['password']);
$auto =$_POST['auto'];
$sql ="SELECT `username`,`password`,`id` FROM `user` WHERE `username`=? AND `password` = ?";
$stmt= $pdo->prepare($sql);
$stmt->bindParam(1,$username);
$stmt->bindParam(2,$password);
$stmt->execute();
$res=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount()==1){
setcookie("username","",time()-3600);
setcookie("auth","",time()-3600);
if(!empty($auto))
{
setcookie("username",$username,strtotime("+7 days"));
$salt="ynllww";
$auth= md5($username.$password.$salt)."%".$res['id'];
setcookie("auth",$auth,strtotime("+7 days"));
setcookie("auto",$auto,strtotime("+7 days"));
header("Location:index.php");
}
else
{
setcookie("username",$username);
$salt="ynllww";
$auth= md5($username.$password.$salt)."%".$res['id'];
setcookie("auth",$auth);
header("Location:index.php");
}
}
else
{
exit ("<script>alert('用戶名密碼錯(cuò)誤');location.href='login.php';</script>");
}
?>
示例圖
示例
<?php
require "config.php";
$auth = $_COOKIE['auth'];
$res=explode("%",$auth);
$id = end($res);
$auth_real=$res[0];
//end 取數(shù)組最后一個(gè)成員
$sql = "SELECT `username`,`password`,`id` FROM `user` WHERE `id`=?";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(1,$id);
$stmt->execute();
$res = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount()==1)
{
$username = $res['username'];
$password = $res['password'];
$salt="ynllww";
$auth = md5($username.$password.$salt);
if($auth != $auth_real)
{
exit("
<script>
alert('登錄錯(cuò)誤,請先登錄!');
location.href = 'login.php';
</script>
");
}
}
else
{
exit("
<script>
alert('登錄錯(cuò)誤,請先登錄!');
location.href = 'login.php';
</script>
");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登錄成功</title>
</head>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
h2
{
text-align: center;
margin-top:20px ;
margin-bottom: 20px;
}
a {
text-decoration: none;
color: #CCC;
}
nav
{
height: 50px;
background-color: #333;
padding: 0 30px;
display: flex;
flex-flow: row nowrap;
}
nav a{
height: inherit;
line-height: 50px;
padding: 0 15px;
justify-content: center;
}
nav a:last-child
{
margin-left: auto;
}
nav a:hover{
background-color: seagreen;
color: white;
}
</style>
<body>
<h2>歡迎<?php echo $_COOKIE['username']; ?>光臨</h2>
<nav>
<a href="">首頁</a>
<a href="">待付款的寶貝</a>
<a href="">待收貨的寶貝</a>
<a href="">已購買的寶貝</a>
<a href="login.php?action=logOut">安全退出</a>
</nav>
</body>
</html>
示例圖
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號