欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PHP会话(COOKIE和SESSION):注册登陆实操案例

程序员文章站 2022-03-01 17:28:02
...

PHP会话基础知识:

1.设置cookie值:setcookie("name","value",time()+时间,"路径","域名");
2.读取:$_COOKIE["name"]中的值
3.关闭(销毁会话):通过过设置过期时间来
setcookie("name","value",time()-时间,"路径","域名");

会话方式二SESSION

1.开启会话:session_start();

  • 任何关于SESSION的操作都必须开启会话

2.把信息写入SESSION中:
$_SESSION["key"]="value";
3.读取SESSION的中的值
$_SESSION["key"]
4.关闭(销毁)会话

  • 销毁SESSION中的某个值unset($_SESSION["key"]);
  • 销毁SESSION中的所有值session_unset();
  • 销毁SESSION文件:session_destroy();

注册登陆案例实战

(一)后台登陆注册以及退出操作

  1. <?php
  2. // echo json_encode($_POST);
  3. session_start();//开启会话
  4. // echo json_encode($_GET["active"]);
  5. include('connection.php');//连接数据库
  6. $active=$_GET["active"];//获取操作方式(登陆?注册?退出)
  7. // 判断并执行
  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("你已经退出!"); 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("你已经登陆!"); 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>登陆成功,跳转中……</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>登陆成功,跳转中……</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>

(四)运行效果

PHP会话(COOKIE和SESSION):注册登陆实操案例