Jump to content

Login giving false error


Smudly

Recommended Posts

I'm having an issue when clicking to login. It gives me the error message: "You must be logged in!". I tried logging in as the admin and another user, but still getting the same message. Do you see what is wrong with the following code, and if so what do I need to change?

 

<?php

session_start();

if ($_SESSION['username'] != Admin)

if ($_SESSION['username'])

   echo "Welcome, ".$_SESSION['username']. "!<br /><a href='settings.php'>Settings</a><br /><a href='logout.php'>Logout</a>";

else
	die("You must be logged in!");

else
if ($_SESSION['username'])

   echo "Welcome, ".$_SESSION['username']. "!<br /><a href='logout.php'>Logout</a><br /><a href='admin/index.php'>Admin Area</a>";

   
   else
	die("You must be logged in!");

?>

Link to comment
Share on other sites

 

Make sure the value of $_SESSION['username'] is what it should be, before anything else.

 

 

print_r($_SESSION);

 

 

Then the logic could be tweaked a bit. I haven't tested this (obviously), but you should get the idea.

 

if( !empty($_SESSION['username']) ) {
     if( $_SESSION['username'] == "Admin" ) {
          echo "Welcome, ".$_SESSION['username']. "!<br /><a href='logout.php'>Logout</a><br /><a href='admin/index.php'>Admin Area</a>";
     } else {
          echo "Welcome, ".$_SESSION['username']. "!<br /><a href='settings.php'>Settings</a><br /><a href='logout.php'>Logout</a>";
     }
} else {
    // Rather than just killing the script, send the user to the login page.
     header("Location: login.php");
     exit();
}

Link to comment
Share on other sites

Well, you seem to be missing a set of braces around your "if()..." statement, you're also missing quotes around Admin (in your if statement). You should use the isset function to check if a variable isset. Finally, your code structure can be consolidated/augmented. Try this:

 

session_start();

if (isset($_SESSION['username'])){
if ($_SESSION['username'] != 'Admin')
	echo "Welcome, ".$_SESSION['username']. "!<br /><a href='settings.php'>Settings</a><br /><a href='logout.php'>Logout</a>";
else
	echo "Welcome, ".$_SESSION['username']. "!<br /><a href='logout.php'>Logout</a><br /><a href='admin/index.php'>Admin Area</a>";
} else
die("You must be logged in!");

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.