Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My cookies are only available on PHP pages they are set on, is this normal?

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

like image 235
Jiminy Cricket Avatar asked Mar 13 '12 06:03

Jiminy Cricket


People also ask

Why is my cookie not set in PHP?

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 .

What is set cookie in PHP?

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.

How do you check if cookies are set PHP?

PHP Cookies Checking if a Cookie is SetUse the isset() function upon the superglobal $_COOKIE variable to check if a cookie is set.

Where are PHP cookies stored?

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.


1 Answers

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.

like image 190
Ben Rowe Avatar answered Oct 19 '22 14:10

Ben Rowe