Skip to main content

$_SESSION created but theres no PHPSESSID in $_SERVER


I'm experiencing some weird problems with SESSION variables on my PHP/Ajax online shopping cart.



When I first view the page, the SESSION is created and works within the page. Then when I navigate to another PHP page within the same directory the SESSION is completely lost. What's weird is that this only happens once . Once the user goes through this process of completely losing their SESSION upon changing page, the SESSION works in full across the entire cart.



I started mailing myself var_exports of both $_SESSION and $_SERVER data on each page view. It seems that when a page is first viewed, the SESSION exists and contains data. However there is no PHPSESSID generated in the $_SERVER['HTTP_COOKIE'] variable. On navigating to another page, the PHPSESSID gets created and the SESSION will start working, but the initial SESSION data of the first page view is lost.



Is there a way to generate a PHPSESSID if one has not yet been generated for the SESSION? Or is this typical behaviour and is irrelevant to my random SESSION loss problem? I'm using PHP 5.2.



Every page in the cart starts the exact same way:




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



And then at the top of header_cart.php there is:




session_start();
if(!isset($_SESSION['active'])){
$_SESSION['active']=$_SERVER['REMOTE_ADDR'];
}


Source: Tips4all

Comments

  1. Have you checked that there is no output before your call to session_start()? (Not even a white-space character!).

    HTTP headers cannot be sent after any output has been flushed so that could be causing the attempt to tell the client the initial session cookie to fail.

    ReplyDelete
  2. Are you switching between http: and https: ? They are sometimes treated as two separate domains, and a key may not be shared between them.

    ReplyDelete
  3. Turns out it was recognizing mydomain.com and www.mydomain.com as separate sessions and was storing 2 cookies with 2 different PHPSESSIDs.

    I added this to my .htaccess file to always redirect mydomain.com/shop to www.mydomain.com/shop for both 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]

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.