Session control in php_PHP tutorial
php session session control
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>';
Then change $_COOKIE[ ] to $_SESSION[ ]
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();
The second type does not need to be set to automatically choose whether to use cookies or sessions for delivery based on whether the browser has turned on the cookie data function.
a, links or forms are followed by "?". This is similar to passing through sid, but SID is a constant
index.php:
>第二页 >第三页 >退出
login.php:
//跳转界面 echo '<script>';|r| echo location='index.php?.SID.'; //SID 常量如果开启cookie则使用cookie,如果没开启就用session|r| echo '</script>';
User login
用户名 | |
---|---|
密码 | |
b, modify the php.ini configuration file
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
