Generally, the unique Session ID passed between each page is used, and the Session variable# saved by this user in the server is extracted through the Session ID. ##, to track a user. There are two main methods for transmitting session IDs.
The first method is to pass the Session ID based on Cookie. This method is more optimized, but it cannot be used frequently because the user can blockCookie
on the client. The second method is to pass it through URL parameters and embed the session ID directly into the URL.In the implementation of Session Usually a cookie-based approach is adopted, and the Session ID saved by the client is a Cookie. When the client disables cookies, the Session ID cannot be saved in the cookie and cannot be passed between pages. At this time, the Session becomes invalid. However, on theRelated topic recommendations: php session (including pictures, texts, videos, cases)
1. Pass Session ID through Cookie
If the client does not prohibit Cookie, proceed through session_start()The process of setting the Session ID to the cookie virtuallysetcookie(session_name(), session_id(), 0, '/')
session_id() function.
session.cookie_lifetime option in the php.ini file. The default is 0, which means that the Session ID will continue in the client's cookie until the browser is closed.
ession.cookie_path option in php.ini. The default is "/", which means that the path to be set in the cookie is valid in the entire domain.
2. Pass the Session ID through the URL
If the client browser supports Cookie, the Session ID is saved in the browser as a Cookie. However, if the user disables the use of cookies, the Session ID as a cookie does not exist in the browser, so the cookie information is not included in the client request. If the Session ID as a Cookie cannot be obtained from the client browser when the session_start() function is called, a new Session ID will be created, and the user status cannot be tracked. Therefore, every time the user requests a PHP script that supports Session, the session_start() function will create a new Session when opening the Session, thus losing the ability to track the user's status. Another mechanism for tracking Session is provided in PHP. If the client browser does not support Cookie, PHP can rewrite the client request URL and add the Session ID to the URL. middle. You can manually add a Session ID to the URL of each hyperlink. This method requires a lot of work and is generally not recommended. The code of the example is as follows:<?php //开启 session session_start(); // 在 URL 后面附加参数,变量名为session_name()获取的名称,值为session_id()获取 echo '<a href="test.php?'.session_name().'='.session_id().'">演示</a>'; ?>
Related learning recommendations:
The above is the detailed content of How to pass Session ID in php. For more information, please follow other related articles on the PHP Chinese website!