0

I have a sign-in page. Once a user is logged in, I want to use php to redirect to a new page. I am trying to do this by checking if a user id is set or not.

One: CONFIG FILE

session_start();

Two: MAIN PAGE

require ('config.php');
if (isset($_SESSION['user_id'])) 
{  
echo'
<META http-equiv="refresh" content="0;URL=**signed in page URL here**>
';}

Up to here works fine. If a correct login is used, the redirection takes place.

Three: PAGE IF SIGNED IN

<?php
require ('config.php');
if (isset($_SESSION['user_id'])) 
 { 
echo'HTML CONTENT HERE';}
else{echo'Not Logged In';}
?>

At the new page, the above code is used but always returns 'Not logged in'. Why is this happening when to get to this step, the same if statement must be true? Why am i losing the session data?

Is there a better way to redirect a logged in user without using headers?

Kind regards,

DVCITIS
  • 1,067
  • 3
  • 16
  • 36
  • First thing I'd do is display errors and set error reporting to `E_ALL`. In your `php.ini` file - `display_errors = On` and `error_reporting = E_ALL`. – Phil Nov 25 '13 at 05:35
  • Steps to find the cause: debug! Output the session value after setting, after the login, before the meta refresh, and on the signed in page. That will tell you exactly where you're "losing" your value – Sterling Archer Nov 25 '13 at 05:36
  • config.php and signedin page in same directory? – Krish R Nov 25 '13 at 05:36
  • config file is not in the same directory. I just used 'config.php' as an example. Main is in web root. Config is a file above; the 'require' statements are working fine because it works for the main page and redirection takes place. – DVCITIS Nov 25 '13 at 05:40
  • first step - remove the require and write session_start in all pages, second step is to debug, there is something missing in your question... if this was all the code alone it seems to be good. – Nimrod007 Nov 25 '13 at 06:58
  • Thanks for the suggestions. I replaced the require statements with session_start and debugged. Now, the first time I log in, the userid is not picked up. The second time it is. I understand this must be due to timing and the server is not receiving the userid before i run the if (isset($_SESSION['user_id'])). – DVCITIS Nov 27 '13 at 05:29

0 Answers0