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
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

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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('/');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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