Simmo Posted October 20, 2010 Share Posted October 20, 2010 Hi I have written a server session on login in the main domain and am trying to read it when they are sent to their sub domain. Unfortunately it won't read the session. I have searched for a solution, which points to altering the php ini. Is there another option that anyone knows about. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/216356-sessions-and-sub-domain/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 20, 2010 Share Posted October 20, 2010 Is the subdomain hosted on the same server as the domain? Have you set the session.cookie_domain setting so that it matches all variations of your domain name? Quote Link to comment https://forums.phpfreaks.com/topic/216356-sessions-and-sub-domain/#findComment-1124349 Share on other sites More sharing options...
Simmo Posted October 20, 2010 Author Share Posted October 20, 2010 Hi I have tried various snippets settings from searching smilar to what you have said. I have also tried snippets of code in the htaccess but it didn't like that at all. Could you post one that you know works and tell me how it should be use e.g. is it on each page you call the session. I can't post the code I have tried because I have tried a few and didn't save them Thanks Quote Link to comment https://forums.phpfreaks.com/topic/216356-sessions-and-sub-domain/#findComment-1124408 Share on other sites More sharing options...
PFMaBiSmAd Posted October 20, 2010 Share Posted October 20, 2010 You can only set php settings in a .htaccess file when php is running as an Apache Module AND your web host has permitted settings to be changed in a .htaccess file. You can only set php settings in a local php.ini when php is running as a CGI application AND your web host has configured your server to cause php to use a local php.ini (some server configurations require a local php.ini in each folder that you want the settings to affect.) If both of those methods are not available, you can always set the session.cookie_domain setting in your script (before every session_start() statement), using either session_set_cookie_params or ini_set Quote Link to comment https://forums.phpfreaks.com/topic/216356-sessions-and-sub-domain/#findComment-1124417 Share on other sites More sharing options...
Simmo Posted October 20, 2010 Author Share Posted October 20, 2010 Thanks for reply I may be way off and apologise for being hard work, but does this look about right: ini_set('session.cookie_domain', '.subdmonain.maindomain.co.uk'); I have tried this before each session_start(); when creating the session and trying to read it, though it didn't work. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/216356-sessions-and-sub-domain/#findComment-1124420 Share on other sites More sharing options...
PFMaBiSmAd Posted October 20, 2010 Share Posted October 20, 2010 You would use the domain name only, because you want the setting to match all variations of the domain name - ini_set('session.cookie_domain', '.maindomain.co.uk'); Quote Link to comment https://forums.phpfreaks.com/topic/216356-sessions-and-sub-domain/#findComment-1124423 Share on other sites More sharing options...
Simmo Posted October 20, 2010 Author Share Posted October 20, 2010 Hi I can't understand why its not working. I have two test scripts: swrite and sread swrite is in the main root: <?php ini_set('session.cookie_domain', '.maindomain.co.uk'); session_start(); $_SESSION['test'] = "Write text"; ?> and sread is in the subdomain folder: <?php ini_set('session.cookie_domain', '.maindomain.co.uk'); session_start(); echo $_SESSION['test']; ?> I have also tried removing the period before the main domain Any advice would be appreciated Thanks Quote Link to comment https://forums.phpfreaks.com/topic/216356-sessions-and-sub-domain/#findComment-1124445 Share on other sites More sharing options...
PFMaBiSmAd Posted October 20, 2010 Share Posted October 20, 2010 Add the following three lines of code to both files, starting on the next line after your first opening <?php tag - ini_set("display_startup_errors", "1"); ini_set("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/216356-sessions-and-sub-domain/#findComment-1124454 Share on other sites More sharing options...
Simmo Posted October 20, 2010 Author Share Posted October 20, 2010 Hi That works!! thank you very much. Can you please tell me what the code does. Quote Link to comment https://forums.phpfreaks.com/topic/216356-sessions-and-sub-domain/#findComment-1124475 Share on other sites More sharing options...
PFMaBiSmAd Posted October 20, 2010 Share Posted October 20, 2010 The only thing that code does (did you read it?) would be to report and display any php detected errors. If it suddenly started working, you likely had an existing session id cookie set with values that did not work and you likely completely closed your browser so that you got a new session id cookie with the setting that does work. Quote Link to comment https://forums.phpfreaks.com/topic/216356-sessions-and-sub-domain/#findComment-1124477 Share on other sites More sharing options...
Simmo Posted October 20, 2010 Author Share Posted October 20, 2010 Hi Mmmm. I thought the E_ALL may have suppressed an error.(only guessing though) Anyway. To test it I created a killsession.php : <?php ini_set('session.cookie_domain', '.booking-availability.co.uk'); session_start(); session_destroy(); ?> then ran the same scripts, only this time I did get an error on the sread.php: Notice: Undefined index: test in /hom........ Line 7 : echo $_SESSION['test']; I then opened a different browser (ff) and everything was fine. So I guess It was not working because I was creating sessions to test it in different places. I can't understand why my kill sessions script didn't work though? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/216356-sessions-and-sub-domain/#findComment-1124482 Share on other sites More sharing options...
Simmo Posted October 20, 2010 Author Share Posted October 20, 2010 All is good, I closed ie and then opened it to start a new session and everything is working. Thanks for your time Quote Link to comment https://forums.phpfreaks.com/topic/216356-sessions-and-sub-domain/#findComment-1124523 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.