Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2003
    Posts
    56

    Unanswered: Read information for a previously set cookie

    I've been fiddling with cookies for a site with a login system. I managed to set a cookie with the username and md5 encrypted password. However i can't seem to find how to read the info from this cookie when I revisit my site (within the time-out period).

    All I can find is that "when a cookie has been set it can be accessed like a variable". But when I return to the site how do I get it to read the actual values from the cookie on y pc?

    My session.php (which is intended to log the user in if he managed to before based on the cookie data looks something like this:

    PHP Code:
    if (isset($_COOKIE['enterprise']))
    {
            
    $ENTINFO explode("-"$_COOKIE['enterprise']);
            
    $name $ENTINFO[0];
            
    $pass $ENTINFO[1];
            
    $query "select * from users where id=$name and password=$pass";
            
    $sql db_query($query);
            
    $count db_count($sql);
            
    $user db_array($sql);
    ..... 
    There is no error when this is run but also I don't see anything happen.

    The cookie gets set by the login process in login.php:

    PHP Code:
     $query "select * from users where id='$NAME' and password='$PASS' and enabled='yes'";
                    
    $sql db_query($query);
                    
    $count db_count($sql);
                    
    //$user = db_array($sql); Mysql version
                    
    $user db_array($query); // Ingres version
                    
    if ($count == 1) {
                            
    $ENTDATA $NAME.'-'.$PASS;
                            
    setcookie("enterpise",$ENTDATAtime()+3600);
                            
    $_SESSION['id'] = $NAME;
                            
    $_SESSION['passwd'] = $PASS;
                            
    $_SESSION['lastlogin'] = $TIME;
                            
    $_SESSION['name'] = $user[4];
                            
    $_SESSION['depart'] = $user[5];
                            
    $_SESSION['access'] = $user[7];
                            
    header ("Location: index.php");
                    }else{
                            print 
    "Login failed";
                    }
    ... 
    Anyone got a pointer on how to get this to work?

    thanx

  2. #2
    Join Date
    Jan 2004
    Location
    India
    Posts
    168
    Try this
    print_r($_COOKIE);

    this will print all the values in the cookie. This way you can check wheater there is any thing stored in that.

    Regards
    Rajesh
    Freelance and Technology Consultant
    -------------------
    Dreams are for ever
    -------------------

  3. #3
    Join Date
    Dec 2003
    Posts
    56
    I did as you suggested and saw the following show up after I logged in:

    Array
    (
    [enterpise] => pietje-af1d068015addcb0d8cf22bc54103a16
    [PHPSESSID] => 2ca7633744eed46f7a467a005504aeda
    )
    After closing down my browser and then going back ot my site I see the same show up. So i assume (correctly??) that my cookie is being set correctly but my reading it is wrong?

  4. #4
    Join Date
    Jan 2004
    Location
    India
    Posts
    168

    Thumbs up

    Ur code is correct. But there is a small correction is required. The spelling of the cookie name while you set the cookie and while you extract the cookie is different. This is the reason you are not getting the information.

    When you set the cookie the cookie name is "enterpise"
    setcookie("enterpise",$ENTDATA, time()+3600);
    But while you are extracting the information the cookie name you are suppling wrong as "enterprise"
    $_COOKIE['enterprise']

    Just correct this your code will work fine.

    Regards
    Rajesh
    Freelance and Technology Consultant
    -------------------
    Dreams are for ever
    -------------------

  5. #5
    Join Date
    Dec 2003
    Posts
    56
    Doh!

    I hate that word mistype it all the time...and I need to type it several times a day hehehe

    thanx for the help!

Posting Permissions

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