SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot bkflash's Avatar
    Join Date
    Apr 2006
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Set cookies to expire at end of day

    Hello, im having a problem thinking how to work out this problem, im setting cookies in php like this simple bit of code:

    Code:
    setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
    thats set to expire in 1 hr, i want to have php some how find out how many hrs are left in the day and then put that into the cookie so when the clock strikes midnight it will go away, does anybody no how to get php to do this?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    setcookie("TestCookie"$valuemktime(2400) - time()); 

  3. #3
    SitePoint Zealot bkflash's Avatar
    Join Date
    Apr 2006
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so that will expire at the end of the day?

  4. #4
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes.

  5. #5
    SitePoint Zealot bkflash's Avatar
    Join Date
    Apr 2006
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sweet thanks

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The mktime() bit says "The time at 24:00:00 today" (i.e. next midnight) and the time() bit is the time now, so the result of the subtraction is the time between now and midnight.

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I use this it doesn't set a cookie at all. Any suggestions as to what might be wrong?

  8. #8
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Does your browser accept cookies? Are you doing this before outputting any HTML or whitespace? Cookies are set when headers are sent so you can't set a cookie after outputting any HTML.

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Raffles, when I use the following code it works.

    Code:
    setcookie ("name", yes, time()+60*60*12, "/", ".mysitename.com")
    When I replace time()+60*60*12 with mktime(24, 0, 0) - time() it doesn't even set a cookie.

    I've been trying to make it expire at midnight so it would be great if you could point out what I've done wrong.

  10. #10
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's because mktime(24,0,0) - time() is a few hours after midnight on the first of January 1970. So if it's a time in the past, the cookie isn't set.

    The correct thing is simply mktime(24,0,0) without time() being subtracted. Got it wrong the first time, forgot cookies need the actual time, not the time from now. I guess I'd better let bkflash know about this...

  11. #11
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, thanks.

  12. #12
    SitePoint Member
    Join Date
    May 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forgive my ignorance, but would that code not be using the time from the server and not from the client? Time zone differences could cause strange behaviour.

    Cheers
    Mike

  13. #13
    SitePoint Addict pkSML's Avatar
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    230
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mcmunt View Post
    Forgive my ignorance, but would that code not be using the time from the server and not from the client? Time zone differences could cause strange behaviour.

    Cheers
    Mike
    Good point, mcmunt. PHP is server-side scripting, whereas Javascript is client-side.

    You'll need to use Javascript to use the client's time.
    See http://stephen.calvarybucyrus.org/se...ipt+set+cookie
    -Stephen

    Get a LitlURL to this page!


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
  •