SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 32
  1. #1
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    remembering past user logins

    how do I implement my scripts.... to remember the user's ID?

    I'd like my scripts to show the user's ID when he arrives at the main page. The user would then just have to visually confirm the ID, and click a Submit button to enter the page.

    I know this will probably involve cookies.. but can someone give me detailed steps? Thanks!

  2. #2
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone have a clue?

  3. #3
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There aren't really any detailed steps to give out. Set the cookie with the username whenever you log in; whenever you access the login page check if the username cookies exists and output if it does. http://php.net/setcookie
    ck :: bringing chris to the masses.

  4. #4
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll check it out, thanks Anarchos!

  5. #5
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    lotsa questions.....

    okay.. i just checked out the site, and I'm confused!!

    So I specify the setcookie() command before any HTML opening tags... just like session_header(), which seems to work for me, if I put it right at the top of the page.

    Now, I've went thru the comments & all, and I still don't grasp the concept. I've got a mainpage.php, that accepts a $password, and then redirects to index2.php.

    1) So, I put the setcookie command on mainpage.php, correct?

    2) The path confuses me:

    int setcookie (string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])

    Am I correct if I understand it as:

    setcookie("name of the variable", "value to put in the variable", "time to expire");

    Eg: setcookie("UserID", "$userid", "$date");


    3) Once a cookie is set, how do I retrieve it?

    4) What happens if I don't specify a time to expire?

    Thanks!

  6. #6
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All previously set cookies are stored in the $HTTP_COOKIE_VARS array. So you'd do something like this:

    $cookies = &$HTTP_COOKIE_VARS; // don't want to type out that long name every time
    if (isset($cookies['userid'])) echo $cookies['userid'];

    If you don't specify a date the cookie will be deleted by the browser when it closes.
    ck :: bringing chris to the masses.

  7. #7
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't specify a date the cookie will be deleted by the browser when it closes.
    OK... say I don't specify an expiry date. Does this mean that future logons will not automatically recognize my $userID and take me straight into the next page? If so, and the browser deletes the cookies upon exit, then doesn't that defeat the purpose of cookies in the 1st place?

  8. #8
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't specify a date then the browser will delete the cookie whenever the browser closes. Most times people will set a date of a year ahead.
    ck :: bringing chris to the masses.

  9. #9
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Anarchos
    If you don't specify a date then the browser will delete the cookie whenever the browser closes. Most times people will set a date of a year ahead.
    Yes Anarchos... you mentioned that before. Could you answer my questions? Just need to clarify my concept of cookies.


    "OK... say I don't specify an expiry date. Does this mean that future logons will not automatically recognize my $userID and take me straight into the next page? If so, and the browser deletes the cookies upon exit, then doesn't that defeat the purpose of cookies in the 1st place?"

  10. #10
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did answer your question
    ck :: bringing chris to the masses.

  11. #11
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, you did... but not directly, and I'm still slightly in the dark.

    Let's try this, give me a simple Yes or No answer to these questions:

    1) Say I don't specify an expiry date. Does this mean that future logons will not automatically recognize my $userID and take me straight into the next page? [Yes or No]

    2) If so, and the browser deletes the cookies upon exit, then doesn't that defeat the purpose of cookies in the 1st place? [Yes or No]

    Thank you!

  12. #12
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. Depends, read my previous post
    2. No, there's a reason that a lot of people use cookies.
    ck :: bringing chris to the masses.

  13. #13
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay.. here's what I've done so far:

    I've got a mainpage.php where I put the following cookie code at the top of the page before any HTML and HEAD declarations:

    PHP Code:
    <? setcookie("userID","$id_num"); ?> 
    <? echo $HTTP_COOKIE_VARS["userID"]; ?>

    So, further down this page, I have a Submit form, where the user's password is captured using $id_num. I then hit Submit, verify the p/w and proceed to the next pages. Later, reloading the mainpage.php doesn't see my $id_num being displayed.... am I doing this correctly?

  14. #14
    SitePoint Member
    Join Date
    Aug 2001
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you do not specify a expire date the browser deletes the cookie upon exit. You need to set the expire dater in the future sometime. ie if you want there login info stored for a week then set expire date to 1 week from today.
    1) Say I don't specify an expiry date. Does this mean that future logons will not automatically recognize my $userID and take me straight into the next page? [Yes or No]
    Yes, if they exit their browser then return to your site later.

  15. #15
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by thebobert
    Yes, if they exit their browser then return to your site later.
    No, it wouldn't: he didn't set an expiration date so it'll be deleted as soon as he closed the browser.
    ck :: bringing chris to the masses.

  16. #16
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this expiration date's in seconds, right?

    3600 <==> 1 hour?

  17. #17
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to do time() + 3600, because it's actually a unix timestamp (seconds since 1970) and time() returns the current timestamp.
    ck :: bringing chris to the masses.

  18. #18
    SitePoint Member
    Join Date
    Aug 2001
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) Say I don't specify an expiry date. Does this mean that future logons will not automatically recognize my $userID and take me straight into the next page? [Yes or No]
    Yes, if they exit their browser then return to your site later.
    ^ I meant if it was not set then when they leave it is gone.


    3600 seconds = 1 hour
    86400 seconds = 1 day
    604800 seconds = 1 week
    Microsoft’s Motto: Resistance is futile, you will be assimilated…

  19. #19
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heh. can i specify an exp_date that is indefinite? Say 3-4 months or so. Man.. that's a whole lot of numbers...

    3 months =
    3*30 days =
    3*30*24 hours =
    3*30*24*60 mins =
    3*30*24*60*60 secs = 7776000

  20. #20
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A year is a close to indefinite as is necessary. Just do something like this: time() + 3600*24*365 so you can easily see how long it is.
    ck :: bringing chris to the masses.

  21. #21
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    setcookie ("my_username"$usernametime()+(3600*24*365), "/"".domain.com"0); // set cookie for one year 
    Netscape requires that the www. be infront of a URL... so I put this at the top of all my pages:

    PHP Code:

    if(!strisrt("www"$HTTP_HOST)){
        
    header (Location$REQUEST_URI);

    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  22. #22
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't know where you go that, but Netscape doesn't require a www.
    ck :: bringing chris to the masses.

  23. #23
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, and the little dot, decimal thingy MUST be in front of your domain name... and NO http. amd NO www!!! The code I gave you is cross platform/browser safe as long as you have the WWW in front of the domain when you're viewing it.
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  24. #24
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What version NS are you talking about? I'm discussing 3.x-4.x... I don't have NS 6 yet.
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  25. #25
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Malaysia
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OKs.... I've got my code setup as follows:

    PHP Code:
    <? setcookie("userID","$id_num","time()+7776000"); ?> <!--cookie expires in 3 months -->
    Now, when I reload the main page, I see my userID output from:
    echo $HTTP_COOKIE_VARS["userID"];

    However, once I close the browser and reopen it, the userID doesn't appear under ECHO. Same thing happens if I try to access the main page opening a second IE window while the first isn't closed.....


Bookmarks

Posting Permissions

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