Second-level domain name session sharing solution)

Source: Internet
Author: User

1. Use cookies to store session_id ();

Example: PHP code for a domain name file:
<? Phpsession_start (); setcookie ("session_id", session_id (), time () + 3600*24*365*10, "/", ".session.com "); $ _ session ['user _ name'] = 'liangshan Limin '; echo $ _ session ['user _ name'];?>

PHP code for domain name 2 file:

<?phpsession_destroy(); session_id($_COOKIE['session_id']);session_start();echo $_SESSION['user_name'];?>
2. use PHP. INI session. cookie_domain Note: you do not need to modify the PHP code here. ini configuration file; if necessary, you can. modify the configuration file in ini (in this case, you do not need to change the segment code: ini_set ("session. cookie_domain ", 'session. com ');) instance: domain name 1 file PHP code:
<? Phpini_set ("session. cookie_domain ", 'session. com '); // note: this parameter must be on sesson_start (); otherwise, session_start () will not take effect (); $ _ session ['user _ name'] = 'liangshan Limin '; echo $ _ session ['user _ name'];?>
PHP code for domain name 2 file:
<?phpini_set("session.cookie_domain",'session.com');session_start();echo $_SESSION['user_name'];?>

In the future, we need to explain some problems!

1: ini_set ("session. cookie_domain", 'session. com '); it must be placed before session_start;

Second: You must note when testing some websites. Maybe you have no problem locally, but there is a problem on another machine or server, you need to check ha, ini_set ("session. cookie_domain ", 'session. com '); session.com must match your domain name! Otherwise, some of your session values will be faulty!

From: http://www.cnblogs.com/linzhenjie/archive/2013/01/23/2872541.html

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.