Redirect does not work with header (location) and session variable

1: I use register.php to register clients,

2: data received from the form is sent to 1.php, they are stored in the database

3: after saving the form data in the 1.php database it sends the selected form data (myValue) to register.php? myValue = 'abc'

in 1.php, I save a session variable like this

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

register.php code

 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 the session variable and redirect it to some other page

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

currently, when step 1, step 2, step 3, instead of opening thankyou.php, the following page opens

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

, register.php 1 ( register.php), thankyou.php...

- , ? , ?

register.php

@session_start();


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

, , ( ), thankyou.php, ( , .)

+2
4

register.php session_start, :

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

EDIT:

, , - . :

$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;
}

, , , .

+3

, exit (0); , , php script, - .

+3

session_start() register.php $_SESSION.

+2

, session_start session_write_close, !

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

All Articles