When the browser turns off cookie data, the website will not be able to use cookie transfer, but the URL parameter transfer can still be carried out (session). In fact, the overall session control of writing PHP is the same as the cookie session control
First create the php file to be used in writing
This step is the same as cookie. In fact, the session can also be passed through cookies. Based on the cookie, open the session at the beginning: session_start()
<!--?php session_start(); //判断:如果没登录自动跳转到登录页面 if(!$_SESSION[isLogin5]){ header(Location:login.php); }</pre--> 下面要注意的是,login.php 的跳转页面不能使用header 而只能通过 javascript 进行跳转
//跳转界面 echo '<script>';|r| echo location='index.php';|r| echo '</script>';
This is how session is passed through cookies. The following mainly explains how to pass url parameters
The first : Pass parameters through sid, that is, add "?sid="
after the link or formThis method can also use the PHPSESSID in the configuration file to replace the sid and achieve the same effect
login.php
<!--?php session_start(); echo session_id().<br-->; //跳转页面不能不是header if(isset($_POST[sub])){ include conn.inc.php; $sql=select id from users where name='{$_POST[name]}' and password='.md5($_POST[password]).'; $result=$mysqli->query($sql); //保存数据 if($result->num_rows > 0){ $row=$result->fetch_assoc(); $_SESSION[username]=$_POST[name]; $_SESSION[uid]=$_POST[uid]; $_SESSION[isLogin5]=1; //跳转界面 echo '<script>';|r| echo location='index.php?sid=.session_id().'; //将session_id() 调过来|r| echo '</script>'; } echo 用户名密码有误; } ?>
The logout process is not like a cookie, it has four steps: open, clear, delete and completely destroy
//开启session session_start(); //情况session值 $_SESSION=array(); //删除客户端的在cookie中的sessionid if(isset($_COOKIE[session_name()])){ setCookie(session_name(),'',time()-3600,'/'); //一定要写上第四个参数(路径) } //彻底销毁session session_destroy();
a, links or forms are followed by "?". This is similar to passing through sid, but SID is a constant
index.php:
>第二页 >第三页 >退出
//跳转界面 echo '<script>';|r| echo location='index.php?.SID.'; //SID 常量如果开启cookie则使用cookie,如果没开启就用session|r| echo '</script>';
用户名 | |
---|---|
密码 | |
The code is basically the same as that passed by the cookie, except that the session needs to be opened at the beginning: session_start();
Method: Change the value of session.use_trans_sid in the configuration file to 1
Function: Add the form of PHPSESSID to all links by default