Jump to content

[SOLVED] cookie handling help


sasori

Recommended Posts

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

b7guua.jpg

Link to comment
https://forums.phpfreaks.com/topic/157907-solved-cookie-handling-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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