
<?phpsession_start();// 判断用户是否已经登录?if (isset($_SESSION['user'])) $user = unserialize($_SESSION['user']);// print_r($user);?><!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>Document</title></head><body><nav><?php if (isset($user)) : ?><a href="" id="logout">退出</a><?php else : ?><a href="login.php">登录</a><?php endif ?></nav><script>document.querySelector('#logout').addEventListener('click', function(event) {if (confirm('是否退出?')) {// 禁用默认跳转行为event.preventDefault();// 跳转到处理器location.assign('handle.php?action=logout');}});</script></body></html>
<!DOCTYPE html><html lang="zh-CN"><?phpsession_start();// 判断用户是否已经登录?if (isset($_SESSION['user'])) echo '<script>alert("不要重复登录");locatoin.href="index.php"</script>';?><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></head><body><form action="handle.php?action=login" method="POST"><fieldset style="display: inline-block;background:lightcyan"><legend>用户登录</legend><p><input type="email" name="email" placeholder="user@email.com" require></p><p><input type="password" name="password" placeholder="不少于6位" require></p><p><button>提交</button></p></fieldset><a href="register.php">如果没有帐号,请先注册</a></form></body></html>
<?php// 开启会话session_start();// 根据用户的不同请求,执行不同的操作// 比如:登录 , 注册, 退出// 连接数据并获取用户表中的数据$db = new PDO('mysql:dbname=phpedu', 'root', 'root');$stmt = $db->prepare('SELECT * FROM `user`');$stmt->execute();$users = $stmt->fetchAll(PDO::FETCH_ASSOC);// print_r($users);$action = $_GET['action'];switch (strtolower($action)) {// 登录case 'login'://要保证数据是通用POST请求发送的if ($_SERVER['REQUEST_METHOD'] === 'POST') {// 先拿到登录数据extract($_POST);// $email = $_POST['email'];// $password = sha1($_POST['password']);// $result 是数组$result = array_filter($users, function ($user) use ($email, $password) {return $user['email'] === $email && $user['password'] === md5($password);});if (count($result) === 1) {// 验证成功,将用户信息写到SESSION// print_r(serialize(array_pop($result)));// $a = serialize(array_pop($result));// print_r(unserialize($a));// 将用户信息序列化之后保存到SESSION中$_SESSION['user'] = serialize(array_pop($result));exit('<script>alert("验证通过");location.href="index.php"</script>');} else {exit('<script>alert("邮箱或密码错误");location.href="index.php"</script>');}} else {die('请求错误');}break;// 退出case 'logout':if (isset($_SESSION['user'])) {session_destroy();exit('<script>alert("退出成功");location.assign("index.php")</script>');} else {session_destroy();exit('<script>alert("退出成功");location.assign("index.php")</script>');}break;// 注册case 'register':// 1. 获取到新用户的信息extract($_POST);$password = md5($password);$register_time = time();$name = getSubstr($email, '@');echo '账号:', $name, '<br>';echo '邮箱:', $email, '<br>';echo '密码:', $password, '<br>';echo '注册时间:', $register_time, '<br>';echo ('<a href="handle.php?action=logout">退出<a>');// 2. 将新用户添加到表中$sql = 'INSERT user SET name=?,email=?,password=?,register_time=?';$stmt = $db->prepare($sql);$stmt->execute([$name, $email, $password, $register_time]);if ($stmt->rowCount() === 1) {echo '<script>alert("注册成功");locaton.href="index.php"</script>';} else {exit('<script>alert("注册失败");locaton.href="index.php"</script>');}break;}//取文本左边function getSubstr($str, $rightStr){$right = strpos($str, $rightStr);return substr($str, 0, $right);}
<!DOCTYPE html><html lang="zh-CN"><?phpsession_start();// 判断用户是否已经登录?if (isset($_SESSION['user'])) echo '<script>alert("不要重复登录");locatoin.href="index.php"</script>';?><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></head><body><form action="handle.php?action=register" method="POST"><fieldset style="display: inline-block;background:lightcyan"><legend>用户注册</legend><p><input type="email" name="email" placeholder="user@email.com" value="tianning@qq.com" require></p><p><input type="password" name="password" placeholder="不少于6位" value="666666" require></p><p><input type="password" name="password" placeholder="二次必须一致" value="666666" require></p><!-- 二次密码是否一致用JS进行验证就可以了 --><p><button onclick="verifyPassword(event)">提交</button></p></fieldset></form><script>function verifyPassword(event) {let input = document.querySelectorAll('input');if (input[1].value !== input[2].value) {alert("两次密码不一致,请重新输入");input[1].value = null;input[2].value = null;event.preventDefault();}}</script></body></html>
相关推荐
© 2020 asciim码
人生就是一场修行