PHP cookie problem - www or without www
Browsers are the main culprit here, not PHP. They store by domain, and don't know that www
is a special case; from their perspective, www.mydomain.com
and mydomain.com
are different strings, and therefore have different security policies. However, there is something you can do.
When setting the cookie, use .mydomain.com
(with the leading dot). This will tell your user's browser make the cookie accessible to mydomain.com
and all subdomains, including www
. PHP's setcookie has the argument $domain
, but it's fifth on the list, so you may need to set $expire
and $path
to their default values in order to get at it.
setcookie('name', 'value', time()+3600, '/', '.mydomain.com');
For consistency, however, you may wish to consider rerouting all web traffic to a specific domain, i.e. send mydomain.com
traffic to www.mydomain.com
, or vice-versa. My vague knowledge of SEO (edit if incorrect) tells me that it's helpful so as not to have duplicate content, and it saves you all such authentication issues. Additionally, if you store assets on a subdomain, having cookies on there slows down traffic by having to transport it each time, so storing application cookies only on www
earns you that speed boost.
Here is a tutorial on how to accomplish such a redirect in Apache.
PHP Cookies not working
Try using setcookie with a a path specified, this used to catch me out, as it assumes the current path by default. Using / will make the cookie work for the whole domain
setcookie("Username", $username, time()+3600*24*30, '/');
PHP Cookie not working on redirect
Make sure that value in $_SERVER['SERVER_NAME'] is exactly link.nl and not www.link.nl. For browsers these are two different values so they create two different cookies.
PHP cookies problem, works in Firefox not in other browser
Check the cookie settings of the other browsers and if they're set to block all or empty on exit.
If the cookies work in one browser, but not another, you will need to make sure that the other browser is letting you set cookies in the first place.
Sometimes it will look like you can create the cookie, but then it will disappear or be deleted with each page reload.
Cookies from an iframe
It's also possible that because you're setting the cookies in an iframe, that the browsers may view it as a third-party cookie and reject it unless explicitly set out in the browser preferences to allow third-party cookies.
In that case you would need a compact privacy policy (or a compact P3P header) on the pages from where you're trying to set the cookies from.
For PHP, you would add this as your header for the page setting the cookie:
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
PHP Cookies problem... cookie working on one page but not working on another
Are the different pages on the same domain? You should also be setting a path, I have found cases when the path was not set to '/' then it would not be accessible by default 'everywhere' on the site even though that would make sense as the default.
Try setting the path of the cookie.
Related Topics
PHP Include Best Practices Question
How to Get All Class Names Inside a Particular Namespace
Redirecting to Authentication Dialog - "An Error Occurred. Please Try Again Later"
How to Preview an Image Before and After Upload
Android JSON Httpclient to Send Data to PHP Server with Httpresponse
Understanding MVC: Whats the Concept of "Fat" on Models, "Skinny" on Controllers
How to Convert a String to JSON Object in PHP
A Better Approach Than Storing MySQL Password in Plain Text in Config File
How to Use Class Methods as Callbacks
Is It Recommended to Store PHP Sessions in Memcache
Algorithms for String Similarities (Better Than Levenshtein, and Similar_Text)? PHP, Js