
批改狀態(tài):合格
老師批語(yǔ):界面實(shí)在找不到合適的詞來(lái)表?yè)P(yáng), 非常有個(gè)性.... 功能寫得不錯(cuò)
1、首頁(yè)代碼
//判斷登陸狀態(tài),輸出顯示登陸狀態(tài)變量(沒(méi)有學(xué)JS利用PHP的if語(yǔ)句來(lái)判斷輸出登陸和登出狀態(tài))
if (filter_has_var(INPUT_COOKIE,'user')) {
$user=unserialize(filter_input(INPUT_COOKIE,'user'));
$users=$user['username'];
$status='退出';
$status_url='0114verify.php?action=logout';
}else {
$users='用戶注冊(cè)';
$user_url='0114register.php';
$status='登陸';
$status_url='0114login.php';
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>種業(yè)圈</title>
<style>
* {
margin:0;
padding: 0;
font-size: 13px;
color:#666666;
}
a {
text-decoration: none;
}
.header {
width: 100%;
height: 100px;
background-color: #333333;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
padding: 0 20px;
box-sizing: border-box;
}
.header > .right {
width: 120px;
align-self: flex-end;
margin-bottom: 20px;
margin-right: 40px;
display: flex;
justify-content: space-between;
}
.header > .right * {
font-size: 18px;
color:#fffdef;
}
.header > .right a:hover {
color:#ff0000;
}
</style>
</head>
<body>
<div class="header">
<a href="0114index.php">
<img src="../0110/images/logo.png" alt="">
</a>
<div class="right">
//根據(jù)判斷輸出的變量信息顯示登陸狀態(tài)
<span><a href="<?php echo $user_url; ?>"><?php echo $users; ?></a></span>
<a id='logout' href="<?php echo $status_url; ?>"><?php echo $status; ?></a>
</div>
</div>
</body>
</html>
2、登陸頁(yè)面代碼:
if (filter_has_var(INPUT_COOKIE,'user')) {
exit('<script>alert("請(qǐng)勿重復(fù)登陸");location.assign("0114index.php");</script>');
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陸</title>
<style>
a {
text-decoration: none;
}
.login{
width: 400px;
height: 250px;
background-color: #55a532;
margin:0 auto;
padding: 10px;
font-size: 16px;
color: #fffdef;
border-radius: 20px;
}
.login:hover {
box-shadow: 0 0 3px #333333 ;
}
.login > h3 {
width: 80px;
margin: 20px auto;
color: #222222;
}
.login > :last-child {
width: 300px;
margin: 10px auto;
text-align: center;
}
.login > form {
display: grid;
grid-template-columns: 70px 1fr;
grid-template-rows:repeat(3,40px) ;
place-items: center;
}
.login > form > input {
width: 200px;
height: 25px;
}
.login > form > input:nth-last-of-type(n+3):hover {
box-shadow: 0 0 3px #df5000 inset;
}
.login > form > label {
width: 80px;
height: 25px;
position: relative;
left:60px;
}
.login > form > input:nth-last-of-type(-n+2) {
margin: 15px 0;
width:100px;
height:35px;
border-style: none;
background-color: #ff0000;
color:#fffdef;
position: relative;
left:66px;
}
.login > form > input:nth-last-of-type(-n+2):hover{
background-color: #178CEE;
}
</style>
</head>
<body>
<div class="login">
<h3>用戶登陸</h3>
<form action="0114verify.php?action=login" method="post" name="sign-in">
<label for="username"><span>用戶名:</span></label>
<input type="text" name="username" id="username" autofocus placeholder="請(qǐng)輸入你的賬號(hào)" required>
<label for="pwd1"><span>密 碼:</span></label>
<input type="password" name="pwd" id="pwd" placeholder="請(qǐng)輸入你的密碼" required>
<input type="submit" value="登陸">
<input type="reset" value="重置">
</form>
<div>
<span>沒(méi)有賬號(hào),請(qǐng)點(diǎn)此處 <a href="0114register.php">注冊(cè)</a> </span>
</div>
</div>
</body>
</html>
3、注冊(cè)頁(yè)面代碼(利用之前做的頁(yè)面沒(méi)有做修改,但只利用用戶名和密碼1、密碼2三項(xiàng),其他項(xiàng)不考慮):
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陸</title>
<style>
a {
text-decoration: none;
}
.login{
width: 400px;
height: 450px;
background-color: #55a532;
margin:0 auto;
padding: 10px;
font-size: 16px;
color: #fffdef;
border-radius: 20px;
}
.login:hover {
box-shadow: 0 0 3px #333333 ;
}
.login > h3 {
width: 100px;
margin: 20px auto;
color: #222222;
}
.login > :last-child {
width: 300px;
margin: 10px auto;
text-align: center;
}
.login > form {
display: grid;
grid-template-columns: 70px 1fr;
grid-template-rows:repeat(7,40px) ;
place-items: center;
}
.login > form > input {
width: 200px;
height: 25px;
}
.login > form > input:nth-last-of-type(n+3):hover {
box-shadow: 0 0 3px #df5000 inset;
}
.login > form > label {
width: 80px;
height: 25px;
position: relative;
left:60px;
}
.login > form > input:nth-last-of-type(-n+2) {
margin: 15px 0;
width:100px;
height:35px;
border-style: none;
background-color: #ff0000;
color:#fffdef;
position: relative;
left:66px;
}
.login > form > input:nth-last-of-type(-n+2):hover{
background-color: #178CEE;
}
</style>
</head>
<body>
<div class="login">
<h3>新用戶注冊(cè)</h3>
<form action="0114verify.php?action=register" method="post" name="sign-in">
<label for="username"><span>用戶名:</span></label>
<input type="text" name="username" id="username" autofocus placeholder="請(qǐng)輸入你的賬號(hào)" required>
<label for="pwd1"><span>密 碼:</span></label>
<input type="password" name="pwd1" id="pwd1" placeholder="請(qǐng)輸入你的密碼" required>
<label for="pwd2"><span>密 碼:</span></label>
<input type="password" name="pwd2" id="pwd2" placeholder="請(qǐng)?jiān)俅屋斎肽愕拿艽a" required>
<label for="email"><span>郵箱</span></label>
<input type="email" name="email" id="email" placeholder="請(qǐng)輸入郵箱" required>
<label for="tel"><span>電話</span></label>
<input type="tel" name="tel" id="tel" placeholder="請(qǐng)輸入手機(jī)號(hào)" required>
<label for="gender"><span>性別</span></label>
<div>
<input type="radio" name="gender" value="1" checked>男
<input type="radio" name="gender" value="2" >女
<input type="radio" name="gender" value="3" >保密
</div>
<label for="like"><span>興趣愛(ài)好</span></label>
<div>
<input type="checkbox" name="like[]" value="1">html
<input type="checkbox" name="like[]" value="2">css
<input type="checkbox" name="like[]" value="3" checked id="like">php
</div>
<input type="submit" value="注冊(cè)">
<input type="reset" value="重置">
</form>
<div>
<span>已有賬號(hào),請(qǐng)點(diǎn)此處 <a href="0114login.php">登陸</a> </span>
</div>
</div>
</body>
</html>
4、驗(yàn)證頁(yè)面代碼:
//驗(yàn)證來(lái)源的合法性
$allow_urls=['0114login.php','0114register.php','0114index.php'];
//echo $_SERVER['HTTP_REFERER'];
$url=(filter_input(INPUT_SERVER,'HTTP_REFERER'));
$url=basename($url);
//echo $url;
$users_dates=[
['username'=>'ldy','pwd'=>'7c4a8d09ca3762af61e59520943dc26494f8941b'],
['username'=>'cqw','pwd'=>'7c4a8d09ca3762af61e59520943dc26494f8941b']
];
if (in_array($url,$allow_urls)) {
// echo '在白名單';
$action= filter_input(INPUT_GET,'action',FILTER_SANITIZE_STRING);
$action=strtolower($action);
//來(lái)判斷是登陸還是注冊(cè)
switch ($action) {
case 'login' ://登陸驗(yàn)證
// echo $action;
// echo $_POST['username'];
// echo $_POST['pwd'];
$name=filter_input(INPUT_POST,'username',FILTER_SANITIZE_STRING);
$pwd=filter_input(INPUT_POST,'pwd',FILTER_SANITIZE_STRING);
$pwd=sha1($pwd);
$result=array_filter($users_dates,function($user)use($name,$pwd){
return $name===$user['username']&&$pwd===$user['pwd'];
});
// print_r($result);
if (count($result)===1) {
$cook=serialize($result[0]);
print_r(unserialize($cook));
setcookie('user',$cook);
exit('<script>alert("驗(yàn)證通過(guò)");location.href="0114index.php"</script>');
} else {
exit('<script>alert("郵箱或密碼錯(cuò)誤,或沒(méi)有帳號(hào)");location.href="0114register.php"</script>');
}
break;
case 'logout' ://登出操作
if(filter_input(INPUT_COOKIE,'user')){
setcookie('user',null,time()-3600);
exit('<script>alert("退出成功");location.href="0114index.php"</script>');
}
break;
case 'register' ://注冊(cè)操作
if (filter_has_var(INPUT_POST,'username')){
$name=filter_input(INPUT_POST,'username',FILTER_SANITIZE_STRING);
$password1=filter_input(INPUT_POST,'pwd1',FILTER_SANITIZE_STRING);
$password2=filter_input(INPUT_POST,'pwd2',FILTER_SANITIZE_STRING);
if($password1===$password2){
$pwd=$password1;
$pwd=sha1($pwd);
}
$data=compact('name','pwd');
$users_dates[]=array_push($users_dates,$data);
if(array_push($users_dates,$data)) {
exit('<script>alert("注冊(cè)成功");location.assign("0114index.php");</script>');
}
}
break;
default:
exit();
}
} else {
echo '不在白名單';
die;
}
1、首頁(yè)效果(未登陸)
2、首頁(yè)(登陸)
3、登陸和注冊(cè)頁(yè)面:
4、登陸頁(yè)面(登陸狀態(tài)進(jìn)入):
由于session版的效果跟cookies的一直,所以只上傳關(guān)鍵代碼
1、首頁(yè)代碼:(通過(guò)$_SESSION[];)判斷登陸狀態(tài)
session_start();//開啟會(huì)話
if (isset($_SESSION['user'])) {
$users=$_SESSION['user']
$status='退出';
$status_url='0114verify.php?action=logout';
}else {
$users='用戶注冊(cè)';
$user_url='0114register.php';
$status='登陸';
$status_url='0114login.php';
}
?>
2、登錄頁(yè)代碼:(判斷登陸狀態(tài),禁止重復(fù)登陸)
session_start();//開啟會(huì)話
if (isset($_SESSION['user'])) {
exit('<script>alert("請(qǐng)不要重復(fù)登錄");location.href="0114index.php"</script>');
}
3、驗(yàn)證頁(yè)代碼:
//1、先開啟session會(huì)話
session_start();
//驗(yàn)證來(lái)源合法性,和cookies一樣
………………
//分發(fā)處理
……………………
//1、登陸處理:創(chuàng)建session ,(通過(guò)session[];保存登錄信息到服務(wù)器)
session['user']=$result;
……………………
//2、退出處理:
session_destroy();
……………………
1、cookie相關(guān)知識(shí)點(diǎn):
(1)、setcookie('name','值信息','失效時(shí)間');
(2)、讀取cookie信息:$_COOKIE['變量名'];
(3)、刪除cookie信息:setcookie('name',null,time()-1);
2、session相關(guān)知識(shí)點(diǎn):
(1)、session_start();
開啟會(huì)話,調(diào)用session必須先開啟會(huì)話
(2)、讀取session信息:$_SESSION['變量名'];
(3)、結(jié)束當(dāng)前會(huì)話刪除session會(huì)話信息:session_destroy();
unset($_SESSION['']);
注銷單個(gè)會(huì)話
3、相關(guān)知識(shí)補(bǔ)充:
(1)、過(guò)濾函數(shù):($_SERVER['HTTP_ERFERER']
進(jìn)入當(dāng)前頁(yè)面的上一個(gè)頁(yè)面的url地址;)
filter_has_var();
判斷是否存在
filter_input();
filter_var();
(2)、serialize();
序列化,寫入cookie時(shí)
unserialize();
反序列化,讀取cookie時(shí)需要先反序列化
(3)、數(shù)組操作:
array_push($arr,$a);
將$a入棧$arr;
array_pop($arr);
將$arr最后一個(gè)元素出棧;
array_filter($arr,callback);
$arr要循環(huán)的數(shù)組,callback回調(diào)函數(shù),返回過(guò)濾后符合回調(diào)函數(shù)的數(shù)組
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)