Php cross-domain, cross-domain, cross-server session read method introduction

Source: Internet
Author: User
Php cross-domain, cross-domain, cross-server session read method introduction

  1. Ini_set ('session. cookie_path ','/');
  2. Ini_set ('session. cookie_domain ',' .mydomain.com ');
  3. Ini_set ('session. cookie_lifetime ', '123 ');

2. set in php. ini

  1. Session. cookie_path =/

  2. Session. cookie_domain = .mydomain.com

  3. Session. cookie_lifetime = 1800

3. call the function at the beginning of the php page (condition 1)

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

These three methods have the same effect.

Here I use the first method to set the domain names in www.mydomain.com and sub.mydomain.com respectively. Sub1.php

  1. // Configure the first accessed page

  2. Ini_set ('session. cookie_path ','/');
  3. Ini_set ('session. cookie_domain ',' .mydomain.com ');
  4. Ini_set ('session. cookie_lifetime ', '123 ');

  5. //

  6. Session_set_cookie_params (1800, '/', '.mydomain.com ');
  7. Session_start ();
  8. $ _ SESSION ['job1'] = 'job1 ';
  9. Print_r ($ _ SESSION );
  10. ?>

Sub2.php

  1. Session_set_cookie_params (1800, '/', '.mydomain.com ');
  2. Session_start ();
  3. $ _ SESSION ['Sub2'] = 'Sub2 ';
  4. Print_r ($ _ SESSION );
  5. ?>

Access sequence: (1) www.mydomain.com/sub1.php page output: Array ([sub1] => sub1)

(2) sub.mydomain.com/sub2.php page output: Array ([sub1] => sub1 [sub2] => sub2)

Successful

The implementation of the second goal can use the database to save SESSION data, so that each server can easily access the same data source and obtain the same SESSION data; or through file sharing, for example, in NFS mode (how to configure nfs in other articles), If session data is stored in a database, there may be a legacy problem, that is, if the website has a large traffic volume, the SESSION reads and writes the database frequently. you can put this in memcache. It is implemented in the previous article in the database. The idea of combining databases with memcache is now available. If it is not good to use memcache to store Sessions separately, it is best to work with the database.

2) Cross-Origin solution: iframe is used, but ff is not supported. Therefore, you need to add the p3p protocol.

P3P (Platform for Privacy Preferences Project) is a protocol through which it declares that it is a good person and allows the collection of browser user behavior. But in reality, everyone can say that they are good people, and they may not be doing anything bad in the back. This is where their differences are. [Reference] most websites in China do not pay attention to this P3P. Privacy issues may not be valued abroad (Microsoft's privacy statement.

The first thought is to use JS to operate the Cookie and allow the cookies of two different domains to access each other, so that the above results can be achieved.

The specific implementation process is as follows: 1. after successfully logging on to system A, use JS to dynamically create A hidden iframe, use the src attribute of iframe to redirect the cookie value in domain A to System B as the get parameter. jsp page;

  1. Var _ frm = document. createElement ("iframe ");
  2. _ Frm. style. display = "none ";
  3. _ Frm. src = "http://bbs.it-home.org/setcookie.php? Mycookie = xxxxx "; // The best encoding for xxx here
  4. Document. body. appendChild (_ frm );

2. setcookie in System B. on the php page, obtain the cookie value passed in system A and write the obtained value to the user's cookie. of course, the domain is your own, in this way, the cross-origin access of cookies is implemented. However, you must note that this operation cannot be successful in IE, and you need to perform this operation in setocokie. php page to set P3P HTTP Header can solve the problem (specific zookeeper information can refer to: http://www.w3.org/P3P/), P3P set the code:

  1. Header ('p3p: CP = "CURa ADMa DEVa PSAo PSDo our bus uni pur int dem sta pre com nav otc noi dsp cor" '); // ecshop

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.