Session variables after header redirection

Take me, if this looks like the other questions posted here, I already went through all the answers provided, but did not solve my problem. I reduced my problem to a minimum.

  • I have two pages (page1.php, page2.php)
  • Page1.php creates a session variable, and if a session variable is set, it sends the browser to Page2.php
  • On page2.php, the browser should display the value of the session variable set in Page1. Php
  • My problem is that page2.php displays the session variable as not set.
  • I tried all the solutions sent by other users when the stack overflowed, as you can see from my code below:

page1.php

<?php
//start the session
session_start();

//set the session
$_SESSION['mysession'] = "Hello";


if(isset($_SESSION['mysession'])){
    //redirect the person to page 2
    session_write_close();
    header("Location: page2.php?PHPSESSID=".session_id());
    exit();
} else {
 echo "Session Not Set";
}
?>

page2.php


<?php
//start the session
session_start();
session_id($_GET['PHPSESSID']);

if ( isset ($_SESSION['mysession']) )
   echo $_SESSION['mysession'];
else
   echo "Session not set!";
?>
0
source share
2 answers

session_id() session_start()

id, . session_id() session_start() . . , a-z A-Z 0-9, () - ()!

. cookie , session_id() cookie session_start() , , .

session_id() -

, cookie.

, URL-, .

+2

page2.php, 2 .

session_start();
session_id($_GET['PHPSESSID']);

to

session_id($_GET['PHPSESSID']);
session_start();

Parameters ..http://php.net/manual/en/function.session-id.php

+1

All Articles