3

I am trying to use session data on multiple subdomains:

  • www.example.com
  • my.example.com
  • test.example.com
  • whateversub.example.com

When I try to use session data from www.example.com to any subdomain, all the session information is not accessible.

  • I am NOT using cookies. Just sessions.
  • I have GoDaddy as web host.
  • GoDaddy DOES allow to upload a custom php5.ini file.

Since I am a PHP beginner, please dumb down your response so I may understand it.

Here is an example:

File 1:

<?php
// FILE 1: www.example.com/index.php

session_start();
$_SESSION['status'] = "ON";
header( 'Location: http://sub.mywebsite/' );
?>

File 2:

<?php
// FILE 2: sub.example.com/index.php

session_start();
echo "Your session status is: ";
echo $_SESSION['status'];
?>
Omar
  • 11,783
  • 21
  • 84
  • 114

3 Answers3

7

I solve my problem thanks to this link PHP Sessions across sub domains

PHP:

<?php
session_set_cookie_params(0, '/', '.mywebsite.com');
session_start();
//Code...
?>
Community
  • 1
  • 1
Omar
  • 11,783
  • 21
  • 84
  • 114
7

Be sure you set SESSIONID cookie on subdomain, too

ini_set('session.cookie_domain', '.my-domain.com');
genesis
  • 50,477
  • 20
  • 96
  • 125
  • 1
    @Omar: Do you think I got 2 upvotes for nothing? Okay, copy line you pasted into your scripts and try again, BTW, be sure you'll destroy your session first and renew it – genesis Sep 07 '11 at 04:33
  • @Omar: only on subdomain? Just change '.my-domain.com' to something like 'subdomain.mydomain.com' – genesis Oct 14 '11 at 04:40
  • How do I set the SESSIONID cookie on the other subdomain? – Omar Oct 26 '11 at 09:40
  • @genesis i also uploaded my website at a subdomain.. its domain is www.agicent.com,but my main files are at...ibuildmart.agicent.com/cms ,I have kept my login page here. Could please help me.Here session is not working.i am into this problem from many days.All the files are located inside the cms folder,so directly or indirectl the person has to access ibuildmart.agicent.com/cms/index.php.What can be the possible solution.My files are at godaddy server.Please help – Abhishek May 29 '14 at 05:14
1

By default this will not work, but you can get around it as described here: http://www.gonnalearn.com/2008/04/10/sharing-session-data-across-domains-with-php/

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254