Sharing session across multiple domains on same server in PHP
Just to verify I am not wrong, you need to share user session across all your applications.
As rightly said above, you may use 4 of the options above. However, I would like to focus on first option which is putting session in DB and would like to suggest another option as keeping sessions in shared directory or server.
Sessions in DB - The answer to your question (What if other user from same network with same user agent hits?) is you will have different session id's value to uniquely identify each row in Table. So, no need to worry about it. But the disadvantage is, each time DB connection would be required and a query would be fired, when session is initialized i.e. for every single page.
Sessions in shared directory/server - Configure all your applications in a such a manner that all applications store session at shared location. Shared location can either be a directory or a secured server. This can easily achieved by using session_set_save_handler.
Sharing session between multiple subdomains
To share sessions across sub-domains, you need to configure two things.
You need the proper cookie settings for the session cookie so that the browser will send the same session cookie to both sub-domains. This involves setting the
domain
attribute on the cookie to the root domain. You can set this in the cookie options for the express-session configuration.You need to make sure that the server for each sub-domain has access to the same session store. If it's actually the same server for each sub-domain, then that's easy. But, if it's a different server, then you will need a shared session store, using some type of shared database (redis, mongodb, etc...). There are session store implementations for many different databases.
How do I maintain PHP sessions across multiple domains on the same server?
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.
Laravel: share session data over multiple domains
- Capture the session id
Session::getId()
in Domain A - send the captured session id via HTTP POST to Domain B
- Access the sent session id in domain B
$sessionid_from_domainA = $_POST['session_from_A']
- Set session in domain B
Session::setId($sessionid_from_domainA)
- Start Session in domain B
Session::start()
Related Topics
Alternative for $_Server['Http_Referer'] PHP Variable in Msie
No Mail Received in Inbox with Xampp 1.8.0, Mercurymail and Mail()
PHP JSON_Encode and JavaScript Functions
Variable Variables in PHP - What Is Their Purpose
Can't Install Xdebug on Windows
Merge a Table and a Change Log into a View in Postgresql
Get Specific Device Information
How to Store Variable Values Over Multiple Page Loads
Warning: MySQLi_Connect(): Unknown MySQL Server Host
Codeigniter Activerecord: Join Backticking
Count Word Frequency in a Text
Find a Percentage Value in a String Using Preg_Match
Check If Method Exists in the Same Class
Getting PHP to Run a Python Script
PHP Flush Stopped Flushing in Iis7.5
Extension PHP5 Does Not Parse in Xampp
Fatal Error: Call to Undefined Function MySQL_Connect() in C:\Apache\Htdocs\Test.PHP on Line 2