Home > Article > Backend Development > Analysis of the implementation principle of Session ID in PHP
Session working mechanism:
Create a unique id (UID) for each visitor, and Variables are stored based on this UID. The UID is stored in a cookie or passed through the URL.
PHPSESSIONID production algorithm principle:
1. hash_func = md5 / sha1 #Can be configured by php.ini
2. PHPSESSIONID = hash_func (customer End IP Current time (seconds) Current time (microseconds) PHP's own random number generator)
From the content analysis of the data sampling value in the above hash_func(*), when multiple users are on the same server The probability of duplicate PHPSESSIONID produced is extremely low.
In addition, if a hacker wants to guess the PHPSESSIONID of a certain user, he must also know the "client IP, current time (seconds, microseconds), random number" and other data before he can simulate it.
php.ini configuration is as follows:
; Select a hash function for use in generating session ids. ; Possible Values ; 0 (MD5 128 bits) ; 1 (SHA-1 160 bits) ; This option may also be set to the name of any hash function supported by ; the hash extension. A list of available hashes is returned by the hash_algos() ; function. ; http://php.net/session.hash-function session.hash_function=0
PHP Session Working Principle
The following description uses cookies to transmit PHPSESSID.
1. The client requests a php server address.
2. The server receives the request, and this time the php script contains session_start()
.
3. The server will generate a PHPSESSID
. (The default session storage method is session.save_handler=files
, which is stored in file form. The generated session file name rule is sess_PHPSESSID
, and the session file exists session.save_path
in.)
4. Server response header Response Headers: Set-Cookie:PHPSESSID=37vjjasgjdv2ouk1uomhgqkv50; path=/
. Generate a cookie on the client to save this PHPSESSID
.
5. At this time, the client's cookie contains PHPSESSID
, and then the client's header for each request Request Headers
: Cookie:PHPSESSID=37vjjasgjdv2ouk1uomhgqkv50
. Every time the server receives a request from the client, it can find the session file of the server based on this PHPSESSID
. By reading and writing this session file, the super global variable attribute of the session is realized.
If the client disables cookies, since the cookie cannot be used to pass PHPSESSID
, then every time the client requests, the server will re-establish a session file and cannot pass PHPSESSID
To reuse the session file, the session will become invalid.
In this case, you can set session.use_trans_sid
to transmit PHPSESSID
. The difference between the specific implementation method and cookies is to pass PHPSESSID
through HTTPGET
Transfer. The PHPSESSID parameter "url?PHPSESSID=37vjjasgjdv2ouk1uomhgqkv50
" will be completed in each requested address.
PHPcli mode uses session through session_id()
You can use it to get the PHPSESSID of the current session, and you can also use it to set the current session PHPSESSID.
You can set this in PHPcli mode to achieve the purpose of using session, which is very convenient.
For example:
<?php // session_id('vingbrv8m64asth0nhplu9gmb7'); session_start(); $_SESSION[md5(rand(100,999))] = rand(100,999); var_dump($_SESSION);
Recommended tutorial: PHP video tutorial
php Chinese network learning topic:php session (including pictures, texts, videos, cases)
The above is the detailed content of Analysis of the implementation principle of Session ID in PHP. For more information, please follow other related articles on the PHP Chinese website!