Jump to content
Larry Ullman's Book Forums

Chapter 12 - User Appears To Stay Logged In


Recommended Posts

Hi guys,

 

Just got this book a while back and am loving it, very helpful.

 

I'm using Chapter 12 to implement a login/out system on my little app I've been working on. I've successfully gotten to the point of being able to log in, and logging out (to a point). In my "includes/navigation.php" file that includes the basic navigation beneath the header, I've included the logout link, but I customized it slightly. Here's my code for that part:

		  <li>
		  <?php
		  	if ( (isset($_COOKIE['userid'])) && (basename($_SERVER['PHP_SELF']) != 'logout.php') ) {
		  		echo "<a href='logout.php'>logout</a> ({$_COOKIE ['username']})";
		  	} else {
		  		echo '<a href="login.php">login</a>';
		  	}
		  ?>
		  </li>

I basically just wanted the current user's name to be shown next to the logout link within parentheses. That part is working fine, and when I click "logout" it brings me to logout.php and says I've successfully logged out. Great! However... when I click to go to any other page, the navigation again says "Logout (Name)", rather than "Login". Here's my logout.php code:

<?php

// This page lets the user logout.

// If no cookie is present, redirect the user:

if (!isset($_COOKIE['userid'])) {
	// Need the function:
	require ('includes/login_functions.php');
	redirect_user();
} else { // Delete the cookies!
		setcookie ('userid', '', 0, '/', '', 0, 0);
		setcookie ('username', '', 0, '/', '', 0, 0);
}

?>

At first I did have the time()-3600 included, but tried to take that out from this page and the login page, but that didn't seem to have an effect.

 

What am I missing here?

 

Thanks!

Link to comment
Share on other sites

Apparently using sessions did not fix it. After attempting a logout, I then checked the cookies, and there is still a SESSID cookie present.

 

Here's part of my logout.php:

session_start(); // Access the existing session.

// If no session is present, redirect the user:

if (!isset($_SESSION['userid'])) {
	// need the functions.
	require ('includes/login_functions.php');
	redirect_user();
} else {
	$_SESSION = array(); // Clear the variables.
	session_destroy(); // Destroy the session itself.
	setcookie ('PHPSESSID', '', time()-3600, '/', '', 0, 0); // Destroy the cookie.
}

Thanks!

Link to comment
Share on other sites

I added that to one of my pages, and it displayed the user id and user name, as well as a long string. Then I clicked logout and went back to that page, and it was empty. And now the login/logout toggle links on the index page and navigation bar are working correctly now....

 

*scratches head*

Link to comment
Share on other sites

Alright, I think this is what's going on:

 

I have two logout links. One on the navigation bar at the top of the page, and one as a button on the dashboard/index page (the navigation is not included on index.php, for now). When I use the link from the navbar, it seems to work correctly. But when I use the logout link on index.php, it tells me I've successfully logged out, but when I visit another page it says I'm logged in still.

Link to comment
Share on other sites

Oh, I realized what I did wrong. Dumb mistake. Here's the code from the index.php:

<a href="login.php">
	<p class="button center">
		<?php
			  if (isset($_SESSION['userid'])) {
			  	echo "Logout";
			  } else {
			  	echo "Login";
			  }
		?>
	</p>
</a>

Wrapping the <p class="button"> with an anchor tag is what I had to do with some other buttons on the index page, so that some hover states worked correctly. I don't have the time yet, but when I move the php tags to encompass the anchor tags as well, and just include the whole button in the if clause, it should work peachy then.

 

Thanks.

Link to comment
Share on other sites

 Share

×
×
  • Create New...