Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2003
    Posts
    12

    Unanswered: setcookie() question

    i have created a login.php which will call the setcookie() function once the login is correct, then will direct to somewhere , header("Location:a.php") for example. Below shows the codes of a.php :

    <code>
    <?php
    echo "test.php";
    if($_COOKIE['user'])
    echo $_COOKIE['user'];
    else
    echo "no cookie";
    ?>
    </code>

    But once i close the browser and run a.php again....it display "no cookie" ..but suppose that the cookie still remains in my machine....it should echo out the username instead...may i know what's wrong ? thanks a million

  2. #2
    Join Date
    Jan 2004
    Location
    India
    Posts
    168
    You might have set the session cookie.
    Can you show us the code in your login validation page where you sets the cookie.
    The cookie syntax you might have follow like this:
    setcookie('user',$username);
    now this is just a session cookie.. it doesnot have the expiry time of the cookie. I should be some thing like this
    setcookie ("user", $username,time()+3600);
    this will expire after 1 hour. This is just an example.
    Hope you got it.
    If you still have doubt please type in the code

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

  3. #3
    Join Date
    Oct 2003
    Posts
    12
    this is part of the login page :

    $row = mysql_fetch_array(mysql_query("select * from admin where username = '$_POST[user]';"));
    if($row['password'] == $_POST['pass'] )
    {
    //echo "login success";
    setcookie("user", $row['username']);
    setcookie("pass", $row['password']);
    header("Location:test.php");
    }

    ///////////////////////////////
    if i use setcookie("user",$row['username'], time()+3600), there will be a cookie file created and stored in my local machines is it ?
    but setcookie("user",$row['username']) will not right ?

  4. #4
    Join Date
    Jan 2004
    Location
    India
    Posts
    168
    See this setcookie("user",$row['username']) will just create a cookie for that perticular session. means once you close the bowser the cookie will be distroyed.
    The other case
    setcookie("user",$row['username'], time()+3600)
    it will create a cookie for 1 hr. So after 1hr your cookie will expire and you need to login again. The time should be in secone. time()+3600 say the current time plus the 3600 sec (means 1 hr). You can increase this up to one year.

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

  5. #5
    Join Date
    Oct 2003
    Posts
    12
    that's the exactly answer i was looking for , thx rajesh

  6. #6
    Join Date
    Oct 2003
    Posts
    12
    i found another problem here....even though i have logout , for example setcookie("user","",time()-6000); .....i still can access the page without closing the browser...so any idea?

  7. #7
    Join Date
    Jan 2004
    Location
    India
    Posts
    168
    Do you mean that the user 's value can be retrived after logging out?
    setcookie ("user", "", time() - 3600); This is an example for deleting a cookie. No where the time that you specify is 1hr back to the current time. So it will be an expired cookie. In this case you should be logedout. Try echo the username int he page and see wheater the value retains the same.
    Try this and get back soon..
    Regards
    Rajesh
    Freelance and Technology Consultant
    -------------------
    Dreams are for ever
    -------------------

  8. #8
    Join Date
    Oct 2003
    Posts
    12
    Quote Originally Posted by rajesh_r_r
    Do you mean that the user 's value can be retrived after logging out?
    setcookie ("user", "", time() - 3600); This is an example for deleting a cookie. No where the time that you specify is 1hr back to the current time. So it will be an expired cookie. In this case you should be logedout. Try echo the username int he page and see wheater the value retains the same.
    Try this and get back soon..
    Regards
    Rajesh

    ok , the problem is like this , once i have removed the cookies and logout from the system .....then i try to navigate to other URL , www.google.com for example, after that i copy the previous link(restricted area) and paste it on the same browser....is shows "you are login(example)" but once i refresh the page .....it is completely logout and shows "you are logout(example)".....so i was wondering why i have to manually refresh the browser in order to show the message "you are logout" ? could it be some kind of internet browser cache ? thanks

  9. #9
    Join Date
    Jan 2004
    Location
    India
    Posts
    168
    Ya it is truly internet browser cache problem. Nothing else.

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

Posting Permissions

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