PHP Client session solution after cookie is disabled-PHP source code

Source: Internet
Author: User
The cookie itself is written on the client. However, if the client browser disables the cookie, the cookie cannot be used normally. In this case, we can use session instead, I hope to help you. The cookie itself is written on the client. However, if the client browser disables the cookie, the cookie cannot be used normally. In this case, we can use session instead, I hope to help you.

Script ec (2); script

By default, the session in PHP uses the Cookie of the client to save the session id. Therefore, when the cookie of the client fails, the session will be affected. It must be noted that the session does not necessarily depend on the cookie, which is also a bit better than the cookie. When the Cookie on the client is disabled or a problem occurs, PHP automatically attaches the session id to the URL, so that the session variable can be used across pages through the session id. However, this attachment also has certain conditions, that is, "session. use_trans_sid = 1 in php. ini or the -- enable-trans-sid option is enabled during compilation ".
Therefore, we can leave the cookie to use the session, that is, assume that the session is used when the user closes the cookie. There are several ways to implement the session:

Method 1: Add a PHPSESSID = $ sid on each hyperlink

The Code is as follows:
// Prevent new sessions from being generated when the initial Page is returned
If (isset ($ _ GET ["PHPSESSID"]) {
Session_id ($ _ GET ["PHPSESSID"]);
}
// Start a session
Session_start ();
// Obtain the session_id () of the current session ()
$ Sid = session_id ();
// Add the PHPSESSID = $ sid parameter to each link
Other pages can be obtained as follows:
If (isset ($ _ GET ["PHPSESSID"]) {
// Set the current session to the initial session, and the session_id () is consistent.
Session_id ($ _ GET ["PHPSESSID"])
}
Session_start ();

Method 2: Use the SID constant to replace PHPSESSID = $ sid on the Link (the SID value is similar to PHPSESSID = sddg34r593dfdlksrewr)

The Code is as follows:

If (isset ($ _ GET ["PHPSESSID"]) {
Session_id ($ _ GET ["PHPSESSID"]);
}
// Start a session
Session_start ();

Other pages can be obtained as follows:
If (isset ($ _ GET ["PHPSESSID"]) {
// Set the current session to the initial session, and the session_id () is consistent.
Session_id ($ _ GET ["PHPSESSID"])
}
Session_start ();

Method 3: Use session. use_trans_sid = 1, which is configured in php. ini.


1. Set session. use_trans_sid = 1 in php. ini or enable the -- enable-trans-sid option when compiling, so that PHP can automatically pass the session id across pages.
2. Manually pass session IDs through URL values and hidden forms.
3. Save session_id in the form of files and databases, and manually call it during the cross-page process.

The Code is as follows:

Index. php

Session_start ();
$ _ SESSION ['name'] = "Aseoe ";
$ Sn = session_id ();
$ Url = "." "index2.php? S = ". $ sn." "> next page ";
Echo $ url;
?>

Index2.php
$ _ GET ['s ']);
Session_start ();
Echo "the value of the passed session variable name is:". $ _ SESSION ['name'];
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.