博客列表 >SESSION登录实战及PDO操作-第九期(191122作业)

SESSION登录实战及PDO操作-第九期(191122作业)

feng
feng原创
2019年11月27日 16:28:31884浏览

SESSION登录实战及PDO操作

check.php源码

  1. <?php
  2. session_start();
  3. //echo 'check';die;
  4. //var_dump($_POST);die;
  5. if (isset($_POST['phone']) && isset($_POST['password'])){
  6. $phone=$_POST['phone']?$_POST['phone']:'';
  7. $password=md5($_POST['password']?$_POST['password']:'');
  8. $sql='SELECT `phone`,`pwd`,`name` FROM `user` WHERE `phone`=:phone and pwd=:password LIMIT 1';
  9. $stsm=$pdo->prepare($sql);
  10. $stsm->execute(['phone'=>$phone,'password'=>$password]);
  11. $user=$stsm->fetch(PDO::FETCH_ASSOC);
  12. // var_dump($user);die;
  13. if ($phone===$user['phone'] && $password===$user['pwd']){
  14. // setcookie('name',$user['name'],time()+3600);
  15. $_SESSION['name']=$user['name'];
  16. header('location:index.php');
  17. }else{
  18. echo '<script>alert("验证失败");history.back();</script>';
  19. }
  20. }else{
  21. echo '<script>alert("请求类型错误")</script>';
  22. header('location:index.php');
  23. }

check.php源码

  1. <?php
  2. $db=[
  3. 'type'=>'mysql',
  4. 'host'=>'localhost',
  5. 'dbname'=>'emshop',
  6. 'username'=>'root',
  7. 'password'=>'123456'
  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('连接失败,错误信息是:'.$e->getMessage());
  14. }
  15. //var_dump($pdo);

dispatch源码

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

index.php源码

  1. <?php
  2. session_start();
  3. if (isset($_SESSION['name'])){
  4. echo '用户名:'.$_SESSION['name'];
  5. echo "<br><a href='dispatch.php?action=logout'>点击退出</a>";
  6. }else{
  7. echo "<a href='dispatch.php?action=login'>用户未登录,点击登陆</a>";
  8. }

login.php源码

  1. <?php
  2. session_start();
  3. //setcookie('name','',time()-1);
  4. if (isset($_SESSION['name']) && !empty($_SESSION['name'])){
  5. echo '<script>alert("用户已登陆")</script>';
  6. header('location:index.php');
  7. die;
  8. }
  9. ?>
  10. <!doctype html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta name="viewport"
  15. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  16. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  17. <title>Document</title>
  18. </head>
  19. <body>
  20. <h3>用户登录</h3>
  21. <form action="dispatch.php?action=check" method="post" onsubmit="return isEmpty();">
  22. <p>
  23. <label for="phone">手机号:</label>
  24. <input type="phone" name="phone" id="phone">
  25. </p>
  26. <p>
  27. <label for="password">密码:</label>
  28. <input type="password" name="password" id="password">
  29. </p>
  30. <p>
  31. <button>提交</button>
  32. </p>
  33. </form>
  34. <script>
  35. function isEmpty() {
  36. var phone = document.getElementById('phone').value;
  37. var password = document.getElementById('password').value;
  38. if (phone.length=== 0 || password.length===0) {
  39. alert('手机和密码不能为空');
  40. return false;
  41. }
  42. }
  43. </script>
  44. </body>
  45. </html>

logout源码

  1. <?php
  2. session_start();
  3. if(!isset($_SESSION['name'])){
  4. header('location:dispatch.php?action=index');
  5. die;
  6. }
  7. //setcookie('name',NULL,time()-1);
  8. session_destroy();
  9. setcookie('PHPSESSID','',time()-3600,'/');
  10. header('location:dispatch.php?action=index');
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议