Jump to content

Login logout link help


peanut2222

Recommended Posts

Hi all,

 

Im new to php and Im having trouble creating a login/logout link for my page that displays as: Log Out | My Account.

when a member is signed in,

And: Log In | Sign Up

When no one is signed in.

 

The code I've been working with so far is:

 

<?php if(!isset($_SESSION['userid']))

{

echo "<a href='../register.php'>My Account</a>"

echo "<a href='$thisPage?logout=true'>Logout</a>"

}

else

{

echo "<a href='../login.php'>Log In</a>"

echo "<a href='../register.php'>Sign Up</a>"

}

?>

 

But when I try to view it nothing displays on the page.

 

I'd love some help with this, thankyou thankyou thankyou!!

Link to comment
https://forums.phpfreaks.com/topic/179137-login-logout-link-help/
Share on other sites

You are missing the semi-colons at the end of each statement:

<?php
if(!isset($_SESSION['userid']))
{
    echo "<a href='../register.php'>My Account</a>";
    echo "<a href='$thisPage?logout=true'>Logout</a>";
}
else
{
    echo "<a href='../login.php'>Log In</a>";
    echo "<a href='../register.php'>Sign Up</a>";
} 
?> 

 

Ken

Thanks so much for the help guys,

 

I've added the ; and

<?php
session_start();
$_SESSION['userid'];
?>

to the top of the page, so the code now looks like:

<?php if(!isset($_SESSION['userid']))
{
  echo "<a href='../register.php'>My Account | </a>";
  echo "<a href='../logout.php?Logout=true'>Logout</a>";
}
else
{
  echo "<a href='../login.php'>Log In | </a>";
  echo "<a href='../register.php'>Sign Up</a>";
} 
?> 

 

But for some reason the 'MyAccount, Logout' version of the code is always visible, whether a user is logged in or not.

 

can anyone see a reason for this?

I've been trawling the net and php books looking for answers but im coming up blank.

 

Thanks again!!!

Thats not a particularly good logout script though.  The session_destroy function doesn't actually unset any of the global variables, it only destroys information about the session itself. If session_start is called again the values will still be set, this is undesirable behavior.

Thats not a particularly good logout script though.  The session_destroy function doesn't actually unset any of the global variables, it only destroys information about the session itself. If session_start is called again the values will still be set, this is undesirable behavior.

 

But thats all logout.php will contain:

session_start();
session_destroy();

 

...and maybe a redirect to the main page.

 

I tested it - I destroyed the session and went to another page on the same site and the session was gone..... Magic :o

Did you actually read what I said, or even check the documentation of session_destroy? Or are you just going to stand by your view that since you ran a few quick tests that session_destroy is obviously magic and doing exactly what you wish it did?

OK ive improved it:

 

session_start();
error_reporting( 0 );

function redirect( $url ){
    if (! headers_sent( ) ){
    
        header( "Location: ".$url );
        exit( 0 );
    }
    echo "<script language=Javascript>document.location.href='".$url."';</script>";
    exit( 0 );
}

$_SESSION = array();

if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-42000, '/');
}
session_destroy();

redirect('/');

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.