Home > Article > Backend Development > PHP session control
The content introduced in this article is PHP session control. Now I share it with everyone. Friends in need can refer to it
HTTP protocol is a protocol for WEB servers and browsers to communicate with each other. It is a stateless protocol, that is, each HTTP request is independent of each other. Therefore, the HTTP protocol does not have a built-in mechanism to maintain state between two transactions. For example, when a user requests one page and then requests another page, HTTP will not be able to tell us whether the two requests came from the same user.
In the website, we often need to track a variable: by tracking the variable, we can establish a connection between multiple request things, and then display different content and different pages based on authorization and user identity. This is session control technology.
Commonly used session control technologies include Cookie
and Session
. Simply put, Cookie determines the user's identity by recording information on the client side; Session determines the user's identity by recording information on the server side.
Cookie is a small text file that is included in the HTTP request message and passed between the web server and the browser. The working principle of Cookie is as follows:
The server sets a Set-Cookie
field in the HTTP response message and puts the Cookie data in Set- The Cookie
field is transmitted to the browser along with the HTTP message;
After the browser receives the HTTP response message, it checks Set-Cookie
If the field has a value, a cookie file will be created locally to save the data;
When the browser sends a request to the server again, the browser will first search the locally saved Cookie file. If there are any cookies related to the URL being connected in the Cookie file, it will be in HTTP Set a Cookie field in the request message, add the data in the Cookie file to the field, and finally send the HTTP request message carrying the Cookie field to the server.
Cookies can be used to save user names, passwords, personalized settings and other simple information. The following are instructions for using cookies:
<?phpsetcookie("Cookie", "cookievalue", time()+3600);
setcookie() must be called before the content of the HTML file is output
<?phpecho $_COOKIE["Cookie"];
<?php#方法一:将值设为空setcookie("Cookie", null);#方法二:将过期时间设为过去时间setcookie("Cookie", "value" , time());
Related learning recommendations: php cookie(Special topic)
Session is a method of maintaining user session data on the server side. Its working principle is as follows:
When the browser accesses the PHP script for the first time, the seesion_start()
function will create a unique Session ID (each client has a unique identifier) and automatically pass the HTTP response header, Save this Session ID to the client cookie. At the same time, a file named with Session ID is also created on the server side to save the user's session information;
When the same user visits this website again, it will automatically pass HTTP The request header brings the Seesion ID saved in the Cookie;
The server PHP script receives the client request, and then the session_start()
function will not Then assign a new Session ID, but search the server's hard disk for a Session file with the same name as the Session ID, and read out the session information previously saved for this user.
首先,创建 Session 唯一标识的方法有两种:通过 Cookie 或者 GET 方式。PHP 在默认情况下使用 Session 会建立一个名叫PHPSESSID
的 Cookie(可以通过 php.ini 修改 session.name 的值),如果客户端禁用cookie,可以指定通过 GET 方式把 Session ID 传到服务器(修改 php.ini 中 session.use_trans_sid
等参数)。其次,Session 是以文件的形式保存的。php.ini 中有个配置项 --session.save_path= ""
,这个里面填写的路径,将会保存所有 Session 文件。Session 文件的命名格式是:sess_[PHPSESSID的值]
。每一个文件,里面保存了一个会话的数据。最后,保存在 Session 文件中的数据是经过序列化处理的,比如:
cityID|i:0;cityName|s:3:"all";fanwe_lang|s:5:"zh-cn";fanwe_currency|a:4:{s:2:"id";s:1:"1";s:6:"name_1";s:9:"人民币";s:4:"unit";s:3:"¥";s:5:"radio";s:6:"1.0000";}_fanwe_hash__|s:32:"77c18770c6cb5d89444c407aaa3e8477";
Session 同样可以用来保存用户名、密码、个性化设置等一些简单的信息,以下是 Session 的使用说明:
//启动 sessionsession_start();//注册session变量,赋值为一个用户的名称$_SESSION["username"] = "jochen";//注册session变量,赋值为一个用户的ID$_SESSION["uid"] = 1;
注意:必须在 HTML 文件的内容输出之前调用 session_start()
<?phpsession_start();echo $_SESSION["username"]; # # jochenecho $_SESSION["uid"]; # 1
<?phpsession_start(); unset($_SESSION["username"]);unset($_SESSION["uid"]);
需要注意的是,当 session 文件比较多的情况下,将会产生 I/Q 读写性能问题。此时可以采用 memcached、redis 等缓存系统。
相关学习推荐:php session(专题)
相关推荐:
The above is the detailed content of PHP session control. For more information, please follow other related articles on the PHP Chinese website!