Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I maintain PHP sessions across multiple domains on the same server?

Tags:

php

mysql

session

I am looking for a way to maintain PHP sessions across multiple domains on the same server. I am going to be integrating my sites with a Simple Machines Forum so I will need to use MySQL based sessions. Thanks!

like image 813
Kyle Gibbons Avatar asked Oct 28 '08 16:10

Kyle Gibbons


People also ask

How do I share a session between two domains?

Assume you have both domains as virtual servers on one machine and you havent called session_save_path() (or you have called it with the same directory on both servers), you can share sesssion using session_id('..'); For example if you have 2 domains, origin1. localhost and origin2.

How many sessions can PHP handle?

1000+ sessions can still be perfectly handled by standard PHP file based sessions. If you notice that is getting a problem, you can exchange the session backend easily. There are pluggable session handlers for memcached or other memory or database based storage systems.

How do PHP manage the sessions?

the session id is sent to the user when his session is created. it is stored in a cookie (called, by default, PHPSESSID ) that cookie is sent by the browser to the server with each request. the server (PHP) uses that cookie, containing the session_id, to know which file corresponds to that user.

How does PHP keep track of sessions?

The session functions keep track of users by issuing them cookies with a randomly generated session IDs. If PHP detects that a user doesn't accept the session ID cookie, it automatically adds the session ID to URLs and forms.


2 Answers

Depending upon your preferred method of modifying PHP variables (Apache's config, .htaccess), change the session.cookie_domain value to be a consistent value.

I have multiple sub-domains, and each VirtualHost section in the Apache config file contains the following line:

php_value session.cookie_domain mydomain.com

The syntax should be similar if you make the changes in a .htaccess file.

Updated for bobert5064's comment:

For multiple domains (ie domain1.com, domain2.org), I think it is only necessary to choose a common domain name (ie domain1.com). I have never tried this, so I cannot verify that it works, but the logic seems accurate.

There is also a method to set the variables direction in PHP described at http://us.php.net/manual/en/function.session-set-cookie-params.php. The documentation makes no reference to the ability or inability to set cookies on a different domain.

like image 105
KDrewiske Avatar answered Oct 20 '22 08:10

KDrewiske


If one site is going to forward or link to a second it can include the session id in the href of the link or as an input in a form. Similar to George's img tag method, but the session would only move over if and when it was needed.

Which is best depends on the usage pattern of your sites really.

like image 34
Alex Avatar answered Oct 20 '22 10:10

Alex