亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 46
粉絲 0
評(píng)論 0
訪問量 46459
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
PHP實(shí)現(xiàn)用戶登錄、注冊(cè)、退出
上草一方
原創(chuàng)
1829人瀏覽過

注冊(cè)頁代碼

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>用戶注冊(cè)表單</title>
  8. </head>
  9. <body>
  10. <form action="handle.php?action=register" method="POST">
  11. <fieldset style="display: inline-block;background:lightcyan">
  12. <legend align="center">用戶注冊(cè)</legend>
  13. <p>
  14. <label for="name">用戶名:</label>
  15. <!-- require :規(guī)定必需在提交之前填寫輸入字段 -->
  16. <input type="txet" name="name" id="name" placeholder="請(qǐng)輸入用戶名" require>
  17. </p>
  18. <p>
  19. <label for="password">郵箱:</label>
  20. <input type="email" name="email" id="email" placeholder="請(qǐng)輸入郵箱" require>
  21. </p>
  22. <p>
  23. <label for="password">密碼:</label>
  24. <input type="password" name="password" id="pw" placeholder="請(qǐng)輸入密碼" require>
  25. </p>
  26. <p>
  27. <label for="password">確認(rèn)密碼:</label>
  28. <input type="password" name="repassword" id="repw" placeholder="請(qǐng)?jiān)俅屋斎朊艽a" require>
  29. </p>
  30. <p>
  31. <button>提交</button>
  32. </p>
  33. </fieldset>
  34. </form>
  35. <script>
  36. document.querySelector('button').addEventListener('click',function(event) {
  37. $name = document.querySelector('#name').value;
  38. $pwd = document.querySelector('#pw').value;
  39. $repwd = document.querySelector('#repw').value;
  40. console.log($name);
  41. console.log($pwd);
  42. console.log($repwd);
  43. if ($name === '') {
  44. event.preventDefault();
  45. alert("用戶名不能為空");
  46. return false;
  47. }
  48. if ($pwd === '') {
  49. event.preventDefault();
  50. alert("密碼不能為空");
  51. return false;
  52. }
  53. if ($repwd === '') {
  54. event.preventDefault();
  55. alert("請(qǐng)輸入確認(rèn)密碼");
  56. return false;
  57. }
  58. if ($name != '' && $pwd != $repwd) {
  59. event.preventDefault();
  60. alert("兩次輸入的密碼不一致");
  61. }
  62. })
  63. </script>
  64. </body>
  65. </html>

首頁代碼

  1. <?php
  2. session_start();
  3. // 判斷用戶是否已經(jīng)登錄?
  4. if(isset($_SESSION['user'])) $user = unserialize($_SESSION['user']);
  5. // print_r(user);
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <title>Document</title>
  14. </head>
  15. <body>
  16. <nav>
  17. <?php if (isset($user)): ?>
  18. <a href="" id="logout">退出</a>
  19. <?php else : ?>
  20. <a href="login.php">登錄</a>
  21. <?php endif ?>
  22. </nav>
  23. <script>
  24. let s = document.getElementById('logout');
  25. if (s) {
  26. document.querySelector('#logout').addEventListener('click', function(event) {
  27. if (confirm('是否退出?')){
  28. // 禁用默認(rèn)跳轉(zhuǎn)行為
  29. event.preventDefault();
  30. // 跳轉(zhuǎn)到處理器
  31. location.assign('handle.php?action=logout');
  32. }
  33. });
  34. }
  35. </script>
  36. </body>
  37. </html>

登錄頁代碼

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. // 判斷用戶是否已經(jīng)登錄?
  5. if (isset($_SESSION['user']))
  6. echo '<script>alert("不要重復(fù)登錄");location.href="index.php"</script>';
  7. ?>
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title>用戶登錄表單</title>
  13. </head>
  14. <body>
  15. <form action="handle.php?action=login" method="POST">
  16. <fieldset style="display:inline-block; background:lightcyan">
  17. <legend align="center">用戶登錄</legend>
  18. <p>
  19. <label for="name">用戶名:</label>
  20. <input type="text" name="name" id="name" placeholder="請(qǐng)輸入用戶名" require>
  21. </p>
  22. <p>
  23. <label for="email">郵箱:</label>
  24. <input type="email" name="email" id="email" placeholder="user@email.com" require>
  25. </p>
  26. <p>
  27. <label for="password">密碼:</label>
  28. <input type="password" name="password" placeholder="不少于6位" require>
  29. </p>
  30. <p>
  31. <button>提交</button>
  32. </p>
  33. </fieldset>
  34. <br>
  35. <a href="register.php">如果沒有帳號(hào),請(qǐng)先注冊(cè)</a>
  36. </form>
  37. </body>
  38. </html>

數(shù)據(jù)庫操作頁代碼

  1. <?php
  2. //開啟會(huì)話
  3. session_start();
  4. // 根據(jù)用戶的不同請(qǐng)求,執(zhí)行不同的操作
  5. // 比如:登錄,注冊(cè),退出
  6. // 連接數(shù)據(jù)并獲取用戶表中的數(shù)據(jù)
  7. $db = new PDO ('mysql:dbname=phpedu','root','root');
  8. $stmt = $db->prepare('SELECT * FROM `user`');
  9. $stmt->execute();
  10. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  11. // print_r($users);
  12. // * 查看是否跳轉(zhuǎn)頁面并獲取index里的表單action
  13. $action = $_GET['action'];
  14. // * strtolower() 將字符串轉(zhuǎn)化為小寫
  15. switch (strtolower($action)) {
  16. // 登錄
  17. case 'login':
  18. // 要保證數(shù)據(jù)是通過POST請(qǐng)求發(fā)送的
  19. // * REQUEST_METHOD:訪問頁面使用的請(qǐng)求方法($_SERVER:超全局變量)
  20. // echo $_SERVER['REQUEST_METHOD'];
  21. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  22. // var_dump($_SERVER['REQUEST_METHOD']);
  23. // 先拿到登錄數(shù)據(jù)
  24. // print_r($_POST)
  25. // * extract() - 從數(shù)組中將變量導(dǎo)入到當(dāng)前的符號(hào)表
  26. extract($_POST);
  27. // $email = $_POST['email];
  28. // $password = sha1($_POST['password']);
  29. // * array_filter()- 使用回調(diào)函數(shù)過濾掉數(shù)組的元素
  30. // $result 是數(shù)組
  31. $result = array_filter($users,function($users) use ($email,$password) {
  32. return $users['name'] === $name && $users['password'] === md5($password);
  33. });
  34. if (count($result) === 1) {
  35. // 驗(yàn)證成功,將用戶信息寫到SESSION
  36. // print_r(serialize(array_pop($result)));
  37. // $a = serialize(array_pop($result));
  38. // print_r(unserialize($a));
  39. // 將用戶信息序列化之后保存到SESSION中
  40. $_SESSION['user'] = serialize(array_pop($result));
  41. exit('<script>alert("驗(yàn)證通過");location.href="index.php"</script>');
  42. } else {
  43. exit('<script>alert("郵箱或密碼錯(cuò)誤");location.href="index.php"</script>');
  44. }
  45. } else {
  46. // var_dump($_SERVER['REQUEST_METHOD']);
  47. die('請(qǐng)求錯(cuò)誤');
  48. }
  49. break;
  50. // 退出
  51. case 'logout':
  52. if (isset($_SESSION['user'])) {
  53. session_destroy();
  54. // * location.assign:載入一個(gè)新文檔,瀏覽器的后退按鈕可用
  55. exit('<script>alert("退出成功");location.assign("index.php")</script>');
  56. }
  57. break;
  58. // 注冊(cè)
  59. case 'register':
  60. // 1. 獲取到新用戶的信息
  61. $name = $_POST['name'];
  62. $email = $_POST['email'];
  63. $password = md5($_POST['password']);
  64. $register_time = time();
  65. // 2. 將新用戶添加到表中
  66. $sql = 'INSERT `user` SET `name`=?,`email`=?,`password`=?,`register_time`=?';
  67. $stmt = $db->prepare($sql);
  68. $stmt->execute([$name,$email,$password,$register_time]);
  69. if ($stmt->rowCount() === 1){
  70. exit ('<script>alert("注冊(cè)成功");location.href="index.php"</script>');
  71. } else {
  72. exit('<script>alert("注冊(cè)失敗");location.href="index.php"</script>');
  73. }
  74. break;
  75. }

效果圖如下:

批改老師:PHPzPHPz

批改狀態(tài):合格

老師批語:
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)