Smudly Posted June 7, 2010 Share Posted June 7, 2010 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!"); ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 7, 2010 Share Posted June 7, 2010 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(); } Quote Link to comment Share on other sites More sharing options...
Goldeneye Posted June 7, 2010 Share Posted June 7, 2010 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!"); 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.