Blogger Information
Blog 43
fans 1
comment 0
visits 33486
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP中的会话控制
蔚蓝世纪
Original
718 people have browsed it
1.建立首页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. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <link rel="stylesheet" type="text/css" href="../css/index.css">
  11. <title>首页</title>
  12. </head>
  13. <body>
  14. <nav>
  15. <a href="index.php">首页</a>
  16. <?php if (isset($user)) : ?>
  17. <a href="" id="logout"><span style="color:red"><?php echo $user['name']?></span>&nbsp;&nbsp;退出</a>
  18. <?php else: ?>
  19. <a href="">音乐</a>
  20. <a href="">舞蹈</a>
  21. <a href="">绘画</a>
  22. <a href="">雕塑</a>
  23. <a href="">书法</a>
  24. <a href="">戏曲</a>
  25. <a href="">相声</a>
  26. <a href="">小品</a>
  27. <a href="">设计</a>
  28. <a href="login.php">登录</a>
  29. <a href="register.php">注册</a>
  30. <?php endif ?>
  31. </nav>
  32. </body>
  33. <script>//(这一段为js代码)
  34. document.querySelector('#logout').addEventListener('click', function(event) {
  35. if (confirm('是否退出')) {
  36. event.preventDefault();
  37. window.location.assign('handle.php?action=logout');
  38. }
  39. });
  40. </script>
  41. </html>

输出效果:

2.建立用户注册register.php文件

代码举例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" type="text/css" href="../css/style.css">
  7. <title>注册用户</title>
  8. </head>
  9. <body>
  10. <h3>用户注册</h3>
  11. <form action="handle.php?action=register" method="post" onsubmit="return compare()">
  12. <div>
  13. <label for="name">昵称:</label>
  14. <input type="text" name="name" id="name" placeholder="不少于3个字符" required autofocus>
  15. </div>
  16. <div>
  17. <label for="email">邮箱:</label>
  18. <input type="email" name="email" id="email" placeholder="demo@email.com" required>
  19. </div>
  20. <div>
  21. <label for="p1">密码:</label>
  22. <input type="password" name="p1" id="p1" placeholder="不少于6位" required>
  23. </div>
  24. <div>
  25. <label for="p2">重复:</label>
  26. <input type="password" name="p2" id="p2" placeholder="必须与上面一致" required>
  27. </div>
  28. <div>
  29. <button>提交</button><span id="tips" style="color: red"></span>
  30. </div>
  31. </form>
  32. <a href="login.php">我有帐号,直接登录</a>
  33. <script>
  34. // 验证二次密码是否相等?
  35. function compare() {
  36. if (document.forms[0].p1.value.trim() !== document.forms[0].p2.value.trim()) {
  37. document.querySelector('#tips').innerText = '密码输入不一致';
  38. return false;
  39. }
  40. }
  41. </script>
  42. </body>
  43. </html>

输出效果:

3.建立用户登录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. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  11. <link rel="stylesheet" type="text/css" href="../css/style.css">
  12. <title>用户登录</title>
  13. </head>
  14. <body>
  15. <!-- 只需要验证用户的邮箱和密码就可以 -->
  16. <h3>用户登录</h3>
  17. <form action="handle.php?action=login" method="post">
  18. <div>
  19. <label for="email">邮箱:</label>
  20. <input type="email" name="email" id="email" placeholder="demo@email.com" require autofocus>
  21. </div>
  22. <div>
  23. <label for="password">密码:</label>
  24. <input type="password" name="password" id="password" placeholder="不少于6位" required>
  25. </div>
  26. <div>
  27. <button>提交</button>
  28. </div>
  29. </form>
  30. <a href="register.php">还没有帐号, 注册一个吧</a>
  31. </body>
  32. </html>

输出效果:

4.建立handle.php文件,用来连接数据库和首页index.php、注册register.php、登录login.php文件。

代码举例:

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

输出效果:

三、通过session实现PHP的会话控制

session会话控制与cookie的会话控制类似,但是需要在执行代码前先开启会话“session_start()”,其余代码与cookie类似,不作赘述。

四、总结

1.PHP session 可以在服务器上存储用户信息以便随后使用。
2.会话信息是临时的,在用户离开网站后将被删除。如果需要永久储存信息,可以把数据存储在数据库中。
3.删除session 数据,可以使用 unset() 或 session_destroy() 函数。
但是session_destroy() 会重置 session,您将失去所有已经存储的 session 数据。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:可以看出, 不论是基于cookie, 还是session, 都离不开cookie的支持
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!