sasori Posted May 13, 2009 Share Posted May 13, 2009 hi, I created a log-in page for my practice site. and then I altered the menu, and place an if else loop to make login and logout menu to appear this snippet is my login.php code if(empty($errors)){ $query = "SELECT user_id, first_name FROM users WHERE email='$e' AND password1=SHA('$p')"; $result = mysql_query($query); $row = mysql_fetch_array($result, MYSQL_NUM); if($row){ setcookie('user_id',$row[0]); setcookie('first_name', $row[1]); $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); if((substr($url, -1) == '/') || (substr($url, -1) == '\\')){ $url = substr($url, 0, -1); } $url .= '/loggedin.php'; header("Location: $url"); exit(); this code is for my navigation menu <h3>Menu</h3> <ul> <li class="navtop"><a href="index.php" title="Go to Home Page">Home</a></li> <li><a href="register.php" title="Register">Register</a></li> <li><a href="view_users.php" title="View The Existing Users">View Users</a></li> <li> <?php if((isset($_COOKIE['user_id'])) && (!strpos($_SERVER['PHP_SELF'],'logout.php'))){ echo '<a href="logout.php" title="Logout">Logout</a>'; }else{ echo '<a href="login.php" title="Login">Login</a>'; } ?> </li> <li><a href="password.php" title="Change Your Password">Change Password</a></li> </ul> this code is working.. when i log-in, it redirects to loggedin.php page and the menu shows "logout" button.... but when I go to homepage...the logout still exist...can you guys help me with this?..i believe i already remove the cookies on logout.php page if(!isset($_COOKIE['user_id'])){ $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); if((substr($url, -1) == '/') || (substr($url, -1) == '\\')){ $url = substr($url, 0 , -1); } $url .= './index.php'; header("Location: $url"); exit(); }else{ setcookie('first_name','',time() - 3600,'/',0); setcookie('user_id','',time() - 3600,'/',0); } $page_title = 'Logged out'; require('./includes/header.html'); echo "<h1>Logged out!</h1> <p>you are now logged out {$_COOKIE['first_name']}!</p><p><br /><br /></p>"; and here's the screen shot Quote Link to comment https://forums.phpfreaks.com/topic/157907-solved-cookie-handling-help/ Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 Try changing the line to this - if(!empty($_COOKIE['user_id']) && strpos($_SERVER['PHP_SELF'],'logout.php') !== false){ Quote Link to comment https://forums.phpfreaks.com/topic/157907-solved-cookie-handling-help/#findComment-832985 Share on other sites More sharing options...
sasori Posted May 14, 2009 Author Share Posted May 14, 2009 its not working...the user is logged out when clicked the log out from header,..but on the homepage the menu still shows log out...it only changes to log in if i refresh my browser Quote Link to comment https://forums.phpfreaks.com/topic/157907-solved-cookie-handling-help/#findComment-833728 Share on other sites More sharing options...
sasori Posted May 14, 2009 Author Share Posted May 14, 2009 i tried using session instead of using cookies..now its cool and working smoothly ^^, Quote Link to comment https://forums.phpfreaks.com/topic/157907-solved-cookie-handling-help/#findComment-833756 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.