Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP setcookie for 1 Year will not work

Tags:

php

cookies

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 * 24 * 60) ,'/');

The problem is, when I view the cookie using the console in Chrome or Firefox, it show Expires Sun, 05, 2014 in Chrome and Expires = Session in Firefox.

Any other site like Google or stackoverflows cookies show the correct expiration date.

How can I set this right?

like image 321
jmenezes Avatar asked Dec 30 '13 05:12

jmenezes


People also ask

Why is Setcookie not working PHP?

The PHP isset() checks whether a cookie is set. Reminder: if you detect PHP setcookie not working, make sure it appears before the <html> element in your code, and that the set path parameter is correct.

How do you set a cookie for one year?

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.

How long does a cookie last PHP?

PHP Create/Retrieve a Cookie The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie is available in entire website (otherwise, select the directory you prefer).

How do I set never expiring cookie?

Syntax: document. cookie = "cookieName= true; expires=Tue, 19 Jan 2038 04:14:07 GMT"; // OR const cookieName = "something"; const cookieValue = "something"; const daysToExpire = new Date(2147483647 * 1000).

Why PHP setcookie did not work as expected?

Why the php setcookie did not work as expected? In this case, the cookie is set to expire in short time. However, the specified expire time will be compared with the local time of the computer on which the browser is running, not the server time.

How do I know if a cookie has expired PHP?

Note that php also sets the value of the cookie to “deleted”, not “”. Browser uses the “expires” value instead of the value of the cookie to determine whether the cookie has expired or not. Note also that as long as you set a past expiration time, even 1 second ago, the same Set-Cookie will be sent to the client.

How do I set the SameSite cookie value in PHP?

As of PHP 7.3.0 the setcookie () method supports the SameSite attribute in its options and will accept None as a valid value. For earlier versions of PHP, you can set the header () directly: header ('Set-Cookie: cross-site-cookie=bar; SameSite=None; Secure'); up.

What is setcookie () function in http?

The setcookie () function defines a cookie to be sent along with the rest of the HTTP headers. A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too.


2 Answers

setcookie($cookie_name, $cookie_value, strtotime("+1 year"));
like image 183
Shawn Rebelo Avatar answered Oct 12 '22 23:10

Shawn Rebelo


Do like this...

setcookie("myCookie",'exampleUserName',time()+31556926 ,'/');// where 31556926 is total seconds for a year.
like image 25
Shankar Narayana Damodaran Avatar answered Oct 13 '22 00:10

Shankar Narayana Damodaran