Steve2000 Posted June 29, 2007 Share Posted June 29, 2007 Hi, I'm fairly new to PHP, and have just started creating a very basic PHP login system for my site, and have gotten stuck on the login page. I want it to check to see if you're already logged in, and if you are, display a message saying you're already logged in. If you aren't logged in already, it will display the login form. Here's the code: <?php session_start(); if($_SESSION['user_level'] == 4){ echo "You are already logged in as ". $_SESSION['username'] ."!"; } if($_SESSION['user_level'] == 3){ echo "You are already logged in as ". $_SESSION['username'] ."!"; } if($_SESSION['user_level'] == 2){ echo "You are already logged in as ". $_SESSION['username'] ."!"; } if($_SESSION['user_level'] == 1){ echo "You are already logged in as ". $_SESSION['username'] ."!"; } if($_SESSION['user_level'] == 0){ echo "You are already logged in as ". $_SESSION['username'] ."!"; } else { include 'loginform.php'; } ?> This works if you're logged in, but if you aren't then it still shows the "already logged on" message anyway. My second question is instead of using those 5 seperate terms, could I just use one to check whether or not you're logged in? Any help would be very much appreciated. Thanks. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted June 29, 2007 Share Posted June 29, 2007 My guess is that whether or not the session has been created it will still be nothing so the last IF will be true. Try checking using empty() instead and place it at the start instead of the end: <?php if (empty($_SESSION['user_level']) {include("loginform.php");} else { if ($_SESSION['user_level']>=0&&$_SESSION['user_level']<=4) { echo "You are already logged in as ".$_SESSION['username']."!"; } } ?> Quote Link to comment Share on other sites More sharing options...
sushant_d84 Posted June 29, 2007 Share Posted June 29, 2007 YES THAT IS CORRECT.... also try to use the function isset($_SESSION['urVarialbe']) Quote Link to comment Share on other sites More sharing options...
Yesideez Posted June 29, 2007 Share Posted June 29, 2007 I never use isset(). I never rely on PHP testing if a variable has been set or not as I always test for the contents instead. Quote Link to comment Share on other sites More sharing options...
Steve2000 Posted June 29, 2007 Author Share Posted June 29, 2007 Would it matter if the 0 is a user level? The user levels go from 0 to 4, so would it be better to make them 1 to 5? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted June 29, 2007 Share Posted June 29, 2007 I was thinking that myself and you beat me to posting it Quote Link to comment Share on other sites More sharing options...
Steve2000 Posted June 29, 2007 Author Share Posted June 29, 2007 OK got it sorted now, thanks for the help guys! Quote Link to comment Share on other sites More sharing options...
Yesideez Posted June 29, 2007 Share Posted June 29, 2007 btw, imstead of having loads of IF conditions, try using the switch() statement. You can easily test for a multitude of values and have a default action of none of the values prove positive. <?php switch ($_SESSION['user_level']) { case 1:echo 'User level User';break; case 2:echo 'User level 2';break; case 3:echo 'User level 3';break; case 4:echo 'User level 4';break; case 5:echo 'User level Admin';break; default:echo 'Not allowed'; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.