php实现注册登录流程操作
代码
index.php
<?php
namespace _0822;
session_start();
// 判断是否已登录?
if (isset($_SESSION['user'])) {
$user = unserialize($_SESSION['user']);
}
function get_time($var){
return date('Y-m-d H:i:s',$var);
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>首页/入口文件</title>
<link rel="stylesheet" href="/0822/zuoye/login/css/index.css">
</head>
<body>
<nav>
<a href="index.php">我的博客</a>
<?php if (isset($user)) : ?>
<span style="margin-left: 300px"><?=$user['name']?></span>
<a id="logout">退出</a>
<?php else: ?>
<a href="login.php">登录</a>
<?php endif ?>
</nav>
<script>
// 为退出按钮创建事件监听器
document.querySelector('#logout').addEventListener('click', function(event) {
if (confirm('是否退出')) {
// 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
event.preventDefault();
// 跳转到退出事件处理器
window.location.assign('handle.php?action=logout');
}
});
</script>
<?php
if( isset($user)){
$time = get_time($user['register_time']);
echo "<ul>";
echo "<li>昵称:{$user['name']}</li>";
echo "<li>邮箱:{$user['email']}</li>";
echo "<li>注册时间:$time</li>";
echo "</ul>";
}else{
echo '<a href="register.php">还没有帐号, 等你注册一个吧</a>';
echo '<br>';
echo '<a href="login.php">我有帐号,直接登录</a>';
}
?>
</body>
</html>
login.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<link rel="stylesheet" type="text/css" href="/0822/zuoye/login/css/style.css">
</head>
<body>
<h3>用户登录</h3>
<form action="handle.php?action=login" method="post">
<div>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email" placeholder="demo@email.com" required autofocus>
</div>
<div>
<label for="password">密码:</label>
<input type="password" name="password" id="password" placeholder="不少于6位" required>
</div>
<div>
<button>提交</button>
</div>
</form>
<a href="register.php">还没有帐号, 注册一个吧</a>
</body>
</html>
register.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/0822/zuoye/login/css/style.css">
<title>注册用户</title>
</head>
<body>
<h3>用户注册</h3>
<form action="handle.php?action=register" method="post" onsubmit="return compare()">
<div>
<label for="name">呢称:</label>
<input type="text" name="name" id="name" placeholder="不少于3个字符" required autofocus>
</div>
<div>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email" placeholder="demo@email.com" required>
</div>
<div>
<label for="p1">密码:</label>
<input type="password" name="p1" id="p1" placeholder="不少于6位" required>
</div>
<div>
<label for="p2">重复:</label>
<input type="password" name="p2" id="p2" placeholder="必须与上面一致" required>
</div>
<div>
<button>提交</button><span id="tips" style="color: red"></span>
</div>
</form>
<a href="login.php">我有帐号,直接登录</a>
<script>
// 验证二次密码是否相等?
function compare() {
if (document.forms[0].p1.value.trim() !== document.forms[0].p2.value.trim()) {
document.querySelector('#tips').innerText = '二次密码不相等';
return false;
}
}
</script>
</body>
</html>
handle.php
<?php
namespace _0822;
use PDO;
// 开启会话:必须写在顶部
session_start();
//查询用户表中的数据use表
$db = new PDO('mysql:dbname=phpedu', 'root', 'root');
$stmt = $db->prepare('SELECT * FROM `users`;');
if ($stmt->execute()) {
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else {
print_r($stmt->errorInfo());
}
// 获取用户操作类型
$action = strtolower($_GET['action']);
switch ($action) {
// 1. 登录
case 'login':
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 获取登录用户的数据:邮箱和密码
$email= $_POST['email'];
$password= sha1($_POST['password']);
$result = array_filter($users, function ($user) use ($email, $password) {
return $user['email'] === $email && $user['password'] === $password;
});
if (count($result) === 1) {
// 登录成功,写入session
$_SESSION['user'] = serialize(array_pop($result));
exit('<script>alert("验证通过");location.href="index.php"</script>');
}
// echo '请求类型错误'; die;
exit('请求类型错误');
}
// 2. 退出
// no break
case 'logout':
if (isset($_SESSION['user'])) {
session_destroy();
exit('<script>alert("退出成功");location.href="index.php"</script>');
}
// . 注册
// no break
case 'register':
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
// 1. 获取新用户的数据
$email= $_POST['email'];
$name= $_POST['name'];
$password= sha1($_POST['p2']);
$register_time = time();
//防止用户重复提交验证
$result = array_filter($users, function ($user) use ($email, $password) {
return $user['email'] === $email && $user['password'] === $password;
});
if(count($result) === 1){
exit('<script>alert("请勿重复提交数据");location.href="register.php"</script>');
}
// 2. sql
$sql = <<< SQL
INSERT `users`
SET `name`= ?,
`email`= ?,
`password`= ?,
`register_time`= ?;
SQL;
$stmt = $db->prepare($sql);
$data = [$name,$email,$password, $register_time];
if ($stmt->execute($data)) {
if ($stmt->rowCount() > 0) {
// 注册成功之后,让用户自动登录
$sql='SELECT * FROM `users` WHERE `id` = ' . $db->lastInsertId();
$stmt = $db->prepare($sql);
$stmt->execute();
$newUser =$stmt->fetch(PDO::FETCH_ASSOC);
$_SESSION['user'] = serialize($newUser);
exit('<script>alert("注册成功");location.href="index.php"</script>');
} else {
exit('<script>alert("注册失败");location.href="register.php"</script>');
}
} else {
print_r($stmt->errorInfo());
}
}
// no break
default:
exit('参数非法或未定义操作');
}
效果图
1默认未登录状体
2注册成功
3退出相关操作
4注册页面
对重复提交相同信息用户信息提示