I am working on sessioning scripts however my cookies are not carrying over from page to page.

I include the session script on every page which checks the $_COOKIE['session'] cookie, and if set, keeps the session data, if not, creates a guest session and sets a cookie with a new key.

I can see in my SQL Table that the key is being created, however, if I brows to a page on sub directories, it creates a new key.

http://www.example.com <- Session ID [001]
http://www.example.com/subdir <- Session ID [002]

In the sub dir, the $_COOKIE['session'] variable is empty...

I am setting the cookies with the folling line:

setcookie("session", $SessionID, time() + 900, '/', '.example.com' );

If anyone could point me in the correct direction, that would be great.

*Note, cookie need to be accessed and set in every directory.

Solved my own problem by using the following:

setcookie("session", $SessionID, time() + 900, $_SERVER['DOCUMENT_ROOT'] . '/', '.example.com' );

I lied. That initially seemed to work, but ended up creating a new session on every page visit.

Changed - .example.com to example.com
-Fixed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.