博客列表 >11月22日—COOKIE和SESSION介绍,SESSION实战

11月22日—COOKIE和SESSION介绍,SESSION实战

曾龙宇
曾龙宇原创
2019年11月25日 10:55:10758浏览

SESSION实现登录实例

conncet.php:数据库连接文件
  1. <?php
  2. //数据库连接参数
  3. $host = 'localhost';
  4. $dbname = 'demo';
  5. $username = 'root';
  6. $pwd = 'root';
  7. $dsn = "mysql:host=$host;dbname=$dbname";
  8. try{
  9. $pdo = new PDO($dsn,$username,$pwd);
  10. }catch(PDOException $e){
  11. die('连接失败:'.$e->getMessage());
  12. }
  13. ?>
login.php:登录界面
  1. <?php
  2. if (isset($_SESSION['name'])){
  3. echo '<script>alert("请不要重复登录");location.href="index.php";</script>';
  4. }
  5. ?>
  6. <!doctype html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta name="viewport"
  11. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  12. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  13. <title>用户登录</title>
  14. </head>
  15. <body>
  16. <h3>用户登录</h3>
  17. <form action="dispatch.php?action=check" method="post" onsubmit="return isEmpty(this)">
  18. <p>
  19. <label for="username">账号</label>
  20. <input type="text" name="username" id="username">
  21. </p>
  22. <p>
  23. <label for="pwd">密码</label>
  24. <input type="password" name="pwd" id="pwd">
  25. </p>
  26. <p>
  27. <button>提交</button>
  28. </p>
  29. </form>
  30. </body>
  31. <script>
  32. function isEmpty(form) {
  33. if (form.username.value == ''){
  34. alert('账号不能为空');
  35. form.username.focus();
  36. return false;
  37. }
  38. if (form.pwd.value == ''){
  39. alert('密码不能为空');
  40. form.pwd.focus();
  41. return false;
  42. }
  43. return true;
  44. }
  45. </script>
  46. </html>
dispatch.php:派发器,控制系统访问
  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. require '../connect.php';
  4. $action = isset($_GET['action'])?$_GET['action']:'';
  5. $action = strip_tags(strtolower(trim($action)));
  6. switch ($action){
  7. case 'login':
  8. include 'login.php';
  9. break;
  10. case 'check':
  11. include 'check.php';
  12. break;
  13. case 'logout':
  14. include 'logout.php';
  15. break;
  16. default:
  17. include 'index.php';
  18. break;
  19. }
index.php:首页
  1. <?php
  2. session_start();
  3. header('content-type:text/html;charset=utf-8');
  4. if (isset($_SESSION['name'])){
  5. echo '用户:'.$_SESSION['name'].'已登录<br>';
  6. echo '<a href="dispatch.php?action=logout">请退出</a>';
  7. }else{
  8. echo '<a href="dispatch.php?action=login">请登录</a>';
  9. }
check.php:验证登录功能
  1. <?php
  2. session_start();
  3. header('content-type:text/html;charset=utf-8');
  4. if ($_POST){
  5. $phone = trim($_POST['username']);
  6. $pwd = md5(trim($_POST['pwd']));
  7. $sql = 'SELECT * FROM `user` WHERE `phone`=:phone AND `pwd`=:pwd';
  8. $stmt = $pdo->prepare($sql);
  9. $stmt->execute(['phone'=>$phone,'pwd'=>$pwd]);
  10. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  11. if ($result){
  12. $_SESSION['name'] = $result['name'];
  13. echo '<script>alert("登录成功");location.href="index.php";</script>';
  14. }else{
  15. echo '<script>alert("登录失败");history.back();</script>';
  16. }
  17. }
logout.php:退出功能
  1. <?php
  2. session_start();
  3. header('content-type:text/html;charset=utf-8');
  4. if (isset($_SESSION['name'])){
  5. session_destroy();
  6. echo '<script>alert("退出成功");location.href="index.php";</script>';
  7. }else{
  8. echo '<script>alert("请先登录");location.href="login.php";</script>';
  9. }

手工抄写作业:



声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议