473,795 Members | 3,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to share session data across multiple domains on same server?

2 New Member
I heard the best method to share session across multiple domains on same server is to use custom php session handler. (ie, domain name different like abc.com, xyz.com but single application.)

But after i tried it, even custom php session handler that using SAME DATABASE ON 1 SERVER can't share session, when i tried to read cookie value from different domain.

Here's my custom session handler, Please kindly check or fix if something missing here. because i've tried it for a week now. can't get it to work



SESSION_INCLUDE .PHP
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  
  3. // config 
  4. $m_host = "localhost"; //MySQL Host 
  5. $m_user = "db_user"; //MySQL User 
  6. $m_pass = "db_pass"; //MySQL Pass 
  7. $m_db   = "db_name"; //MySQL Database
  8. $table  = "sess_data";
  9.  
  10. $session_expire = 600; // Session expire time, in seconds (minutes * 60 = seconds) 
  11.  
  12. $gc_probability = 100; // Probability that the garbage collection function will be called. 50% chance by default 
  13.  
  14. ini_set("session.gc_probability",$gc_probability); 
  15.  
  16. /* Open function; Opens/starts session 
  17.  
  18.    Opens a connection to the database and stays open until specifically closed 
  19.    This function is called first and with each page load */ 
  20.  
  21. function open ($s,$n) // do not modify function parameters 
  22.   global $session_connection, $m_host, $m_user, $m_pass, $m_db; 
  23.   $session_connection = mysql_pconnect($m_host,$m_user,$m_pass); 
  24.   mysql_select_db($m_db,$session_connection); 
  25.   return true; 
  26.  
  27. /* Read function; downloads data from repository to current session 
  28.  
  29.    Queries the mysql database, unencrypts data, and returns it. 
  30.    This function is called after 'open' with each page load. */ 
  31. function read ($id) // do not modify function parameters 
  32.   global $session_connection,$session_read,$table; 
  33.   $query = "SELECT data FROM `$table` WHERE id=\"{$id}\""; 
  34.   $res = mysql_query($query,$session_connection); 
  35.   if(mysql_num_rows($res) != 1) return ""; // must return string, not 'false' 
  36.   else 
  37.   { 
  38.     $session_read = mysql_fetch_assoc($res); 
  39.     $session_read["data"] = base64_decode($session_read["data"]); 
  40.     return $session_read["data"]; 
  41.   } 
  42. function write ($id,$data) // do not modify function parameters 
  43.   if(!$data) { return false; } 
  44.   global $session_connection, $session_read, $session_expire, $table; 
  45.   $expire = time() + $session_expire; 
  46.   $data = mysql_real_escape_string(base64_encode($data)); 
  47.   if($session_read) $query = "UPDATE `$table` SET data=\"{$data}\", expire=\"{$expire}\" WHERE id=\"{$id}\""; 
  48.   else $query = "INSERT INTO sess_data SET id=\"{$id}\", data=\"{$data}\", expire=\"{$expire}\""; 
  49.   mysql_query($query,$session_connection); 
  50.   return true; 
  51. function close () 
  52.   global $session_connection; 
  53.   mysql_close($session_connection); 
  54.   return true; 
  55. function destroy ($id) // do not modify function parameters 
  56.   global $session_connection,$table; 
  57.   $query = "DELETE FROM `$table` WHERE id=\"{$id}\""; 
  58.   mysql_query($query,$session_connection); 
  59.   return true; 
  60. }
  61. function gc ($expire) 
  62.   global $session_connection,$table; 
  63.   $query = "DELETE FROM `$table` WHERE expire < ".time(); 
  64.   mysql_query($query,$session_connection); 
  65. }
  66. // Set custom handlers 
  67. session_set_save_handler ("open", "close", "read", "write", "destroy", "gc"); 
  68.  
  69. // Start session 
  70. session_start(); 
  71. ?>


// MySQL Database Table
Expand|Select|Wrap|Line Numbers
  1. create table sess_data (
  2. id2 int not null auto_increment,
  3. id text not null,
  4. data text,
  5. expire int not null,
  6. primary key(id2)
  7. );
Jan 21 '11 #1
2 4348
dlite922
1,584 Recognized Expert Top Contributor
What you've done is just change the location of where PHP stores information. I don't see any code that tells it to share this across multiple domains.

I'd go back to where you 'heard' about this and ask them what else you need to do different.

If I had to guess, I'd guess that your read would now need to look up the record with something other-than $id. It has to be a custom id that YOU makeup for your clients. That PHP id you're currently using is issued by PHP and PHP does not share that ID between domains.

Cheers,


Dan
Jan 21 '11 #2
Mas Heru
2 New Member
"I don't see any code that tells it to share this across multiple domains."

because it's still on testing phase i just use firefox addon to manually change the ssid cookie value myself.. so the value of the session is same with the old domain.. no problem with that. & on real life i can just pass that old session id from old domain to new domain using $_GET
Jan 22 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
3478
by: George Hester | last post by:
In one Application (2) the client is redirected to a Logon ASP in a different Application (1). A Session Variable is made in Application 2 which needs to be recognized in Application 1. Can I do that in Windows 2000 SP3 IIS5 no .NET Framework? Thanks. -- George Hester __________________________________
4
2563
by: Le | last post by:
Hello I was wondering if there was a way to keep a user's session info across multple domains For example, company A owns website www.a.com and www.b.com. A user logs into www.a.co and later visits www.b.com. I would like to have it where the user doesn't have to login again. Thank Le
0
1123
by: Darren Oakey | last post by:
G'day - is there anyway of sharing the Session data across two different ASP.Net projects? Basically, I'm jumping from a page in one to a page in another, and I'd like to have the data flit across the divide!
1
2203
by: eSapient | last post by:
My objective was to implement a 'Wait' page by using a combination of an asynchronous call and a Session variable to signal the end of the wait. My first page has: --------------------------------------------------------------- private void cmdSubmit_Click(object sender, System.EventArgs e) { Thread LongProcessThread = new Thread(new ThreadStart(LongProcess));
2
8005
by: Jim Tilson | last post by:
I'm having a problem with Session ID being duplicated across multiple IE windows. I edited the web.config for my ASP.net web application to display trace information on the page. I open an IE window and navigate to the home page of my ASP.net app, and note the Session ID that was assigned. I then type CTRL-N to open a new IE window. The session ID displayed on the
1
799
by: Nayana Thara | last post by:
We have a web site where we have asp and asp.net. how can we share session data b/w the two environments.
3
8297
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4. I have two domains -- www.mydomain1.com and www.mydomain2.com. Both point to the same IP address. I have two pages on that IP -- first.php <?php session_start(); $_SESSION = "hello"; ?>
18
2367
by: BDE Consulting | last post by:
I am going crazy. This has been a problem now for over a year and I have yet to figure out what is causing it. I have a single server that is running multiple domains. For this example I will list four domains all of which have the phpinfo.php file in their root directories. However, only one of them has working PHP. What am I doing wrong? http://www.lyricvault.com/phpinfo.php http://www.paynephotos.com/phpinfo.php
5
2610
rajiv07
by: rajiv07 | last post by:
Hi to all I am wondering how do i share the session across multiple Domains.Suppose i have set session in example1.com and i want to access the session in example2.com which is set in example1.com.And both are in different IP address. If you have any idea or any reference material will be appreciated. Regards Rajiv
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.