博客列表 >pdo增删查改,session登陆验证

pdo增删查改,session登陆验证

张浩刚
张浩刚原创
2019年11月26日 14:26:46843浏览

session登陆验证

index.php

  1. <?php
  2. session_start();
  3. if(isset($_SESSION['name'])){
  4. echo $_SESSION['name'] . '欢迎您 ';
  5. echo '<a href="dispatch.php?action=logout">退出</a>';
  6. }else{
  7. echo '<a href="dispatch.php?action=login">登陆</a>';
  8. }

dispatch.php

  1. <?php
  2. session_start();
  3. include __DIR__ . '/pablic/pdo.php';
  4. $action = $_GET['action'] ?? 'login';
  5. $action = htmlentities(strtolower(trim($action)));
  6. switch($action){
  7. case 'login' : include __DIR__ . '/pablic/login.php';
  8. break;
  9. case 'check' : include __DIR__ . '/pablic/check.php';
  10. break;
  11. case 'logout' : include __DIR__ . '/pablic/logout.php';
  12. break;
  13. default : include __DIR__ . '../index.php';
  14. break;
  15. }

pdo.php

  1. <?php
  2. $db = [
  3. 'type' => 'mysql',
  4. 'host' => 'localhost',
  5. 'dbname' => 'film',
  6. 'user' => 'root',
  7. 'password' => 'root'
  8. ];
  9. $dns = $db['type'] . ':host=' . $db['host'] . ';dbname=' . $db['dbname'];
  10. try{
  11. $pdo = new PDO($dns,$db['user'],$db['password']);
  12. }catch(PDOException $e){
  13. die('错误原因' . print_r($e -> getMessage(),true));
  14. }

login.php

  1. <?php
  2. if(isset($_SESSION['name'])){
  3. echo $_SESSION['name'] . '重复登陆啦';
  4. }
  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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  12. <title>登陆</title>
  13. </head>
  14. <body>
  15. <h3>立即登陆</h3>
  16. <form action="../dispatch.php?action=check" method="post">
  17. <p>
  18. <label for="tel">账户:<input type="tel" name="tel" id=""></label>
  19. </p>
  20. <p>
  21. <label for="password">密码:<input type="password" name="password" id=""></label>
  22. </p>
  23. <p>
  24. <button>立即登陆</button>
  25. </p>
  26. </form>
  27. </body>
  28. </html>

check.php

  1. <?php
  2. if( !empty($_POST['tel']) && !empty(sha1($_POST['password'])) ){
  3. $tel = $_POST['tel'];
  4. $password = sha1($_POST['password']);
  5. $sql = 'SELECT * FROM `user` WHERE `tel`=:tel AND `password`=:password LIMIT 1 ';
  6. $stmt = $pdo -> prepare($sql);
  7. $stmt -> execute(['tel'=>$tel, 'password'=>$password]);
  8. $user = $stmt -> fetch(PDO::FETCH_ASSOC);
  9. if(empty($user)){
  10. echo '<script>alert("账户或密码错误");window.history.go(-1)</script>';
  11. }else{
  12. $_SESSION['name'] = $user['name'];
  13. echo '<script>alert("登录成功");location.assign("../index.php");</script>';
  14. exit;
  15. }
  16. }else{
  17. echo '账户密码不能为空';
  18. }

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("dispatch.php?action=login.php")</script>';
  7. }

PDO手抄


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