Search code examples
phpsetcookie

Set a cookie to the entire domain from a page inside a folder


I'm a begginer in cookies and stuff like that.

I need to set a cookie to the entire domain from a page inside a folder like mydomain.com/folder/page.php. I had to do this before and I got it done redirecting to another page mydomain.com/another.php which set the cookies. The thing is now I should not redirect the page because of some data coming from a form and other things, so how could I do this?

setcookie("name", $name, time()+31536000); will set the cookies only for the pages inside folder and I tried setcookie("name", $name, time()+31536000,'/','mydomain.com'); but it didn't worked.


Solution

  • This code definitely works:

    setcookie("name", $value, time()+31536000,'/');
    

    You should take note that mydomain.com is different from www.mydomain.com or sub.mydomain.com.

    Other than this, take note that the value of $value should be a string.