登陆注册实战案例:
一、COOKIE形式
1、登陆
<?php
if(isset($_COOKIE['user'])){
// echo 'OK';
// exit('<script>alert("登陆成功!");location.href="index.php";</script>');
exit('<script>alert("已经登陆,请勿重复等登陆!");location.href="index.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>
<style>
* {
margin: 0;
padding: 0;
}
h2 {
/* display: block; */
width: 350px;
margin: 0 auto;
text-align: center;
padding-top: 10px;
box-sizing: border-box;
}
form {
margin: 10px auto;
width: 350px;
height: 250px;
background-color: #5384e8;
display: flex;
flex-flow: column nowrap;
justify-content: space-evenly;
align-content: center;
align-items: center;
font-size: 1.2rem;
}
form:hover {
box-shadow: 0 0 5px #626262;
}
form>.button {
width: 280px;
display: flex;
justify-content: space-evenly;
}
form>.button>input {
width: 100px;
height: 30px;
background-color: #00bb00;
border: none;
border-radius: 15px;
}
form>.button>input:hover {
background-color: red;
color: white;
}
a {
color: white;
text-decoration: none;
}
</style>
</head>
<body>
<h2>用户注册</h2>
<form action="handle.php?action=select" method="POST">
<!-- <fieldset> -->
<!-- <legend align="center">用户注册</legend> -->
<div>
<label for="username">账户:</label>
<input type="email" required name="username" id="username" placeholder="example@163.com">
</div>
<div>
<label for="p2">密码:</label>
<input type="password" required name="p2" id="p2" placeholder="不少于六位">
</div>
<!-- </fieldset> -->
<div class="button">
<input type="submit" value="登陆">
<input type="reset" value="重置">
</div>
<div>
<a href="regist.php">没有账号,点击此处注册!</a>
</div>
</form>
</body>
</html>
2、注册
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用户注册</title>
<style>
* {
margin: 0;
padding: 0;
}
h2 {
/* display: block; */
width: 350px;
margin: 0 auto;
text-align: center;
padding-top: 10px;
box-sizing: border-box;
}
form {
margin: 10px auto;
width: 350px;
height: 450px;
background-color: #5384e8;
display: flex;
flex-flow: column nowrap;
justify-content: space-evenly;
align-content: center;
align-items: center;
font-size: 1.2rem;
}
form:hover {
box-shadow: 0 0 5px #626262;
}
form>.button {
width: 280px;
display: flex;
justify-content: space-evenly;
}
form>.button>input {
width: 100px;
height: 30px;
background-color: #00bb00;
border: none;
border-radius: 15px;
}
form>.button>input:hover {
background-color: red;
color: white;
}
a {
color: white;
text-decoration: none;
}
</style>
</head>
<body>
<h2>用户注册</h2>
<form action="handle.php?action=insert" method="POST" name="my_form">
<!-- <fieldset> -->
<!-- <legend align="center">用户注册</legend> -->
<div>
<label for="username">账户:</label>
<input type="email" required name="username" id="username" placeholder="example@163.com">
</div>
<div>
<label for="name">账户:</label>
<input type="text" required name="name" id="name" placeholder="输入你的昵称">
</div>
<div>
<label for="p1">密码:</label>
<input type="password" required name="p1" id="p1" placeholder="不少于六位">
</div>
<div>
<label for="p2">密码:</label>
<input type="password" required name="p2" id="p2" placeholder="不少于六位">
</div>
<!-- <span id="tips" name="tips" style="color: red"></span> -->
<!-- </fieldset> -->
<div class="button">
<input type="submit" value="注册">
<input type="reset" value="重置">
</div>
<div>
<a href="login.php">已有账号,点击此处登陆!</a>
</div>
</form>
</body>
</html>
3、首页
<?php
if(isset($_COOKIE['user'])){
$user=unserialize($_COOKIE['user']);
// var_dump($user);
$username=$user['name'];
$states="登出";
$statesurl="handle.php?action=logout";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>首页</title>
<style>
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
}
nav {
width: 100%;
height: 40px;
background: #00ffff;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 20px;
box-sizing: border-box;
font-size: 1.4rem;
}
nav a {
color: white;
}
</style>
</head>
<body>
<nav>
<a href="">首页</a>
<div><a href="<?php echo null?? "regist.php";?>"><?php echo $username ?? "注册"; ?></a> <a
href="<?php echo $statesurl ?? "login.php"; ?>"><?php echo $states ?? "登陆";?></a>
</div>
</nav>
</body>
</html>
4、登陆注册处理脚本
<?php
// 连接数库所需参数
$dsn="mysql:host=php.edu;dbname=php.edu;charset=utf8;port=3306";
$db=new PDO($dsn,'phpedu','123456');
// var_dump($db);
$action=filter_input(INPUT_GET,'action',FILTER_SANITIZE_STRING);
$action=strtolower($action);
switch($action){
case 'select':
// 获取查询条件
// echo $_POST['username'];
$user=filter_input(INPUT_POST,'username',FILTER_SANITIZE_EMAIL);
$user=trim($user);
// echo $user;
// 准备sql语句
$sql="select `id`,`password`,`name` FROM `users` WHERE `email`='{$user}'";
// 预处理sql语句
$stmt=$db->prepare($sql);
// var_dump($stmt);
$stmt->execute();
$res=$stmt->fetch(PDO::FETCH_ASSOC);
$password=filter_input(INPUT_POST,'p2',FILTER_SANITIZE_STRING);
// echo md5($password),'<br>',$res['password'];
if(md5($password)===$res['password']){
setcookie('user',serialize($res),time()+7200);
exit('<script>alert("登陆成功!");location.href="index.php";</script>');
}else{
exit('<script>alert("密码账号错误!");location.herf="login.php";</script>');
}
// echo md5('12345678');
// printf("<pre>%s</pre>",print_r($res,true));
break;
case 'logout':
setcookie('user',null,time()-3600);
exit('<script>alert("登处成功!");location.href="index.php";</script>');
break;
case 'insert':
$arg=[
'username'=>FILTER_SANITIZE_EMAIL,
'p1'=>FILTER_SANITIZE_STRING,
'name'=>FILTER_SANITIZE_STRING,
'p2'=>FILTER_SANITIZE_STRING
];
$user=filter_input_array(INPUT_POST,$arg);
if($user['p1']!==$user['p2']){
exit('<script>alert("密码不一致!");location.href="regist.php";</script>');
}
$user['p1']=md5($user['p1']);
$data=array_values($user);
array_pop($data);
array_push($data,time());
$sql="insert `users` SET `email`='{$data[0]}',`password`='{$data[1]}',`name`='{$data[2]}',`datetime`={$data[3]}";
// 处理用户数据符合写入数据库要求
// printf("<pre>%s</pre>",print_r($data,true));
// 预处理sql语句;
$stmt=$db->prepare($sql);
// var_dump($stmt);
$stmt->execute();
if($stmt->rowCount()===1){
setcookie('user',serialize($user),time()+3600);
exit('<script>alert("登陆成功!");location.href="index.php";</script>');
}
break;
default:
exit('<script>alert("未知错误!");location.href="index.php";</script>');
}
$db=null;
5、运行结果
二、SESSION形式(每一次对session处理都必须开启会话);
session_start();//开启会话
$_SESSION['user']=$user;//把用户信息写入session
if(isset($_SESSION['user'])){
//判断是否登陆,并作出处理
}
//登出操作
session_distroy();
总结:
1、重点数据库连接:PDO连接、select、insert;
2、filter_input(INPUT_GET|INPUT_POST,”获取的值的name属性”,过滤类型);
3、cookie相关知识点:
(1)、setcookie(‘name’,’值信息’,’失效时间’);
(2)、读取cookie信息:$_COOKIE[‘变量名’];
(3)、删除cookie信息:setcookie(‘name’,null,time()-1);
4、session相关知识点:
(1)、session_start();开启会话,调用session必须先开启会话
(2)、读取session信息:$_SESSION[‘变量名’];
(3)、结束当前会话删除session会话信息:session_destroy();
unset($_SESSION[‘’]);注销单个会话
5、serialize()
和unserialize()
:序列化和发序列化;