Click here to Skip to main content
15,886,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
session data not displaying on logging in for the 1st time but its displaying once logging out and logging in again.

Anything can i do to display session data on example.com/page2.php on logging in for the first time ?

example.com/page1.php

PHP
<?php
session_start();                    
                    $_SESSION['id'] = 1;
                    $_SESSION['name'] = 'dummy name';
                    $_SESSION['email'] = 'dummy@dummymail.com';
                    session_write_close();
header("Location: http://example.com/page2.php");
?>


example.com/page2.php

PHP
<?php

if ($_SERVER['HTTP_REFERER'] == 'http://example.com/page1.php' )

{   

   ob_start();
   session_start();
   echo $_SESSION['id'];
   echo $_SESSION['name'];
   echo $_SESSION['email'];
}
?>

<a href = 'example.com/logout.php'>Logout</a>


example.com/logout.php

PHP
<?php
session_destroy();
header("Location: http://example.com/page1.php");
?>
Posted

1 solution

On your second page put the following code BEFORE you do that IF statement check.
PHP
echo $_SERVER['HTTP_REFERER'];

The value of that is what you want to check the HTTP_REFERER for. In my test the value for that was ALWAYS page2.php

First time you login the HTTP_REFERER is not set... it is set the second time (to the correct value) which then shows your SESSION variables.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900