博客列表 >2019年11月22号:SESSION 登录实战

2019年11月22号:SESSION 登录实战

Rambo-Yang
Rambo-Yang原创
2019年11月25日 14:55:02379浏览

SESSION 登录实战

index.php

  1. <?php
  2. session_start();
  3. //print_r($_SESSION);
  4. if(isset($_SESSION['name'])){
  5. echo '用户'.$_SESSION['name'].'已登录';
  6. echo '<a href="dispatch.php?action=logout">退出登录</a>';
  7. }else {
  8. echo '<a href="dispatch.php?action=login">请登录</a>';
  9. }

login.php

  1. <?php
  2. session_start();
  3. if(isset($_SESSION['name'])){
  4. echo '<script>alert("你已登录,请不要重复登录");location.assign("index.php");</script>';
  5. }
  6. ?>
  7. <!doctype html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta name="viewport"
  12. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  13. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  14. <title>Document</title>
  15. </head>
  16. <body>
  17. <h2>用户登陆</h2>
  18. <form action="dispatch.php?action=check" method="post">
  19. <p>
  20. 用户名:<input type="text" name="name">
  21. </p><p>
  22. 密码:<input type="password" name="pwd">
  23. </p>
  24. <p>
  25. <input type="submit" value="提交">
  26. </p>
  27. </form>
  28. </body>
  29. </html>

dispatch.php

  1. <?php
  2. //开启session会话
  3. session_start();
  4. header("Content-type: text/html; charset=utf-8");
  5. require __DIR__ . '/db.php';
  6. $action = (isset($_GET['action'])) ? $_GET['action'] : login ;
  7. $action = htmlentities(strtolower(trim($action)));
  8. switch ($action){
  9. case 'login':
  10. header('location:login.php');
  11. break;
  12. case 'check':
  13. include __DIR__ . '/check.php';
  14. break;
  15. case 'logout':
  16. include __DIR__ . '/logout.php';
  17. break;
  18. default:
  19. header('location:index.php');
  20. }

check.php

  1. <?php
  2. //print_r($_SERVER);
  3. if ($_SERVER['REQUEST_METHOD'] === 'POST'){
  4. $name = $_POST['name'];
  5. $pwd = md5($_POST['pwd']);
  6. $sql = 'SELECT * FROM `user` WHERE `name` = :name AND `pwd` = :pwd LIMIT 1';
  7. $stmt = $pdo->prepare($sql);
  8. $stmt ->execute(['name' => $name,'pwd'=>$pwd]);
  9. $user = $stmt->fetch(PDO::FETCH_ASSOC);
  10. if(false === $user){
  11. echo '<script>alert("账号或密码错误");history.back();</script>';
  12. die();
  13. }
  14. // setcookie('name',$name);
  15. $_SESSION['name'] = $user['name'];
  16. echo '<script>alert("登录成功");location.assign("index.php");</script>';
  17. exit();
  18. }else{
  19. die("非法操作");
  20. }

logout.php

  1. <?php
  2. if(isset($_SESSION['name'])){
  3. session_destroy();
  4. echo '<script>alert("退出成功");location.assign("index.php");</script>';
  5. }else{
  6. echo '<script>alert("请先登录");location.assign("login.php");</script>';
  7. }

db.php

  1. <?php
  2. $db = [
  3. 'type' => 'mysql',
  4. 'host' => '127.0.0.1',
  5. 'dbname' => 'movies',
  6. 'username' => 'root',
  7. 'password' => 'root'
  8. ];
  9. $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
  10. try{
  11. $pdo = new PDO($dsn,$db['username'],$db['password']);
  12. }catch(PDOException $e){
  13. die('错误信息'.print_r($e->getMessage()));
  14. }



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