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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex