简体   繁体   中英

Redirect not working with Header(Location ) and session variable

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..)

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.

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.

您在register.php中调用session_start()需要在调用任何$_SESSION变量之前。

I have the same issue, then I try to add session_start and session_write_close , and it works!

session_start();
$_SESSION['status'] = 'Updated Poem successfully';
session_write_close(); 
header("location: index.php");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM