Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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