Skip to content Skip to sidebar Skip to footer

How Can I Get Session Variables To Persist Across Header Redirects?

How can I get session variables to persist across header redirects? My header redirect looks like this: header('Location:./content/user_dash.php'); I have session_start(); at the t

Solution 1:

Put the session_start() before any other code including plain text.

<?php session_start(); ?>
Hello<?phpecho" ".$_SESSION['firstname']." ".$_SESSION['lastname'];?>.

Solution 2:

You need to debug your application.

It could be a surprise for you, but debugging takes most of the programmers time. So, time to learn some of the real programmers job.

header("Location:./content/user_dash.php?".session_name()."=".session_id());

used along with session_write_close() is a most bulletproof way

if it's still not working start debugging it.

  • check the session file
  • check the cookies
  • check ini settings
  • check session id in the user_dash.php - is it the same?
  • check if you really lose all your session or may be just one variable

thank you for the ini settings. I can see 2 suspicious ones

  • session.use_only_cookies On means sending SID via url will have no effect. check session cookies AGAIN
  • session.cache_expire 180 180 is way too small.

and I may overlooked some as I got not paid for the debugging your app and I have my own job to be done. So, I'd just suggest to make all settings default and never touch them until you got to know what are you doing certainly

Solution 3:

I have found part of my issue my $_SESSION variables though being set are not being saved to the cookie along with the session id. Posting new question on that specific issue.

Solution 4:

WE can fix this issue in two way

i . in Firefox browser setting change character Encoding to "UTF-8"

ii . By PHP code at the page add

<?php header('Content-Type: text/html; charset=utf-8'); ?>

which you going to post

Thanks,

Suresh Ramakrishnan

Solution 5:

Dealing with redirects and header() + refresh / location, may cause the session_id to be updated, depending on the PHP structure used:

By experience, I've found that naming the session does help addressing the values since the session_name prevents the session_id from being refreshed:


Read About it: http://pt2.php.net/session_name


PHP BEFORE ALL ELSE

// If session is not set, this will set it up with the defined name.// Session is then initialized.define("CONF_SESSION_NAME", 'my_session_named');
if (!isset($_SESSION)) {
    session_name(CONF_SESSION_NAME);
    session_start();
}

In my experience, this as avoided session collision from multiple platforms on the same location, and did help going around the header() issues.

Post a Comment for "How Can I Get Session Variables To Persist Across Header Redirects?"