PHP setcookie for 1 Year will not work

后端 未结 5 1133
慢半拍i
慢半拍i 2021-01-07 22:32

I\'m trying to set a PHP cookie to expire 1 Year from when the user logged in, and I\'m doing it this way:

setcookie(\"myCookie\",\'exampleUserName\',(365 *          


        
相关标签:
5条回答
  • 2021-01-07 23:02

    The third param is the time in future that mean time() + time in seconds. Your 365 * 24 * 60 means 1971 ( 1970 + 1 ), this is in the past. Cookie in past will not be used - people set cookie to the past time to clear cookie. Do exactly like Shankar.

    0 讨论(0)
  • 2021-01-07 23:03

    Do like this...

    setcookie("myCookie",'exampleUserName',time()+31556926 ,'/');// where 31556926 is total seconds for a year.
    
    0 讨论(0)
  • 2021-01-07 23:20
    setcookie($cookie_name, $cookie_value, strtotime("+1 year"));
    
    0 讨论(0)
  • 2021-01-07 23:22

    Try this:

    <?php
           setcookie("TestName", "Test Value", time()+3600 * 24 * 365);
    ?>
    
    >> Here 'TestName' is name of cookie.
    >> "Test Value" is value to store.
    >> time()+3600 * 24 * 365 - will set cookie time till 1 year.
    

    Thanks!

    0 讨论(0)
  • 2021-01-07 23:23

    try this one

    setcookie($cookie_name, $cookie_value, time() + ( 365 * 24 * 60 * 60));
    
    0 讨论(0)
提交回复
热议问题