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

搜索
博主信息
博文 145
粉絲 7
評論 7
訪問量 198792
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
PHP會話(COOKIE和SESSION):注冊登陸實操案例
李東亞1??3????12?
原創(chuàng)
1788人瀏覽過

PHP會話基礎(chǔ)知識:

1.設(shè)置cookie值:setcookie("name","value",time()+時間,"路徑","域名");
2.讀取:$_COOKIE["name"]中的值
3.關(guān)閉(銷毀會話):通過過設(shè)置過期時間來
setcookie("name","value",time()-時間,"路徑","域名");

會話方式二SESSION

1.開啟會話:session_start();

  • 任何關(guān)于SESSION的操作都必須開啟會話

2.把信息寫入SESSION中:
$_SESSION["key"]="value";
3.讀取SESSION的中的值
$_SESSION["key"]
4.關(guān)閉(銷毀)會話

  • 銷毀SESSION中的某個值unset($_SESSION["key"]);
  • 銷毀SESSION中的所有值session_unset();
  • 銷毀SESSION文件:session_destroy();

注冊登陸案例實戰(zhàn)

(一)后臺登陸注冊以及退出操作

  1. <?php
  2. // echo json_encode($_POST);
  3. session_start();//開啟會話
  4. // echo json_encode($_GET["active"]);
  5. include('connection.php');//連接數(shù)據(jù)庫
  6. $active=$_GET["active"];//獲取操作方式(登陸?注冊?退出)
  7. // 判斷并執(zhí)行
  8. if($active=="register"){
  9. $sql="insert users (username,email,password) values (?,?,?);";
  10. $stmt=$pdo->prepare($sql);
  11. $stmt->execute([$_POST["username"],$_POST['email'],$_POST["password"]]);
  12. // $stmt->debugDumpParams();
  13. if($stmt->rowCount()>0){
  14. $_SESSION['name']=$_POST["username"];
  15. $_SESSION["username"]=$_POST['email'];
  16. $_SESSION["password"]=$_POST['password'];
  17. echo json_encode([$pdo->lastInsertId(),"注冊成功"]);
  18. }else{
  19. echo json_encode([0,"注冊失敗"]);
  20. }
  21. }elseif($active=="login"){
  22. $sql="select username,email,password from users where email=?";
  23. $stmt=$pdo->prepare($sql);
  24. $stmt->execute([$_POST["email"]]);
  25. $res=$stmt->fetch();
  26. if($res['email']==$_POST["email"]&&$res["password"]==$_POST["password"]){
  27. $_SESSION["name"]=$res['username'];
  28. $_SESSION["username"]=$_POST['email'];
  29. $_SESSION["password"]=$_POST['password'];
  30. echo json_encode([$res["username"],"登陸成功"]);
  31. }else{
  32. echo json_encode([0,"登陸失敗"]);
  33. }
  34. }elseif($active=="logout"){
  35. // session_destroy();
  36. unset($_SESSION["username"]);
  37. exit('<script>alert("你已經(jīng)退出!"); window.location.href="home.php";</script>');
  38. }else{
  39. echo "非法操作";
  40. }

(二)前端登陸和注冊

1.登陸

  1. <?php
  2. session_start();
  3. $username=$_SESSION["username"];
  4. if($username){
  5. exit('<script>alert("你已經(jīng)登陸!"); window.location.href="home.php";</script>');
  6. }
  7. ?>
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  15. <title>登陸</title>
  16. </head>
  17. <style>
  18. * {
  19. margin: 0;
  20. padding: 0;
  21. }
  22. form {
  23. width: 400px;
  24. margin: 0 auto;
  25. display: flex;
  26. flex-flow: column nowrap;
  27. align-items: center;
  28. }
  29. form * {
  30. width: 360px;
  31. height: 28px;
  32. margin-top: 5px;
  33. }
  34. </style>
  35. <body>
  36. <form action="" method="post" name="login">
  37. <h3>用戶登陸</h3>
  38. <input type="email" name="email" placeholder="請輸入郵箱賬號">
  39. <input type="password" name="pwd" placeholder="請輸入密碼">
  40. <button type="button">登陸</button>
  41. <div class="tips"></div>
  42. </form>
  43. </body>
  44. <script>
  45. $("[type='button']").click(function() {
  46. email = $("[name='email']").val().trim();
  47. pwd = $("[name='pwd']").val().trim();
  48. if (email.length > 0) {
  49. if (pwd.length > 0) {
  50. $.ajax({
  51. type:'POST',
  52. url:'http://php.edu/test/handle.php?active=login',
  53. data:{
  54. "email":email,
  55. "password":pwd
  56. },
  57. dataType:"json",
  58. success:function(res){
  59. if(res[0]) {
  60. $('.tips').append("<span>登陸成功,跳轉(zhuǎn)中……</span>");
  61. setTimeout(() => {
  62. window.location.href="home.php";
  63. }, 1000);
  64. }else{
  65. alert("賬號密碼錯誤?。。?);
  66. $('input').val('');
  67. }
  68. }
  69. });
  70. } else {
  71. alert("密碼不能為空");
  72. return false;
  73. }
  74. } else {
  75. alert("郵箱不能為空");
  76. return false;
  77. }
  78. })
  79. </script>
  80. </html>

2.注冊

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  8. <title>注冊</title>
  9. </head>
  10. <style>
  11. * {
  12. margin: 0;
  13. padding: 0;
  14. }
  15. form {
  16. width: 400px;
  17. margin: 0 auto;
  18. display: flex;
  19. flex-flow: column nowrap;
  20. align-items: center;
  21. }
  22. form * {
  23. width: 360px;
  24. height: 28px;
  25. margin-top: 5px;
  26. }
  27. </style>
  28. <body>
  29. <form action="" method="post" name="register">
  30. <h3>用戶注冊</h3>
  31. <input type="text" name="username" placeholder="用戶名">
  32. <input type="email" name="email" placeholder="XXXX@163.com">
  33. <input type="password" name="pwd1" placeholder="不少于6位">
  34. <input type="password" name="pwd2" placeholder="再次確認密碼">
  35. <button type="button">注冊</button>
  36. <div class="tips"></div>
  37. </form>
  38. </body>
  39. <script>
  40. $("[type='button']").click(function() {
  41. username = $("[name='username']").val().trim();
  42. email = $("[name='email']").val().trim();
  43. pwd1 = $("[name='pwd1']").val().trim();
  44. pwd2 = $("[name='pwd2']").val().trim();
  45. console.log(pwd1,pwd2);
  46. if (username.length > 0) {
  47. if (email.length > 0) {
  48. if (pwd1 == pwd2 && pwd1.length > 0) {
  49. $.ajax({
  50. type:'POST',
  51. url:'http://php.edu/test/handle.php?active=register',
  52. data:{
  53. "username":username,
  54. "email":email,
  55. "password":pwd1
  56. },
  57. dataType:"json",
  58. success:function(res){
  59. if(res[0]) {
  60. $('.tips').append("<span>登陸成功,跳轉(zhuǎn)中……</span>");
  61. setTimeout(() => {
  62. window.location.href="home.php";
  63. }, 1000);
  64. }else{
  65. alert("注冊失?。。。≌堉刈?);
  66. $('input').val('');
  67. }
  68. }
  69. });
  70. } else {
  71. alert("密碼不能為空,且兩密碼必須一致");
  72. return false;
  73. }
  74. } else {
  75. alert("郵箱不能為空");
  76. return false;
  77. }
  78. } else {
  79. alert("用戶名不能為空");
  80. return false;
  81. }
  82. })
  83. </script>
  84. </html>

(三)首頁判斷是否登陸并顯示信息

  1. <?php
  2. session_start();
  3. $user=$_SESSION["username"];
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  11. <title>首頁</title>
  12. </head>
  13. <style>
  14. *{
  15. margin:0;
  16. padding:0;
  17. }
  18. header{
  19. height:40px;
  20. background-color:lightgray;
  21. display:flex;
  22. justify-content: space-between;
  23. padding:0 20px;
  24. align-items:center;
  25. }
  26. header a {
  27. text-decoration: none;
  28. color:black;
  29. }
  30. </style>
  31. <body>
  32. <header>
  33. <a href="<?= $_SERVER["SCRIPT_NAME"] ?>">首頁</a>
  34. <div>
  35. <?php if($user): ?>
  36. <a href=""><?= $user?></a>
  37. <a href="http://php.edu/test/handle.php?active=logout">退出</a>
  38. <?php else:?>
  39. <a href="http://php.edu/test/login.php">登陸</a>
  40. <a href="http://php.edu/test/register.php">注冊</a>
  41. <?php endif ?>
  42. </div>
  43. </header>
  44. <?php
  45. include 'index.php';
  46. ?>
  47. </body>
  48. </html>

(四)運行效果

批改老師:天蓬老師天蓬老師

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

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

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

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