Strange cookies issue

Discussion in 'PHP' started by MCJim, Aug 27, 2008.

  1. #1
    I'm making a login system. When a user logs in, it stores the username in a cookie. The problem is, the cookie isn't recognized as soon as its created, so if I have some code that relies on if (isset($_COOKIE['username']) ... it doesn't work until the user refreshes. I have the create-cookie code above this if-statement, but the cookie isn't actually stored until the page refreshes. Is this supposed to be how it works? Here is how I'm setting my cookie:

    setcookie('username', $username , time()+3600);
    Code (markup):


    Thanks in advance for any help...

    EDIT: That may have been confusing. To clarify it, the cookie isn't being stored until the page is refreshed. So if the page isn't refreshed, none of the code that relies on the cookie can be executed.
     
    MCJim, Aug 27, 2008 IP
  2. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    on the process page of the login

    you have to set the cookie - which is the first thing to do

    then store that cookie into a session from there

    $_SESSION['user'] = $_COOKIE['user']

    Then when the user passes thru the system they are under
    a session variable, until it is destroyed.

    Either by logging out, or by closing the browser.

    But the great thing is that the cookie is set
    and when they go back to the login page, and if
    you have coded it properly - then they should see
    their login and password fields set.

    Hope this helps
     
    IGiveMoney, Aug 27, 2008 IP
  3. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Alright thanks, so a cookie/session combo, interesting. I'll give it a shot.
     
    MCJim, Aug 27, 2008 IP
  4. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    google something called vauthenitcate

    that may help you ;)
     
    IGiveMoney, Aug 27, 2008 IP