Jump to content

[SOLVED] Two Quick Questions on Sessions


Steve2000

Recommended Posts

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.

Link to comment
Share on other sites

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']."!";
  }
}
?>

Link to comment
Share on other sites

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';
  }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.