Blogger Information
Blog 14
fans 0
comment 1
visits 12854
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分别用cookie与session实现登陆验证及新用户注册
王珂
Original
896 people have browsed it

cookie方式

index.php

  1. <?php
  2. // 判断是否已经登录?
  3. if (isset($_COOKIE['user'])) $user = unserialize($_COOKIE['user']);
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <title>管理系统</title>
  10. <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
  11. </head>
  12. <body>
  13. <div class="container">
  14. <div class="row">
  15. <div class="col-md-12">
  16. <!-- 顶部导航 -->
  17. <nav class="navbar navbar-default">
  18. <div class="container-fluid">
  19. <!-- Brand and toggle get grouped for better mobile display -->
  20. <div class="navbar-header">
  21. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
  22. <span class="sr-only">Toggle navigation</span>
  23. <span class="icon-bar"></span>
  24. <span class="icon-bar"></span>
  25. <span class="icon-bar"></span>
  26. </button>
  27. <a class="navbar-brand" href="index.php">管理系统</a>
  28. </div>
  29. <!-- Collect the nav links, forms, and other content for toggling -->
  30. <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
  31. <ul class="nav navbar-nav navbar-right">
  32. <!-- 根据cookie判断用户是否登录,显示不同的内容 -->
  33. <?php if (isset($user)) : ?>
  34. <li><a href="" id="logout"><span style="color:red"><?php echo $user['name']?></span>&nbsp;&nbsp;退出</a></li>
  35. <?php else: ?>
  36. <li><a href="login.php">登录</a></li>
  37. <?php endif ?>
  38. </ul>
  39. </div><!-- /.navbar-collapse -->
  40. </div><!-- /.container-fluid -->
  41. </nav>
  42. </div>
  43. </div>
  44. <script>
  45. // 为退出按钮创建事件监听器
  46. document.querySelector('#logout').addEventListener('click', function(event) {
  47. if (confirm('是否退出')) {
  48. // 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
  49. event.preventDefault();
  50. // 跳转到退出事件处理器
  51. window.location.assign('handle.php?action=logout');
  52. }
  53. });
  54. </script>
  55. </body>
  56. </html>

handle.php

  1. <?php
  2. $pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');
  3. $stmt = $pdo->prepare('SELECT * FROM `user`');
  4. $stmt->execute();
  5. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  6. //print_r( $users);
  7. $action = $_GET['action'];
  8. switch (strtolower($action))
  9. {
  10. //登陆
  11. case 'login':
  12. if($_SERVER['REQUEST_METHOD']='POST'){
  13. $email = $_POST['email'];
  14. $password = sha1($_POST['password']);
  15. $results = array_filter($users, function($user) use ($email, $password) {
  16. return $user['email'] === $email && $user['password'] === $password;
  17. });
  18. if(count($results) === 1){
  19. setcookie('user',serialize(array_pop($results)));
  20. exit('<script>alert("验证通过");location.href="index.php"</script>');
  21. }else{
  22. exit('<script>alert("邮箱或密码错误,或者还没有帐号");location.href="login.php";</script>');
  23. }
  24. }else {
  25. die('请求类型错误');
  26. }
  27. break;
  28. //退出
  29. case 'logout':
  30. if(isset($_COOKIE['user']))
  31. {
  32. setcookie('user',null,time()-3600);
  33. exit('<script>alert("退出成功");location.assign("index.php")</script>');
  34. }
  35. break;
  36. case 'register':
  37. $name = $_POST['name'];
  38. $email = $_POST['email'];
  39. $password = sha1($_POST['p1']);
  40. $register_time = time();
  41. $sql = "INSERT `user` SET `name`='{$name}', `email`='{$email}', `password`='{$password}', `create_time`={$register_time}";
  42. $stmt = $pdo->prepare($sql);
  43. $stmt->execute();
  44. if ($stmt->rowCount() === 1) exit('<script>alert("注册成功");location.assign("login.php")</script>');
  45. else exit('<script>alert("注册失败");location.assign("register.php")</script>');
  46. break;
  47. // 未定义
  48. default:
  49. exit('未定义操作');
  50. }

login.php

  1. <?php
  2. // 判断是否已登录
  3. if (isset($_COOKIE['user']))
  4. exit('<script>alert("请不要重复登录");location.href="index.php";</script>');
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>管理系统</title>
  11. <!-- 使用load标签加载资源文件 -->
  12. <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
  13. </head>
  14. <body>
  15. <div class="container">
  16. <div class="row">
  17. <div class="col-md-4">
  18. </div>
  19. <div class="col-md-4">
  20. <div class="page-header text-center">
  21. <h3>管理系统登陆</h3>
  22. </div>
  23. <form class="form-horizontal"
  24. action="handle.php?action=login" method="post">
  25. <div class="form-group">
  26. <label for="inputEmail3" class="col-sm-2 control-label">邮箱:</label>
  27. <div class="col-sm-10">
  28. <input type="email" name="email" class="form-control" id="inputEmail3" placeholder="邮箱">
  29. </div>
  30. </div>
  31. <div class="form-group">
  32. <label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
  33. <div class="col-sm-10">
  34. <input type="password" name="password" class="form-control" id="inputPassword3" placeholder="密码">
  35. </div>
  36. </div>
  37. <div class="form-group">
  38. <div class="col-sm-offset-2 col-sm-10">
  39. <button type="submit" class="btn btn-primary btn-block">登录</button>
  40. </div>
  41. </div>
  42. </form>
  43. <div class="form-group">
  44. <label ><a href="register.php">还没有帐号, 注册一个吧</a></label>
  45. </div>
  46. </div>
  47. <div class="col-md-4">
  48. </div>
  49. </div>
  50. </div>
  51. </body>
  52. </html>

register.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>管理系统</title>
  6. <!-- 使用load标签加载资源文件 -->
  7. <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div class="row">
  12. <div class="col-md-4">
  13. </div>
  14. <div class="col-md-4">
  15. <div class="page-header text-center">
  16. <h3>管理系统注册</h3>
  17. </div>
  18. <form class="form-horizontal"
  19. action="handle.php?action=register" method="post">
  20. <div class="form-group">
  21. <label for="inputEmail3" class="col-sm-2 control-label">用户:</label>
  22. <div class="col-sm-10">
  23. <input type="text" name="name" class="form-control" id="name" placeholder="用户名"  required>
  24. </div>
  25. </div>
  26. <div class="form-group">
  27. <label for="inputEmail3" class="col-sm-2 control-label">邮箱:</label>
  28. <div class="col-sm-10">
  29. <input type="email" name="email" class="form-control" id="inputEmail3" placeholder="邮箱"  required>
  30. </div>
  31. </div>
  32. <div class="form-group">
  33. <label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
  34. <div class="col-sm-10">
  35. <input type="password" name="password1" class="form-control" id="inputPassword3" placeholder="密码"  required>
  36. </div>
  37. </div>
  38. <div class="form-group">
  39. <label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
  40. <div class="col-sm-10">
  41. <input type="password" name="password2" class="form-control" id="inputPassword4" placeholder="必须与上面一致"  required>
  42. </div>
  43. </div>
  44. <div class="form-group">
  45. <div class="col-sm-offset-2 col-sm-10">
  46. <button type="submit" class="btn btn-primary btn-block">注册</button>
  47. </div>
  48. </div>
  49. </form>
  50. <a href="login.php">我有帐号,直接登录</a>
  51. <script>
  52. // 验证二次密码是否相等?
  53. function compare() {
  54. if (document.forms[0].p1.value.trim() !== document.forms[0].p2.value.trim()) {
  55. document.querySelector('#tips').innerText = '二次密码不相等';
  56. return false;
  57. }
  58. }
  59. </script>
  60. </div>
  61. <div class="col-md-4">
  62. </div>
  63. </div>
  64. </div>
  65. </body>
  66. </html>

session方式

index.php

  1. <?php
  2. session_start();
  3. // 判断是否已经登录?
  4. if (isset($_SESSION['user'])) $user = unserialize($_SESSION['user']);
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>管理系统</title>
  11. <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
  12. </head>
  13. <body>
  14. <div class="container">
  15. <div class="row">
  16. <div class="col-md-12">
  17. <!-- 顶部导航 -->
  18. <nav class="navbar navbar-default">
  19. <div class="container-fluid">
  20. <!-- Brand and toggle get grouped for better mobile display -->
  21. <div class="navbar-header">
  22. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
  23. <span class="sr-only">Toggle navigation</span>
  24. <span class="icon-bar"></span>
  25. <span class="icon-bar"></span>
  26. <span class="icon-bar"></span>
  27. </button>
  28. <a class="navbar-brand" href="index.php">管理系统</a>
  29. </div>
  30. <!-- Collect the nav links, forms, and other content for toggling -->
  31. <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
  32. <ul class="nav navbar-nav navbar-right">
  33. <!-- 根据cookie判断用户是否登录,显示不同的内容 -->
  34. <?php if (isset($user)) : ?>
  35. <li><a href="" id="logout"><span style="color:red"><?php echo $user['name']?></span>&nbsp;&nbsp;退出</a></li>
  36. <?php else: ?>
  37. <li><a href="login.php">登录</a></li>
  38. <?php endif ?>
  39. </ul>
  40. </div><!-- /.navbar-collapse -->
  41. </div><!-- /.container-fluid -->
  42. </nav>
  43. </div>
  44. </div>
  45. <script>
  46. // 为退出按钮创建事件监听器
  47. document.querySelector('#logout').addEventListener('click', function(event) {
  48. if (confirm('是否退出')) {
  49. // 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
  50. event.preventDefault();
  51. // 跳转到退出事件处理器
  52. window.location.assign('handle.php?action=logout');
  53. }
  54. });
  55. </script>
  56. </body>
  57. </html>

login.php

  1. <?php
  2. session_start();
  3. // 判断是否已登录
  4. if (isset($_SESSION['user']))
  5. exit('<script>alert("请不要重复登录");location.href="index.php";</script>');
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <title>管理系统</title>
  12. <!-- 使用load标签加载资源文件 -->
  13. <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
  14. </head>
  15. <body>
  16. <div class="container">
  17. <div class="row">
  18. <div class="col-md-4">
  19. </div>
  20. <div class="col-md-4">
  21. <div class="page-header text-center">
  22. <h3>管理系统登陆</h3>
  23. </div>
  24. <form class="form-horizontal"
  25. action="handle.php?action=login" method="post">
  26. <div class="form-group">
  27. <label for="inputEmail3" class="col-sm-2 control-label">邮箱:</label>
  28. <div class="col-sm-10">
  29. <input type="email" name="email" class="form-control" id="inputEmail3" placeholder="邮箱">
  30. </div>
  31. </div>
  32. <div class="form-group">
  33. <label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
  34. <div class="col-sm-10">
  35. <input type="password" name="password" class="form-control" id="inputPassword3" placeholder="密码">
  36. </div>
  37. </div>
  38. <div class="form-group">
  39. <div class="col-sm-offset-2 col-sm-10">
  40. <button type="submit" class="btn btn-primary btn-block">登录</button>
  41. </div>
  42. </div>
  43. </form>
  44. <div class="form-group">
  45. <label ><a href="register.php">还没有帐号, 注册一个吧</a></label>
  46. </div>
  47. </div>
  48. <div class="col-md-4">
  49. </div>
  50. </div>
  51. </div>
  52. </body>
  53. </html>

handle.php

  1. <?php
  2. session_start();
  3. $pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');
  4. $stmt = $pdo->prepare('SELECT * FROM `user`');
  5. $stmt->execute();
  6. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  7. //print_r( $users);
  8. $action = $_GET['action'];
  9. switch (strtolower($action))
  10. {
  11. //登陆
  12. case 'login':
  13. if($_SERVER['REQUEST_METHOD']='POST'){
  14. $email = $_POST['email'];
  15. $password = sha1($_POST['password']);
  16. $results = array_filter($users, function($user) use ($email, $password) {
  17. return $user['email'] === $email && $user['password'] === $password;
  18. });
  19. if(count($results) === 1){
  20. $_SESSION['user'] = serialize(array_pop($results));
  21. exit('<script>alert("验证通过");location.href="index.php"</script>');
  22. }else{
  23. exit('<script>alert("邮箱或密码错误,或者还没有帐号");location.href="login.php";</script>');
  24. }
  25. }else {
  26. die('请求类型错误');
  27. }
  28. break;
  29. //退出
  30. case 'logout':
  31. if (isset($_SESSION['user']))
  32. {
  33. session_destroy();
  34. exit('<script>alert("退出成功");location.assign("index.php")</script>');
  35. }
  36. break;
  37. case 'register':
  38. $name = $_POST['name'];
  39. $email = $_POST['email'];
  40. $password = sha1($_POST['p1']);
  41. $register_time = time();
  42. $sql = "INSERT `user` SET `name`='{$name}', `email`='{$email}', `password`='{$password}', `create_time`={$register_time}";
  43. $stmt = $pdo->prepare($sql);
  44. $stmt->execute();
  45. if ($stmt->rowCount() === 1) exit('<script>alert("注册成功");location.assign("login.php")</script>');
  46. else exit('<script>alert("注册失败");location.assign("register.php")</script>');
  47. break;
  48. // 未定义
  49. default:
  50. exit('未定义操作');
  51. }

register.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>管理系统</title>
  6. <!-- 使用load标签加载资源文件 -->
  7. <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div class="row">
  12. <div class="col-md-4">
  13. </div>
  14. <div class="col-md-4">
  15. <div class="page-header text-center">
  16. <h3>管理系统注册</h3>
  17. </div>
  18. <form class="form-horizontal"
  19. action="handle.php?action=register" method="post">
  20. <div class="form-group">
  21. <label for="inputEmail3" class="col-sm-2 control-label">用户:</label>
  22. <div class="col-sm-10">
  23. <input type="text" name="name" class="form-control" id="name" placeholder="用户名"  required>
  24. </div>
  25. </div>
  26. <div class="form-group">
  27. <label for="inputEmail3" class="col-sm-2 control-label">邮箱:</label>
  28. <div class="col-sm-10">
  29. <input type="email" name="email" class="form-control" id="inputEmail3" placeholder="邮箱"  required>
  30. </div>
  31. </div>
  32. <div class="form-group">
  33. <label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
  34. <div class="col-sm-10">
  35. <input type="password" name="password1" class="form-control" id="inputPassword3" placeholder="密码"  required>
  36. </div>
  37. </div>
  38. <div class="form-group">
  39. <label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
  40. <div class="col-sm-10">
  41. <input type="password" name="password2" class="form-control" id="inputPassword4" placeholder="必须与上面一致"  required>
  42. </div>
  43. </div>
  44. <div class="form-group">
  45. <div class="col-sm-offset-2 col-sm-10">
  46. <button type="submit" class="btn btn-primary btn-block">注册</button>
  47. </div>
  48. </div>
  49. </form>
  50. <a href="login.php">我有帐号,直接登录</a>
  51. <script>
  52. // 验证二次密码是否相等?
  53. function compare() {
  54. if (document.forms[0].p1.value.trim() !== document.forms[0].p2.value.trim()) {
  55. document.querySelector('#tips').innerText = '二次密码不相等';
  56. return false;
  57. }
  58. }
  59. </script>
  60. </div>
  61. <div class="col-md-4">
  62. </div>
  63. </div>
  64. </div>
  65. </body>
  66. </html>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:看来, 还是离开老师代码, 一句也写不出来, 全程抄写
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!