Thursday, September 12, 2013

Passing SESSION variables between Domain and Subdomain

An issue I came across recently was that when I set a Session variable at the subdomain level of my site I couldn't access that session variable at the root level. In other words, when I set $_SESSION['name']= "John"; at http://john.harbison.com I couldn't access $_SESSION['name'] at www.harbison.com. That sucks! I did find the answer though thanks to Jeroen on Stack Overflow.

The solution is to give your SESSION a Name! You need to name the session before you even start the session. So here is the code:
$some_name = session_name("some_name");
session_set_cookie_params(0, '/', '.some_domain.com');
session_start();
I love the internet!

No comments: