2

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

Zaffar Saffee
  • 6,167
  • 5
  • 39
  • 77

4 Answers4

4

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.

MarkR
  • 187
  • 1
  • 9
3

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.

AlexC
  • 1,091
  • 13
  • 25
  • 1
    @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. – AlexC Feb 20 '12 at 19:52
  • 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? – MarkR Feb 20 '12 at 21:05
  • 1
    okay, going to post the code, and i starting session and session variable before anything else is executed or displayed.. – Zaffar Saffee Feb 20 '12 at 21:08
2

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

Nathan Loding
  • 3,185
  • 2
  • 37
  • 43
0

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");
duykhoa
  • 2,227
  • 1
  • 25
  • 43