登录  /  注册
博主信息
博文 47
粉丝 1
评论 0
访问量 38298
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
用户登录/注册/退出全流程详解
新手1314
原创
1099人浏览过

index-zuoye.php

  1. <?php
  2. session_start();
  3. //! unserialize — 从已存储的表示中创建 PHP 的值
  4. if (isset($_SESSION['user'])) {
  5. $user = unserialize($_SESSION['user']);
  6. }
  7. // print_r($user);
  8. ?>
  9. <!DOCTYPE html>
  10. <html lang="en">
  11. <head>
  12. <meta charset="UTF-8">
  13. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <title>Document</title>
  16. </head>
  17. <body>
  18. <!-- isset — 检测变量是否已声明并且其值不为 null -->
  19. <?php if (isset($user)) :?>
  20. <a href="" id="out-zuoye">退出</a>
  21. <?php else :?>
  22. <a href="login-zuoye.php">登录</a>
  23. <?php endif?>
  24. <script>
  25. let bools = document.querySelector('#out-zuoye');
  26. if (bools) {
  27. document.querySelector('#out-zuoye').addEventListener('click', function(event) {
  28. if (confirm('是否退出')) {
  29. event.preventDefault();
  30. location.assign('handle-zuoye.php?action=out-zuoye');
  31. }
  32. });
  33. }
  34. </script>
  35. </body>
  36. </html>

login-zuoye.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. session_start();
  5. if (isset($_SESSION['user'])) {
  6. echo '<script>alert("不要重复登录");location.href="index-zuoye.php"</script>';
  7. }
  8. ?>
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <title>用户登录表单</title>
  14. </head>
  15. <body>
  16. <form action="handle-zuoye.php?action=login-zuoye" method="post">
  17. <fieldset style="display: inline-block;background:lightcyan">
  18. <legend>用户登录</legend>
  19. <p>
  20. <label for="username">用户名:</label>
  21. <input type="text" name="username" id="username" placeholder="请输入用户名" require>
  22. </p>
  23. <p>
  24. <label for="password">密码:</label>
  25. <input type="password" name="password" id="password" placeholder="请输入密码" require>
  26. </p>
  27. <p>
  28. <button>提交</button>
  29. <a href="register-zuoye.php" style="margin-left: 100px; border:1px solid; text-decoration:none;background-color:#00bcd4;border-radius:8px;">注册</a>
  30. </p>
  31. </fieldset>
  32. </form>
  33. </body>
  34. </html>

register-zuoye.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>用户注册表单</title>
  8. </head>
  9. <body>
  10. <form action="handle-zuoye.php?action=register-zuoye" method="POST">
  11. <fieldset style="display: inline-block;background:lightcyan">
  12. <legend>用户注册</legend>
  13. <p>
  14. <label for="username">用户名:</label>
  15. <!-- require :规定必需在提交之前填写输入字段 -->
  16. <input type="text" name="username" id="username" placeholder="请输入用户名" require>
  17. </p>
  18. <p>
  19. <label for="password">密码:</label>
  20. <input type="password" name="password" id="password" placeholder="请输入密码" require>
  21. </p>
  22. <p>
  23. <label for="again">确认密码:</label>
  24. <input type="password" name="again" id="again" placeholder="请再次输入密码" require>
  25. </p>
  26. <p>
  27. <button>提交</button>
  28. </p>
  29. </fieldset>
  30. </form>
  31. <script>
  32. document.querySelector('button').addEventListener('click', function(event) {
  33. $username = document.querySelector('#username').value;
  34. $pwd = document.querySelector('#password').value;
  35. $pwds = document.querySelector('#again').value;
  36. console.log($username);
  37. console.log($pwd);
  38. console.log($pwds);
  39. if ($username === '') {
  40. event.preventDefault();
  41. alert("用户名不能为空");
  42. return false;
  43. }
  44. if ($pwd === '') {
  45. event.preventDefault();
  46. alert("密码不能为空");
  47. return false;
  48. }
  49. if ($pwds === '') {
  50. event.preventDefault();
  51. alert("请输入确认密码");
  52. return false;
  53. }
  54. if ($username != '' && $pwd !== $pwds) {
  55. event.preventDefault();
  56. alert("两次输入的密码不一致");
  57. }
  58. })
  59. </script>
  60. </body>
  61. </html>

handle-zuoye.php

  1. <?php
  2. // include "register-zuoye.php";
  3. session_start();
  4. $db = new PDO('mysql:dbname=php', 'root', 'root');
  5. $sql = 'SELECT * FROM `login`';
  6. $stmt = $db->prepare($sql);
  7. $stmt->execute();
  8. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  9. // print_r($users);
  10. //!查看是否转跳页面并获取index里的表单action
  11. $action = $_GET['action'];
  12. //!strtolower — 将字符串转化为小写
  13. switch (strtolower($action)) {
  14. //!登录
  15. case 'login-zuoye':
  16. //!REQUEST_METHOD:访问页面使用的请求方法($_SERVER:超全局变量)
  17. // echo $_SERVER['REQUEST_METHOD'];
  18. if ($_SERVER['REQUEST_METHOD']==='POST') {
  19. // print_r($_POST);
  20. //!extract — 从数组中将变量导入到当前的符号表
  21. extract($_POST);
  22. // echo $name = $_POST['username'];
  23. // echo $password = $_POST['password'];
  24. //!array_filter — 使用回调函数过滤数组的元素
  25. $result = array_filter($users, function ($user) use ($username, $password) {
  26. return $user['name'] ===$username &&$user['password'] = md5($password);
  27. });
  28. // printf('<pre>%s</pre>',print_r($result,true));
  29. if (count($result)===1) {
  30. //!serialize — 产生一个可存储的值的表示
  31. //!array_pop — 弹出数组最后一个单元(出栈)
  32. $_SESSION['user'] = serialize(array_pop($result));
  33. //!exit — 输出一个消息并且退出当前脚本
  34. exit('<script>alert("登录成功");location.href="index-zuoye.php"</script>');
  35. } else {
  36. exit('<script>alert("登录失败,账号或密码错误");location.href="index-zuoye.php"</script>');
  37. }
  38. } else {
  39. //!die:结束进程
  40. die('请求错误');
  41. }
  42. break;
  43. //!退出
  44. case 'out-zuoye':
  45. if (isset($_SESSION['user'])) {
  46. //!session_destroy — 销毁一个会话中的全部数据
  47. session_destroy();
  48. //!location.assign:载入一个新文档,浏览器的后退按钮可用
  49. exit('<script>alert("退出成功");location.assign("index-zuoye.php")</script>');
  50. }
  51. break;
  52. //!注册
  53. case 'register-zuoye':
  54. // 获取注册用户信息
  55. $name = $_POST['username'];
  56. $password = md5($_POST['password']);
  57. $register_time = time();
  58. //print_r($_POST);
  59. //将新用户添加到表中
  60. $sqls = 'INSERT `login` SET `name`=?,`password`=?,`register_time`=? ';
  61. $stmt = $db->prepare($sqls);
  62. $stmt ->execute(array($name,$password,$register_time));
  63. if ($stmt->rowCount()===1) {
  64. exit('<script>alert("注册成功");location.href="index-zuoye.php"</script>');
  65. } else {
  66. exit('<script>alert("注册失败");</script>') ;
  67. }
  68. break;
  69. }
批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学