swthate Posted March 31, 2014 Share Posted March 31, 2014 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 More sharing options...
swthate Posted March 31, 2014 Author Share Posted March 31, 2014 I switched to using sessions, and I'm not having that problem anymore. But, why were the cookies giving me grief? Link to comment Share on other sites More sharing options...
Larry Posted March 31, 2014 Share Posted March 31, 2014 Did you confirm that the delete cookies were being sent? Also I prefer to use a value of false rather than an empty string and an expiration in the past. Link to comment Share on other sites More sharing options...
swthate Posted April 1, 2014 Author Share Posted April 1, 2014 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 More sharing options...
HartleySan Posted April 1, 2014 Share Posted April 1, 2014 Try adding the following to your code to see what's store in the $_SESSION and $_COOKIE variables: echo '<pre>'; print_r($_SESSION); print_r($_COOKIE); echo '</pre>'; Link to comment Share on other sites More sharing options...
swthate Posted April 1, 2014 Author Share Posted April 1, 2014 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 More sharing options...
HartleySan Posted April 1, 2014 Share Posted April 1, 2014 Does that mean that it's correctly working now? I'd continue to play around with it until you're sure that in all instances, you get the intended behavior. Link to comment Share on other sites More sharing options...
swthate Posted April 1, 2014 Author Share Posted April 1, 2014 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 More sharing options...
HartleySan Posted April 1, 2014 Share Posted April 1, 2014 What's the difference between the two logout scripts? Link to comment Share on other sites More sharing options...
swthate Posted April 1, 2014 Author Share Posted April 1, 2014 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 More sharing options...
HartleySan Posted April 1, 2014 Share Posted April 1, 2014 Glad you found the issue. Thanks for sharing the solution. Link to comment Share on other sites More sharing options...
Recommended Posts