0

I am using sessions for my expressionengine project. I have used the following methods to set sessions at the top of the page.

    if (session_id() == ''){
         session_start(); 
    } or

   if(!isset($_SESSION)){
        session_start();
    }

      or

   if(empty($_SESSION['userid'])){
      session_start(); 
   } 

I have written custom expression engine plugin where I do session check.

After sometime I observed a strange behaviour of sessions. It is getting set different for different url. for http:// url it sets differently and for www:// url it sets differently. How do I set and retrieve sessions such that both http://example.com and www.example.com will yeild same result.

I have followed steps from this url as well http://ellislab.com/forums/viewthread/231468/. I have posted the same issue on http://ellislab.com/forums/viewthread/232604/. But its not helping me.

Can someone help me on this.

Srikanth V M
  • 672
  • 5
  • 14
  • 31

2 Answers2

2

A small note: www is a subdomain, not a protocol. So it's www.example.com, not www://example.com

This being said: the problem is the fact that the session cookie (I guess you are using a session cookie) is not recognized on the different subdomains, so you will need to make it available on the entire domain

You could check this php function for more details: http://php.net/session_set_cookie_params

mishu
  • 5,347
  • 1
  • 21
  • 39
0

Thanks @mishu. After you explained that www was a subdomain and a session_cookie problem, I did some experimentation and then googled a bit.

This post on stackoverflow was very helpful. PHP Sessions across sub domains

I opened the index.php file on root folder of expressionengine / codeigniter and then added the below line

ini_set('session.cookie_domain','.example.com');

Now its working fine. If someone could explain briefly on this thread, it will be of great use to other developers.

Community
  • 1
  • 1
Srikanth V M
  • 672
  • 5
  • 14
  • 31
  • http://stackoverflow.com/questions/4948340/how-to-pass-session-variable-to-a-page-in-the-parent-directory is also a useful post. – Srikanth V M Jan 15 '13 at 15:23
  • you are welcome. And that is why I gave the link to that specific function, not just to suggest you should use it. Maybe the system you are using has a recommended wrapper over it, but that man page has the advantage of being a good starting point for understanding the idea (the description and links to other resources); I am glad that you solved your problem – mishu Jan 15 '13 at 15:28