I cannot access cookies from any page other than the page they are set on. I looked at print_r($_COOKIE)
on different pages and the only common variable between pages is the $_COOKIE['PHPSESSID']
.
I am developing on a local XAMPP testing server. Is there a setting I should change on the PHP.ini or is this normal behavior for cookies? Sorry, I'm a little new to this stuff and I was under the impression cookies were accessible site wide.
I am setting cookies like:
setcookie("user", "Dave Schmave", time()+60*60*24*120);
Any help would be greatly appreciated. Thanks
Why you are having this problem. The problem comes from the fact that setcookie() doesn't set the cookies immediately, it sends the headers so the browser sets the cookies. This means that, for the current page load, setcookie() will no generate any $_COOKIE .
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.
PHP Cookies Checking if a Cookie is SetUse the isset() function upon the superglobal $_COOKIE variable to check if a cookie is set.
Cookies are always stored in the client. The path only sets restrictions to what remote pages can access said cookies. For example, if you set a cookie with the path "/foo/" then only pages in the directory "/foo/" and subdirectories of "/foo/" can read the cookie.
Try setting the cookie path to the root:
setcookie("user", "Dave Schmave", time()+60*60*24*120, '/');
Also is it on the same domain? Accessing via HTTPS will also affect your cookies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With