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
https://forums.phpfreaks.com/topic/204141-login-giving-false-error/
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();
}

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!");

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.