Forum Moderators: coopster
I created a login form for a site's admin.
If user exists in database go to modifications.php .
The problem I have is that if I am logged in as admin and try to go to index.php the link is disabled.
My code in index.php is
<?php
//connect to database
include_once("config.php");
// check if user is logged in
checkLoggedIn("no");
?>
<HTML>....</HTML>
function checkLoggedIn($status){
switch($status){
case "yes":
if(!isset($_SESSION["loggedIn"])){
header("Location:login.php");
exit;
}
break; case "no":
if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"] === true ){
header("Location: admin_modifications.php");
}
break;
}
return true;
}
I am trying in index page this code :
if(checkLoggedIn("yes"))
print("<li class='logout'><a href=\"logout.php"."\">Logout</a></li>");
else
print("<li class='admin'><a href='login.php'><img src='images/admin.png' width='33' height='25' /></a></li>");
Could someone help me ?
P.S. I am sorry for my English!
Welcome to WebmasterWorld!
checkLoggedIn("no");
This line on your index.php sends 'no' to the checkLoggedIn() function... and the more I look at it the more it looks like your function is written backward for what it should be:
It says if the person is not logged in send them to admin_modifications.php
case "no":
if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"] === true ){
header("Location: admin_modifications.php");
}
break;
}
If the person is logged in send them to login.php
case "yes":
if(!isset($_SESSION["loggedIn"])){
header("Location:login.php");
exit;
}
break;
It just looks backward to me, because if the variable passed says 'yes' they are logged in they are required to login, and if it says 'no' they are not logged in, they are sent to the admin page?
<?php
//connect to database
include_once("config.php");// check if user is logged in
checkLoggedIn("no");
?>
<HTML>....</HTML>function checkLoggedIn($status){
switch($status){
case "yes":
if(!isset($_SESSION["loggedIn"])){
header("Location:login.php");
exit;
}
break;case "no":
if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"] === true ){
header("Location: admin_modifications.php");
}
break;
}return true;
}
This makes more sense to me:
<?php
//connect to database
include_once("config.php");
// check if user is logged in
checkLoggedIn("yes");
?>
<HTML>....</HTML>
function checkLoggedIn($status){
switch($status){
case "yes":
if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"] === true ){
header("Location: admin_modifications.php");
}
break;
case "no":
if(!isset($_SESSION["loggedIn"])){
header("Location:login.php");
exit;
}
break;
}
return true;
}
##### @ ##### @ #####
I would also probably check the session variable rather than passing a 'yes' or 'no' which is 'hard-coded' to the function, but am assuming it is just set this way for testing purposes.
I use on pages that there is no need of login :
<?php
if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"]=== true) {
print("<li class='logout'><a href=\"logout.php"."\">Logout</a></li>");
} else {
print("<li class='admin'><a href='login.php'><img src='images/admin.png' width='33' height='25' /></a></li>");
}?>
To me it is that the function should check whether the user is loggedd in or not... and then decide where to send him..
I think u should do something like this:
$result = checkLoggedIn();
-------
function checkLoggedIn(){
if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"] === true ) return true;
else return false;
}