Session Variables Lost Between Pages

Session variables are not persisting between page loads

The answer to this is it was a hosting configuration error. Hosting company changed something and it has worked ever since.

Losing session variables between two pages

I did a test with your Application.cfm page. As I said in the above comments, your session gets restarted for each and every request. (I mean your CFID & CFToken values are changed for every request).

I went through your code flow. Here you are resetting the CFID and CFToken cookies with the session values.

<cfif structKeyExists(session,"cfid")>
<cfcookie name="cfid" value="#session.cfid#" expires="NOW">
<cfcookie name="cftoken" value="#session.cftoken#" expires="NOW">
</cfif>

On every request, the Application.cfm page is executed. At that time, the above condition structKeyExists(session,"cfid") returns true for every request. So every request runs the <cfcookie> code. You have set the cookies to expire "Now", which means they expire immediately. So that your session is considered as a new one. This is the problem in your application.

As per the docs

The cookie expires when the user closes the browser, that is, the cookie is "session only".

So please check your above condition. I'm not sure why you are expiring the cookie immediately. Maybe your business logic is like that, but the code logic is not correct.

So change this logic as per your business needs. Please remove that code and restart your application and then you will get only one CFID and CFToken for each and every request until the session expires.

The below image I've run the application to set CFCookie value. It's considered a different CFID & CFToken value for every request. You can see below the CFID is different, like 2106,2107,2108

Sample Image

If I remove the condition with cookie value, it's considered only one session. The CFID remains 2109 until the session expires.

Sample Image

So please correct your condition and CFCookie functionalities. That's the cause of the problem.

PHP session lost after redirect

First, carry out these usual checks:

  1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening <?php declaration before anything else. Also ensure there are no whitespaces/tabs before the opening <?php declaration.
  2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();)
  3. Make sure cookies are enabled in the browser you are using to test it on.
  4. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo(). Refer to this as to how to turn it off.
  5. Make sure you didn't delete or empty the session
  6. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere
  7. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.
  8. Make sure your file extension is .php (it happens!)

Now, these are the most common mistakes, but if they didn't do the trick, the problem is most likely to do with your hosting company. If everything works on localhost but not on your remote/testing server, then this is most likely the culprit. So check the knowledge base of your hosting provider (also try their forums etc). For companies like FatCow and iPage, they require you to specify session_save_path. So like this:

session_save_path('"your home directory path"/cgi-bin/tmp');
session_start();

(replace "your home directory path" with your actual home directory path. This is usually within your control panel (or equivalent), but you can also create a test.php file on your root directory and type:

<?php echo $_SERVER['SCRIPT_FILENAME']; ?>

The bit before 'test.php' is your home directory path. And of course, make sure that the folder actually exists within your root directory. (Some programs do not upload empty folders when synchronizing)

Why is session lost between SOME pages, not all?

I finally solved the question so I thought I'll post the answer here in case someone else is having trouble.

I changed this line ("/"):

setcookie($cookie_name, $cookie_value, time() + (60*60*24*2), "/");

to ('/')

setcookie($cookie_name, $cookie_value, time() + (60*60*24*2), '/');

and suddenly it works! :-)

php Session variable lost between pages with a form submit

Config.php

<?php
ob_start();
session_start();
$_SESSION['valid_user'] = admin;
?>

Details.php

<?php
include('Config.php');
?>

process_tenant.php

<?php
include('Config.php');

if (isset($_SESSION['valid_user']))
{
echo 'heya';
}
echo 'hi' . $_SESSION['valid_user'];
?>


Related Topics



Leave a reply



Submit