1、首页代码
//判断登陆状态,输出显示登陆状态变量(没有学JS利用PHP的if语句来判断输出登陆和登出状态)
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='用户注册';
$user_url='0114register.php';
$status='登陆';
$status_url='0114login.php';
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>种业圈</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">
//根据判断输出的变量信息显示登陆状态
<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、登陆页面代码:
if (filter_has_var(INPUT_COOKIE,'user')) {
exit('<script>alert("请勿重复登陆");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="请输入你的账号" required>
<label for="pwd1"><span>密 码:</span></label>
<input type="password" name="pwd" id="pwd" placeholder="请输入你的密码" required>
<input type="submit" value="登陆">
<input type="reset" value="重置">
</form>
<div>
<span>没有账号,请点此处 <a href="0114register.php">注册</a> </span>
</div>
</div>
</body>
</html>
3、注册页面代码(利用之前做的页面没有做修改,但只利用用户名和密码1、密码2三项,其他项不考虑):
<!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>新用户注册</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="请输入你的账号" required>
<label for="pwd1"><span>密 码:</span></label>
<input type="password" name="pwd1" id="pwd1" placeholder="请输入你的密码" required>
<label for="pwd2"><span>密 码:</span></label>
<input type="password" name="pwd2" id="pwd2" placeholder="请再次输入你的密码" required>
<label for="email"><span>邮箱</span></label>
<input type="email" name="email" id="email" placeholder="请输入邮箱" required>
<label for="tel"><span>电话</span></label>
<input type="tel" name="tel" id="tel" placeholder="请输入手机号" 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>兴趣爱好</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="注册">
<input type="reset" value="重置">
</form>
<div>
<span>已有账号,请点此处 <a href="0114login.php">登陆</a> </span>
</div>
</div>
</body>
</html>
4、验证页面代码:
//验证来源的合法性
$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);
//来判断是登陆还是注册
switch ($action) {
case 'login' ://登陆验证
// 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("验证通过");location.href="0114index.php"</script>');
} else {
exit('<script>alert("邮箱或密码错误,或没有帐号");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' ://注册操作
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("注册成功");location.assign("0114index.php");</script>');
}
}
break;
default:
exit();
}
} else {
echo '不在白名单';
die;
}
1、首页效果(未登陆)
2、首页(登陆)
3、登陆和注册页面:
4、登陆页面(登陆状态进入):
由于session版的效果跟cookies的一直,所以只上传关键代码
1、首页代码:(通过$_SESSION[];)判断登陆状态
session_start();//开启会话
if (isset($_SESSION['user'])) {
$users=$_SESSION['user']
$status='退出';
$status_url='0114verify.php?action=logout';
}else {
$users='用户注册';
$user_url='0114register.php';
$status='登陆';
$status_url='0114login.php';
}
?>
2、登录页代码:(判断登陆状态,禁止重复登陆)
session_start();//开启会话
if (isset($_SESSION['user'])) {
exit('<script>alert("请不要重复登录");location.href="0114index.php"</script>');
}
3、验证页代码:
//1、先开启session会话
session_start();
//验证来源合法性,和cookies一样
………………
//分发处理
……………………
//1、登陆处理:创建session ,(通过session[];保存登录信息到服务器)
session['user']=$result;
……………………
//2、退出处理:
session_destroy();
……………………
1、cookie相关知识点:
(1)、setcookie('name','值信息','失效时间');
(2)、读取cookie信息:$_COOKIE['变量名'];
(3)、删除cookie信息:setcookie('name',null,time()-1);
2、session相关知识点:
(1)、session_start();
开启会话,调用session必须先开启会话
(2)、读取session信息:$_SESSION['变量名'];
(3)、结束当前会话删除session会话信息:session_destroy();
unset($_SESSION['']);
注销单个会话
3、相关知识补充:
(1)、过滤函数:($_SERVER['HTTP_ERFERER']
进入当前页面的上一个页面的url地址;)
filter_has_var();
判断是否存在
filter_input();
filter_var();
(2)、serialize();
序列化,写入cookie时
unserialize();
反序列化,读取cookie时需要先反序列化
(3)、数组操作:
array_push($arr,$a);
将$a入栈$arr;
array_pop($arr);
将$arr最后一个元素出栈;
array_filter($arr,callback);
$arr要循环的数组,callback回调函数,返回过滤后符合回调函数的数组
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号