Forum Moderators: coopster

Message Too Old, No Replies

Problem with logout when I am logged in

         

bufoss

4:27 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



Hi all,
I am new in php and I need your help.

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

but index page then requires login.

Could someone help me ?

P.S. I am sorry for my English!

TheMadScientist

5:00 pm on Oct 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi bufoss,

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.

bufoss

5:41 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



I don't think that the name is the problem.
Could be RequireLoggedIn the function.
Maybe I called it wrong.

Thanks for your quick answer

bufoss

4:47 pm on Oct 9, 2009 (gmt 0)

10+ Year Member



I correct the problem

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>");
}?>

s0h31l

6:40 pm on Oct 9, 2009 (gmt 0)

10+ Year Member



The function checkLoggedIn looks wrong...

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;

}