PHP implementation of the same server with multiple two-level domain share session data

Source: Internet
Author: User

  Now many classified information sites will be divided into a number of two domain names, such as: sh.ganji.com (Shanghai net), su.ganji.com (Suzhou net) and so on, like this with more than two domain name of the site, how to achieve synchronous sharing session? , there is no discussion on multiple servers, just sharing the session with multiple level two domain name sites on the same server.

Before talking about this, let's take a look at how the PHP session works. When a client (such as a browser) logs on to a Web site, the accessed PHP page can use Session_Start () to open the session, which produces the client's unique identifier (this ID can be obtained/set through the function session_id ()). The session ID can be kept on the client in two ways, so that when a different page is requested, the PHP program can learn the client's session ID, either by automatically adding the session ID to the URL of Get, or by the POST's form, by default, the variable is named P Hpsessid; the other is to save the session ID in a cookie by using a cookie, which by default is named Phpsessid. Here we mainly in the COOKIE way to explain, because the application is more extensive. The data for the

session is saved in a file or database on the server side. By default, the session Save method set in php.ini is files (session.save_handler=files), which saves session data in a read-write file, while the session file saves a directory that is saved by the SESSION.SAVE_PATH specifies that the filename is prefixed with sess_, followed by the session ID, such as: sess_c72665af28a8b14c0fe11afe3b59b51b. The data in the file is the session data after serialization. If the visit is large, may produce the session file will be more, at this time can set the hierarchical directory to save the session file, the efficiency will be improved a lot, set the method is: session.save_path= "N;/save_path", N for graded series, save _path is the start directory. When the session data is written, PHP obtains the session_id of the client, and then finds the corresponding session file in the directory of the specified session file, which is not present, and then the data is serialized and then written to the file. Reading session data is similar to the operation process, to read out the data needs to be serialized, generate the corresponding session variables.

By understanding how the PHP session works, there are two ways we can solve the session sharing problem.

(1) Using Cookies to store session_id ();

Domain name one file PHP code:

<?php
session_start ();
Setcookie ("session_id", session_id (), Time () +3600*24*365*10, "/". Alixixi.com ");
$_session[' user_name ']= ' hello ';
echo $_session[' user_name '];
? >

Domain two file PHP code:

<?php
Session_destroy (); 
session_id ($_cookie[' session_id '));
Session_Start ();
echo $_session[' user_name '];
? >

(2) using PHP.ini's Session.cookie_domain

Note: Here is the use of PHP code to implement the configuration file does not need to modify php.ini, if necessary you can modify the configuration file in PHP.ini (in which case you do not need to change the code snippet: Ini_set ("Session.cookie_domain", ' Session.com ');

Domain name one file PHP code:

<?php
ini_set ("Session.cookie_domain", ' alixixi.com ');/NOTE: This parameter must be above Sesson_start (), otherwise it will not take effect
session_ Start ();
$_session[' user_name ']= ' Liang ';
echo $_session[' user_name '];
? >

Domain two file PHP code:

<?php
ini_set ("Session.cookie_domain", ' alixixi.com ');
Session_Start ();
echo $_session[' user_name '];
? >

Here are two things to look out for:

One: Ini_set ("Session.cookie_domain", ' session.com '), must be placed before session_start ();

Second: Some Web site testing must pay attention, maybe you have no problem, but on other machines or server problems, then you need to check Ini_set ("Session.cookie_domain", ' session.com '); The session.com in this must be a real match with your domain name! Otherwise your part of the session value will be a problem!

Articles that you may be interested in

    • Apache IP configuration multiple domain names, apache with an IP configuration of multiple virtual hosts
    • Use PHP functions in Smarty Templates and how to use multiple functions for a variable in smarty templates
    • How to see how many Web sites are hanging on the same server
    • How to realize the optimization of server, static, database optimization, load balancing to achieve high load in large flow website
    • MySQL server master-slave database synchronization configuration
    • CuteFTP solution to "Encounter invalid parameters" error when connecting to an FTP server
    • How to enhance Linux and UNIX server system security
    • Summarize MySQL database server gradually slow down reason and solution


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.