peanut2222 Posted October 27, 2009 Share Posted October 27, 2009 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!! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 27, 2009 Share Posted October 27, 2009 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 Quote Link to comment Share on other sites More sharing options...
Garethp Posted October 27, 2009 Share Posted October 27, 2009 Don't forget, you need to use session_start(); at the top of the page, before you use sessions in any way Quote Link to comment Share on other sites More sharing options...
peanut2222 Posted October 27, 2009 Author Share Posted October 27, 2009 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!!! Quote Link to comment Share on other sites More sharing options...
dreamwest Posted October 27, 2009 Share Posted October 27, 2009 Logout: session_start(); session_destroy(); Ive seen some ppl make logout script 6 KB in size, what are they thinking - its so easy Login: session_start(); $_SESSION['userid'] = 'name here'; Quote Link to comment Share on other sites More sharing options...
cags Posted October 27, 2009 Share Posted October 27, 2009 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. Quote Link to comment Share on other sites More sharing options...
dreamwest Posted October 27, 2009 Share Posted October 27, 2009 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 Quote Link to comment Share on other sites More sharing options...
cags Posted October 27, 2009 Share Posted October 27, 2009 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? Quote Link to comment Share on other sites More sharing options...
dreamwest Posted October 27, 2009 Share Posted October 27, 2009 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('/'); 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.