Redirect not working with Header(Location ) and session variable

10,081

Solution 1

In your register.php, you can't test for the session variable before you issue the session_start, so your code should be more like:

session_start(); 
 if (isset($_SESSION['color'])) {
            header('Location: http://mydomain.com/thankyou.php');
    }
 else {
 // Something else....

EDIT:

Another thing I've found useful when trying to set session variable in conjunction with redirects is to proceed to the redirect only after running a function. Here's how it would work:

$throwAwayVariable = setColor('blue');
if($throwAwayVariable ){  // separated out into a function so it wouldn't redirect before the session variable was saved
    session_write_close();
    header("Location: http://mydomain.com/thankyou.php");
}

function setColor($color){
    @session_start();
    $_SESSION['color']='blue';
    return true;
}

Since not all your code is posted, you'll have to figure out where this goes, but I've always had my session vars work after this process.

Solution 2

Make sure you use exit(0); right after you do a header redirect otherwise php will still parse and run the rest of your script, sometimes it can cause some funny behaviour.

Solution 3

Your session_start() call in register.php needs to be BEFORE you call any $_SESSION variables.

Share:
10,081
Zaffar Saffee
Author by

Zaffar Saffee

Totally a NewBee..! aiming at learning things i always thought of; things i wondered how they work; to be a professional.. Recently got my MTA Certificate

Updated on September 09, 2022

Comments

  • Zaffar Saffee
    Zaffar Saffee over 1 year

    1: i use register.php to sign up the clients,

    2: the data collected from the form is send to 1.php, it is saved in database

    3: after form data is saved in database, 1.php forwards selected form data (myValue) to register.php?myValue='abc'

    in 1.php, i am saving a session variable like this

    @session_start();
    $_SESSION['color']='blue';
    

    the code of register.php is

     if (isset($_SESSION['color'])) {
                header('Location: http://mydomain.com/thankyou.php');
        }
     else {
    
    
    @session_start(); 
    some other stuff  that was initially use for signing up the clients
    

    my logic is to check for session variable and to redirect it to some-other page

    when step 1 , step 2 and step 3 are complete, page should be redirected to thankyou.php

    currently, when step 1, step 2, step 3 are done, instead of opening thankyou.php, the following page is being opened

    http://mydomain.com/register.php?myValue='abc'
    

    however, if i re-open register.php or go back to step one (opening register.php), thankyou.php is displayed...

    can somebody guide me where i am doing the blunder? why redirection is not being successful although session variables are being created?

    code Update

    i tried the following code at the top of my register.php

    @session_start();
    
    
       if (isset($_SESSION['color'])) {
                header('Location:http://mydomain.com/thankyou.php');
                exit;
        }
     else{
    remaining stuff
    

    it occasionally do the trick, redirects to the page, while on occasion (greater in number), it fails in redirecting to thankyou.php,, also the code needs to delete complete history and cache to work (after doing so, still miss hits occurs..)

  • AlexC
    AlexC about 12 years
    @NewBee, please see my edits. This process has never failed me in getting a redirect to respect session variable I set on the previous page. If not, ping me back.
  • MarkR
    MarkR about 12 years
    If it's still no working with what you have you might need to post more code so we can see what is going on. Are you running the code you have at the very top of the page before outputting anyting to the user?
  • Zaffar Saffee
    Zaffar Saffee about 12 years
    okay, going to post the code, and i starting session and session variable before anything else is executed or displayed..