Several ways to implement cross-domain in PHP session

Source: Internet
Author: User

The session is mainly divided into two parts:

One is the session data, which is stored by default under the server's TMP file and is in the form of a file.

Another is marked session data session Id,session ID, is that session file file name, session Id is randomly generated, so can ensure uniqueness and randomness, to ensure the security session. Typically, if the session's lifetime is not set, the session ID is stored in memory, the ID is automatically logged off after the browser is closed, and the session ID is re-register after the page is again requested. If the client does not disable the cookie, the cookie plays the role of storing the session ID and duration of the sessions at the time of the start.

Two different domain name Web site, want to use the same session, is involved to the session cross domain problem!

By default, each server generates SESSIONID for the same client, such as for the same user browser, the session ID generated by server A is 11111111111, and the B server generates 222222. In addition, the PHP session data are stored separately in the file system of the server. To share session data, you have to achieve two goals:

One is that each server must have the same session ID for the same client, and it can be passed by the same cookie, which means that each server must be able to read the same cookie named Phpsessid, and the other is how the session data is stored. Location must be guaranteed to be accessible to each server. These two goals are simply the session IDs of the shared clients of multiple servers (A, b servers), and the server-side session data must also be shared.

There are three ways to solve this problem:

1. The following settings are provided at the beginning of the PHP page (before any output, and before Session_Start ())

Ini_set (' Session.cookie_path ', '/');
Ini_set (' Session.cookie_domain ', '. mydomain.com ');
Ini_set (' Session.cookie_lifetime ', ' 1800 ');

2. Set in php.ini

Session.cookie_path =/
Session.cookie_domain =. mydomain.com
Session.cookie_lifetime = 1800

3. Invoke function at the beginning of PHP page (condition same as 1)

Session_set_cookie_params (1800, '/', '. mydomain.com ');

The session has a session_id as the only sign of the session.

To implement the session subdomain, the session is actually the same for accessing two A and B subdomains in the same browser.

Since the session is stored on the server side, how can two servers recognize that the two requests are sent by a browser?

Cookies are stored on the client, and the server usually identifies different clients through cookies, so you can use cookies to save the session_id and set the cookie to the parent domain.

For example, when a.sso.com is accessed, the session_id is saved in a cookie. When the b.sso.com is accessed, the session_id is removed from the cookie.

and get the session through session_id to a persistent container.

For example, when a.sso.com is accessed, the session_id is saved in a cookie. When the b.sso.com is accessed, the session_id is removed from the cookie.

and get the session through session_id to a persistent container.

In this experiment, PHP is used as the experimental language.

When you access a.sso.com, you will pass the

Session_Start ();
$_session[' person '] = "Sbsbsbs";
$session _id = session_id ();
Setcookie (' name ', $session _id,time () +3600*24, '/', ' sso.com ');

Save the session_id in a cookie.

Because in PHP, the session is an array, PHP has the serialize () function to serialize the array

$session _value = serialize ($_session);

The $session_value is then saved in the database.

When b.sso.com is accessed, the session_id is fetched from the cookie, and then the serialized session is fetched in the database according to session_id

The session can then be manipulated to implement the session across the subdomain.


Because the session is saved in the database, access is a time-consuming operation, so you can save the session in the cache, such as memcached or Redis,

This allows access to the session to be more rapid.

There is also the advantage of using caching is that the session has a certain survival time, if there is a database, you need to save the session's survival time, when the session is taken out, but also to determine whether it is invalid.

The

Use the cache to store the session can set its survival time at the time of storage, reducing the failure judgment after the removal of the process.

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.