前端小白之每天学习记录----php(7)session与cookie
服务器端的会话技术(通讯协议)
坐地铁:
深圳通
拿手机刷
cookie( 保存在 客户端的浏览器里面的数据 )
session( 保存在 web服务器里面的数据 )
如何使用cookie?
1.设置/注销cookie(新建cookie.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php /* array( 'user' => 'ghostwu' ) */ // function setCookie(c_name,value,expiredays) // 变量名,值,过期天数 setcookie( "user" , "zhangsan" ); //设置成功 if ( isset( $_GET [ 'act' ] ) && $_GET [ 'act' ] == 'logout' ){ //time(): 获取到当前的时间戳 setcookie( "user" , "" , time() - 1 ); //清空user的值,并使它过期 } ?> <a href= "?act=logout" >注销</a> |
2.取出cookie(新建cookie2.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php /* php里面的所有cookie都是存储在超级全局变量 $_COOKIE 里面 $_COOKIE是储存在浏览器的类数组 */ // print_r( $_COOKIE ); // echo $_COOKIE['user']; if ( ! empty ( $_COOKIE [ 'user' ] ) ){ echo $_COOKIE [ 'user' ]; } ?> |
3.一个设置cookie的登录页面(新建login.php)
登录成功时候用cookie保存用户名与密码
setcookie( "name", $userName ); /
setcookie( "pwd", $userPwd );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <meta http-equiv= "X-UA-Compatible" content= "ie=edge" > <title>Document</title> <link href= "https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel= "stylesheet" > <style> .reg-form { margin-top: 100px; } </style> </head> <body> <!-- 链接数据库 --> <?php require ( './mysql.class.php' ); //链接数据库的类文件,请参考本系列上一节 ?> <!-- html文本 --> <div class = "container" > <div class = "row" > <div class = "tip" ></div> <form class = "form-horizontal reg-form" role= "form" method= "post" > <div class = "form-group" > <label for = "firstname" class = "col-md-2 control-label" >用户名:</label> <div class = "col-md-6" > <input type= "text" class = "form-control" id= "user" name= "user" placeholder= "请输入用户名" > </div> </div> <div class = "form-group" > <label for = "lastname" class = "col-md-2 control-label" >密码</label> <div class = "col-md-6" > <input type= "password" name= "pwd" class = "form-control" id= "pwd" placeholder= "请输入密码" > </div> </div> <div class = "form-group" > <div class = "col-sm-offset-2 col-sm-10" > <button type= "submit" class = "btn btn-primary" >登录</button> </div> </div> </form> </div> </div> <!-- PHP文本 --> <?php if ( ! empty ( $_POST [ 'user' ] ) && ! empty ( $_POST [ 'pwd' ] ) ){ //判断用户名密码非空 $userName = $_POST [ 'user' ]; $userPwd = $_POST [ 'pwd' ]; $sql = "SELECT * FROM user_info WHERE user_name = '$userName' AND user_pwd = '$userPwd'" ; $res = mysql_query( $sql ); //查询用户名密码 if ( $res && mysql_num_rows( $res ) ){ //如果查询成功,并且账号密码正确 setcookie( "name" , $userName ); //设置cookie setcookie( "pwd" , $userPwd ); header( "Location:success.php" ); //设置完跳转到success.php页面 ?> <!-- <script> window.location.href = './success.html' ; </script> --> <?php } else { //登录不成功,重新加载页面 header( "Location:login.php" ); ?> <!-- <script> window.location.href = './error.html' ; </script> --> <?php } } ?> </body> </html> |
4.使用保存的cookie --->登录成功页面(新建success.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <meta http-equiv= "X-UA-Compatible" content= "ie=edge" > <title>Document</title> </head> <body> <?php if ( empty ( $_COOKIE [ 'name' ] ) ){ //是否有用户名 header( "Location:login.php" ); // 没有用户名则跳到登录页面 } //这里要添加一条判断 数据库查询用户名与密码是否正确 //判断方法与登录页面相同,如果用户名密码错误,跳去登录页面 //点击注销页面:使cookie过期 if ( isset( $_GET [ 'act' ] ) && $_GET [ 'act' ] == 'logout' ){ setcookie( 'name' , '' , time() - 1 ); //注销cookie header( "Location:login.php" ); //注销后跳转到登录页面 } ?> <h3>欢迎您 <!-- 把cookie保存的用户名取出来 --> <?php echo ! empty ( $_COOKIE [ 'name' ]) ? $_COOKIE [ 'name' ] : '' ; ?> !登录</h3> | <!-- 注销按钮 --> <a href= "?act=logout" >注销</a> </body> </html> |
如何使用session?
1.设置/注销session(新建session.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php /* 要使用session, 必须先要用session_start() 开启session web服务器上会创建一个类数组$_SESSION */ session_start(); $_SESSION [ 'name' ] = 'zhangsan' ; if ( isset( $_GET [ 'act' ] ) && $_GET [ 'act' ] == 'logout' ){ //删除某个数组的一项,或者删除一个变量 unset( $_SESSION [ 'name' ] ); } ?> <a href= "?act=logout" >注销</a> |
2.取出session(新建session2.php)
1 2 3 4 5 6 | <?php session_start(); if ( ! empty ( $_SESSION [ 'name' ] ) ){ echo $_SESSION [ 'name' ]; } ?> |
3.一个设置session的登录页面(新建login2.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <meta http-equiv= "X-UA-Compatible" content= "ie=edge" > <title>Document</title> <link href= "https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel= "stylesheet" > <style> .reg-form { margin-top: 100px; } </style> </head> <body> <?php session_start(); require ( './mysql.class.php' ); ?> <div class = "container" > <div class = "row" > <div class = "tip" ></div> <form class = "form-horizontal reg-form" role= "form" method= "post" > <div class = "form-group" > <label for = "firstname" class = "col-md-2 control-label" >用户名:</label> <div class = "col-md-6" > <input type= "text" class = "form-control" id= "user" name= "user" placeholder= "请输入用户名" > </div> </div> <div class = "form-group" > <label for = "lastname" class = "col-md-2 control-label" >密码</label> <div class = "col-md-6" > <input type= "password" name= "pwd" class = "form-control" id= "pwd" placeholder= "请输入密码" > </div> </div> <div class = "form-group" > <div class = "col-sm-offset-2 col-sm-10" > <button type= "submit" class = "btn btn-primary" >登录</button> </div> </div> </form> </div> </div> <?php if ( ! empty ( $_POST [ 'user' ] ) && ! empty ( $_POST [ 'pwd' ] ) ){ $userName = $_POST [ 'user' ]; $userPwd = $_POST [ 'pwd' ]; $sql = "SELECT * FROM user_info WHERE user_name = '$userName' AND user_pwd = '$userPwd'" ; $res = mysql_query( $sql ); if ( $res && mysql_num_rows( $res ) ){ $_SESSION [ 'name' ] = $userName ; $_SESSION [ 'pwd' ] = $userPwd ; header( "Location:success2.php" ); ?> <!-- <script> window.location.href = './success.html' ; </script> --> <?php } else { header( "Location:login2.php" ); ?> <!-- <script> window.location.href = './error.html' ; </script> --> <?php } } ?> </body> </html> |
4.使用保存的session --->登录成功页面(新建succeed2.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <meta http-equiv= "X-UA-Compatible" content= "ie=edge" > <title>Document</title> </head> <body> <?php session_start(); //开启session require ( "./mysql.class.php" ); //链接数据库的类文件,请参考本系列上一节 ?> <?php if ( empty ( $_SESSION [ 'name' ] ) ){ //判断用户名是否为空 header( "Location:login2.php" ); } else if ( ! empty ( $_SESSION [ 'name' ] ) ){ //用户名非空 $sql = "SELECT * FROM user_info WHERE user_name = '{$_SESSION['name']}' AND user_pwd = '{$_SESSION['pwd']}'" ; $res = $mysql ->query( $sql ); if ( !( $res && mysql_num_rows( $res ) ) ){ //用户名密码正确 header( "Location:login2.php" ); } } if ( isset( $_GET [ 'act' ] ) && $_GET [ 'act' ] == 'logout' ){ //是否注销 unset( $_SESSION [ 'name' ] ); //移除用户名 header( "Location:login2.php" ); } ?> <h3>欢迎您<?php echo ! empty ( $_SESSION [ 'name' ] ) ? $_SESSION [ 'name' ] : '' ; ?>! 登录会员中心</h3> <a href= "?act=logout" >注销</a> </body> </html> |
5.在注册页面注册成功的时候保存session --->注册页面(新建reg.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <meta http-equiv= "X-UA-Compatible" content= "ie=edge" > <title>Document</title> <link href= "https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel= "stylesheet" > <style> .reg-form { margin-top: 100px; } </style> </head> <body> <?php session_start(); require ( './mysql.class.php' ); ?> <div class = "container" > <div class = "row" > <div class = "tip" ></div> <form class = "form-horizontal reg-form" role= "form" method= "post" > <div class = "form-group" > <label for = "firstname" class = "col-md-2 control-label" >用户名:</label> <div class = "col-md-6" > <input type= "text" class = "form-control" id= "user" name= "user" placeholder= "请输入用户名" > </div> </div> <div class = "form-group" > <label for = "lastname" class = "col-md-2 control-label" >密码</label> <div class = "col-md-6" > <input type= "password" name= "pwd" class = "form-control" id= "pwd" placeholder= "请输入密码" > </div> </div> <div class = "form-group" > <div class = "col-sm-offset-2 col-sm-10" > <button type= "submit" class = "btn btn-primary" >注册</button> </div> </div> </form> </div> </div> <?php if ( ! empty ( $_POST [ 'user' ] ) && ! empty ( $_POST [ 'pwd' ] ) ){ $userName = $_POST [ 'user' ]; $userPwd = $_POST [ 'pwd' ]; $sql = 'SELECT * FROM user_info WHERE user_name = "' . $userName . '"' ; $res = mysql_query( $sql ); if ( $res && mysql_num_rows( $res ) ){ //用户名存在 ?> <script> document.querySelector( ".tip" ).innerHTML = '你输入的用户名已经存在' ; </script> <?php } else { //用户名, 开始注册了 $sql = "INSERT INTO user_info ( user_name, user_pwd ) VALUES( '$userName', '$userPwd' )" ; $res = mysql_query( $sql ); if ( $res !== false ){ $_SESSION [ 'name' ] = $userName ; $_SESSION [ 'pwd' ] = $userPwd ; ?> <script> document.querySelector( ".tip" ).innerHTML = '用户名注册成功' ; </script> <?php } else { ?> <script> document.querySelector( ".tip" ).innerHTML = '用户名注册失败' ; </script> <?php } } } ?> </body> </html> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· .NET Core GC压缩(compact_phase)底层原理浅谈
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能
· 语音处理 开源项目 EchoSharp