Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Bizarre Cookie problem

    I've looked around for anyone with the same problem and I can't see any solution so I decided to post. I have created this login form and it all works fine. I then added cookie support and that also worked. However, I cannot make it so that a cookie expires when the browser is closed. I have used a checkbox "remember_me" in the form, and have tested that it works ok (see the commented out echo statements in the submit section). If I login and no not check the button, then close the browser, then re-open, I am still logged in. I thought if no value for expiry was specified cookies expired on session (browser) close? Mine do not seem to be :(

    PHP Code:


    <?php
        
        
    if (isset($_POST['submit']))    {
        
            require_once (
    '../mysql_connect.php');
            
            function 
    escape_data ($data)    {
            
                global 
    $dbc;
                
                if (
    ini_get('magic_quotes_gpc'))    {
                
                    
    $data stripslashes($data);
                
                }
                
                return 
    mysql_real_escape_string($data$dbc);
                
            }
            
            
    $message NULL;
            
            if (empty(
    $_POST['username']))    {
            
                
    $u FALSE;
                
    $message .= '<p>You forgot to enter your username!</p>';
                
            } else    {
            
                
    $u escape_data($_POST['username']);
                
            }
            
            if (empty(
    $_POST['password']))    {
            
                
    $p FALSE;
                
    $message .= '<p>You forgot to enter your password!</p>';
                
            } else    {
            
                
    $p escape_data($_POST['password']);
                
            }
            
            if (
    $u && $p)    {
                
                
    $query "SELECT user_id, first_name FROM users WHERE username='$u' AND password=MD5('$p')";
                
    $result = @mysql_query ($query);
                
    $row mysql_fetch_array ($resultMYSQL_NUM);
                
                if (
    $row)    {
                    
                    if (isset(
    $_POST['remember_me']))    {
                        
                        
    setcookie ('first_name'$row[1], time()+36000'/'''0);
                        
    setcookie ('user_id'$row[0], time()+36000'/'''0);
                        
    header ("Location: index.php");
                        
    //echo '<p>ticked</p>';
                        
                    
    } else    {
                    
                        
    setcookie ('first_name'$row[1], NULL'/'''0);
                        
    setcookie ('user_id'$row[0], NULL'/'''0);
                        
    header ("Location: index.php");
                        
    //echo '<p>Not ticked</p>';
                        
                    
    }
                    
                } else    {
                
                    
    $message '<p>Error. Your username/password is incorrect.</p>';
                    
                }
                
                
    mysql_close();
                
                
            } else    {
            
                
    $message .= '<p>Please try again.</p>';
                
            }
            
        }
        
        if (isset(
    $message))    {
        
            echo 
    $message;
            
        }

    ?>

    <form action="<?php echo $_SERVER['DOCUMENT_ROOT/index.php']; ?>" method="post">

        <p>Username: <input type="text" name="username" size="15" maxlength="20" value="<?php if (isset($_POST['username']))
        echo 
    $_POST['username']; ?>" /></p>

        <p>Password: <input type="password" name="password" size="15" maxlength="20" /></p>
        
        <input type="checkbox" name="remember_me" /> Keep me logged in (requires cookies to be enabled)

        <div align="center"><input type="submit" name="submit" value="Login" /></div>
        
    </form>

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Your browser might not delete the cookie or show the cached website.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Erm...not sure I really understand your reply. Surely the point of what I'm doing there is to delete the cookie and I'm saying for some reason it isn't working. You've just reiterated my problem. And I'm pretty sure it's not a cached page as I'm doing a live refresh (shift-F5) in FF. Is the code I have written there theoretically correct? Even so you don't provide any possible solutions for the problems you suggest it might be.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    If you are using Firefox, you can view cookies and under "expires" you can see if it says "at end of session". I just tried a NULL expire cookie and it said "at end of session", and when I closed/opened Firefox, the cookie was gone. So... it does work, and if you're getting the "not ticked" display, I would have to call that bizzare as well!

    Anyway, see what "view cookies" tells you.

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by johnharris
    Even so you don't provide any possible solutions for the problems you suggest it might be.
    You managed pretty well on your own, didn't you?
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •