$ _SESSION created, but theres no PHPSESSID in $ _SERVER ['HTTP_COOKIE']

I am having some strange problems with SESSION variables in my shopping cart in PHP / Ajax.

When I first view the page, SESSION is created and works inside the page. Then, when I go to another PHP page in the same directory, SESSION is completely lost. What is strange is that this happens only once . When the user goes through this process, completely losing their SESSION when changing the page, SESSION works completely throughout the cart.

I started sending my var_exports both $ _SESSION and $ _SERVER data on every pageview. It seems that when the page is first viewed, there is a SESSION and contains data. However, there is no PHPSESSID generated in the variable $ _SERVER ['HTTP_COOKIE']. When you go to another page, PHPSESSID is created, and SESSION will start working, but the original SESSION data for the first page view will be lost.

Is there a way to create a PHPSESSID if it has not already been created for the SESSION? Or is this typical behavior and is not relevant to my random SESSION loss problem? I am using PHP 5.2.

Each page in the basket begins exactly the same:

$title="Title"; $keywords="keywords"; $description="description"; @include('../header_cart.php'); 

And then at the top of header_cart.php is:

 session_start(); if(!isset($_SESSION['active'])){ $_SESSION['active']=$_SERVER['REMOTE_ADDR']; } 
+7
source share
3 answers

It turns out that it was recognizing mydomain.com and www.mydomain.com as separate sessions and storing 2 cookies with two different PHPSESSIDs.

I added this to my .htaccess file to always redirect mydomain.com/shop to www.mydomain.com/shop for http and https.

 RewriteEngine On #force http://www. to make sure SESSION data is always the same RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} !^www\. RewriteCond %{REQUEST_URI} shop RewriteRule ^(.*)$ http://www.mydomain.com/shop/$1 [R,L] #force https://www. to make sure SESSION data is always the same RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} !^www\. RewriteCond %{REQUEST_URI} shop RewriteRule ^(.*)$ https://www.mydomain.com/shop/$1 [R,L] 
+1
source

Have you checked that there is no output before calling session_start ()? (Even a symbol of white space!).

HTTP headers cannot be sent after any output has been discarded, so this may cause an attempt to inform the client that the initial session cookie failed.

+1
source

Are you switching between http: and https :? They are sometimes considered as two separate domains, and the key cannot be shared between them.

+1
source

All Articles