-2

i wrote this code but it is not working. simply check if some user is already login (with in a browser), if yes then don't show the login page - redirect user to dashboard.

<?php
session_start();
if(isset($_SESSION['usr']))
{
header('location:dashboard.php');   
}


error_reporting(0);

?>
Phani
  • 23
  • 2
  • 9
  • whats the issue ? is it on the login page – Susheel Singh May 07 '14 at 13:04
  • Just as a little more info, what is `$_SESSION['usr']`? A `string`, `boolean`, `object`, etc. Also, it would probably be a good idea to put `exit;` after `header('location: dashboard.php');` so that the script terminates immediately after sending the redirect header provided you don't make any critical modifications to your session after the header is sent. – War10ck May 07 '14 at 13:05
  • possible duplicate of [PHP Session Security](http://stackoverflow.com/questions/328/php-session-security) – Bud Damyanov May 07 '14 at 13:06
  • $_SESSION['usr'] which is holding a string(username) – Phani May 07 '14 at 13:22

1 Answers1

0
<?php
session_start();
echo $_SESSION['usr']; //just to check
if(isset($_SESSION['usr']))
{
header('location:dashboard.php');   
}
else
{
header('location:login.php');   
}
?>

check correctly that $_SESSION['usr'] is right. Is it usr or username like that? The above code is correct. If it is still not working, it means that there was an error while setting that session ur. recheck the code, to after login form. Try echo $_SESSION['usr']. so that it will make things more clear

Gijo Varghese
  • 11,264
  • 22
  • 73
  • 122