The cookie time created when I close the browser expires

Posted on

Question :

I’m here asking why unfortunately I’ve tried all the alternatives in polls since last week. And I’m probably doing something wrong.

Come on:

    setcookie("email", "email", time() + (360 * 24 * 3600));

This is the code that I created the cookie. That is in the header before reading the html.
For the cookie to last 1 year.

However, the other day he inserted this cookie again. I make a check to see if it exists and then I enter the record in the database, to have control:


    setcookie("email", "email", time() + (360 * 24 * 3600));

    mysql_query("INSERT INTO db(id, email, data) VALUES (0, 'email', NOW() )")


I do not know the reason for the error. Maybe I have to configure php.ini, or something else. But I already researched and did not find the error. This is the first time I work with cookies.

Who can help me, I thank you.


Answer :

In the setcookie there is a field called path. That serves to store the cookie (as I understand it) on the server. I do not know how it works but that’s it:

setcookie("email", "email", time() + (360 * 24 * 3600), '/');

that bar is where the local path is, and indicates where it can be rescued.



The path on the server where the cookie will be available. If set to ‘/’, the cookie will be available for the entire domain. If set to the ‘/ foo /’ directory, the cookie will only be available inside the / foo / directory and all subdirectories like / foo / bar in the domain. The default value is the current directory where the cookie is being configured.


I believe that as long as you do not enter the cookie path, it will remain available until the browser is closed. It does not matter if it is 1 year.

In addition, you can indicate the domain in which you want it to be redeemed:

setcookie("email", "email", time() + (360 * 24 * 3600), '/', '');



The domain for which the cookie will be available. Configuring the domain for ‘’ will make the cookie available in the www subdomain and in the top subdomains. Cookies available for a lower domain such as ‘’ will be available for higher subdomains, such as ‘’. Older browsers still implement RFC 2109 and may require one. at the beginning to work with all subdomains.

Thanks to everyone who helped me in the comments.


Leave a Reply

Your email address will not be published. Required fields are marked *