Why when set php cookie path to "/" doesn't work for every subdirs in the domain, but just for the current directory.
cookie is set like:
setcookie("name", "val", expire_time, "/");
It just doesn't want to work.
try including the domain parameter:
setcookie("name", "val", expire_time, "/", ".domain.com");
// don't forget the prefixing period: .domain.com
that will enable all sudomains of "domain.com"
Are you testing on localhost? In that case, you need to pass null as the value for $domain.
Setting the cookie path to / should make it available to the entire domain. If you set your cookie like that, and it isn't being sent, there is something else wrong.
Try using the Web Developer addon in Firefox. It shows you details on the available cookies. Maybe that can help you diagnose the problem.
Late to the party, I know. But I just discovered that my issue was pretty stupid, but I'll post it for completion:
I was neglecting to add time() to the expires time on the cookie, so it was expiring immediately.
The expires time should be time() + seconds
Related
Can anyone tell me how to read cookie from subdomain. I can set the cookie for subdomain but unable to read it.
Please tell me what is the syntax in PHP to read cookie from subdomain.
If you're enabled to read in other subdomain, it's probably because you didn't properly set the cookie to begin with.
$date_of_expiry=time()+ 3600;
setcookie( "cookie_name", "cookie_value", $date_of_expiry, "/", "example.com" );
It's important that you put the last argument for the cookie to be available in other subdomains.
Also, the cookies must be set before page output.
I have:
mydomain.com (which is the portal of the game, global setting and stuff)
game.mydomain.com (which is the the actual game)
The problem is that I want to set a cookie that is available globally, on game.mydomain.net, mydomain.net (and whatever subdomain i'm going to create in the future).
I've been trying to set the cookie from another subdomain as I've read that subdomains can set cookies to parent domains but not vice versa (which is wierd and I guess I've read it wrong). Whatever, so I've done another account.mydomain.com (from which I'm making an ajax call form mydomain.net so the user can authenticate) and I'm using
setcookie('session', $value, time() + 2592000 (one month), '/', '.tribul.net');
Then, return the success message and refresh the main page on mydomain.net so it can read the new cookie value.. problem is, there's no cookie set. I've also been trying to set the cookie from mydomain.com (as .tribul.net) so it can be avaialable on all subdomains but it's available only on the main domain. What's wrong?
I need to connect all subdomains and the domain to the same cookie, TO BE NOTICED, I am setting the cookie in a backend file named process.php (placed in account.domain.com) as result of an ajax request.
Try this setcookie('session', $value, time() + 2592000 , '', '.tribul.net');
In php.ini:
session.cookie_path = /
session.cookie_domain = ".mydomain.com"
Set Cookie:
setcookie('session', $value, time() + 2592000, '/', 'mydomain.com');
I used Klaus Hartl's jquery cookie plugin in order to use my problem since I haven't been able to set up a global cookie from the ajax backend.
I'm making another project and I'm using cookies to store some unimportant login information.
My problem is that if I go to "www.domain.com", the cookie can be accessed, however, if I go to "domain.com", the cookie cannot be accessed.
I'm writing my project in PHP so if anyone has the answer, or a solution to this problem that would be great.
Thanks,
Mark.
When you set the cookie, set it as .domain.com.
ini_set('session.cookie_domain', '.site-name.com');
or
setcookie ("cookie_name", "", time() + 3600, "/", ".site-name.com", 1);
I'm just trying to set and use a cookie but I can't seem to store anything.
On login, I use:
setcookie("username", $user);
But, when I use Firefox and the Web Developer plugin Cookies -> View Cookie Information There is no username cookie.
Also, when I try to access the value from a subsequent page using
$_COOKIE["username"]
It is returning null/empty
var_dump(setcookie("username", $user));
RESULT: bool(true)
and
var_dump($_COOKIE)
RESULT: specific cookie does not exist (others are there)
I have done some more testing...
The cookie exists after login (first page) but disappears when I go to another (2nd page) and is lost for good...
Are there any headers that must be present or not present?
http://php.net/manual/en/function.setcookie.php
Try setting the $expire parameter to some point in the future. I believe it defaults to 0, which is in the distant past.
Make sure that you are setting the domain parameter correctly in case the URL is changing after you go to another page after login. You can read more about the domain parameter on http://php.net/manual/en/function.setcookie.php
The cookie is probably expired because $expire defaults to 0 seconds since the Unix epoch. (docs)
Try
setcookie("username", $user, time() + 1200);
which expires 20 minutes after set (based on the client's time).
Use var_dump() on setcookie(..) to see what is returned. Also might do the same to $_COOKIE to see if the key is set.
Thanks everyone for the feedback... Aditya lead me to further analyse the cookie and I discovered that the path was the issue...
The login path was /admin/ and then I was redirecting back to the root...
Thanks all for your help and feedback!
Ok I have a cookie set, and I can clearly see it if I go to private data in Firefox... ok so when I echo it on one page in a certain directory it works, (www.example.com/dir), but on the index page of the site (www.example.com), it wont echo, it says the cookie is not set. Yes I have cookies enabled, yes I tried clearing cache and all that. Any ideas? PHP btw
Which directory are you in when the cookie gets set?
From the PHP manual on setcookie(), emphasis mine:
Path
The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain . If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain . The default value is the current directory that the cookie is being set in.
Cookies can be bound to a specific domain, subdomain, path, and protocol (http/https). You need to specify the path when setting the cookie in PHP:
setcookie("TestCookie", "Value", time()+3600 , '/' );
The fourth parameter binds it to the root of the site and it will be available in any subdirectory of the main site.
If you want it available on the main domain and any subdomain, supply the fifth parameter like this:
setcookie("TestCookie", "Value", time()+3600 , '/', '.example.com' );
Now it will be readable at:
www.example.com
example.com/newdir
awesome.example.com/newdir
You need to check the path that the cookie is being set.
If it's not '/', there's your answer!
Yes try this, I was also facing this problem but resolved by below code.
setcookie("TestCookie", "Value", time()+3600 , '/' );
Set your path option; the default value is the current directory that the cookie is being set in. Because you're setting the cookie in the directory /dir , its only available within that directory or below it.
You get around this by explicitly setting the path, ie.
setcookie(name,value,expire,path,domain,secure)
Set the path to "/".
setcookie("Cookie_name", "Cookie_Value", time()+3600 , '/' );
fourth parameter ('/') will make your cookies accessible to pages in parent directories.
You need to set the $path to / in setcookie(), if you want to access it in all directories
Cookies Must Be Set Before Page Output !!!
Since cookies are sent by the script to the browser in the HTTP headers, before your page is sent, they must be set before you even send a single line of HTML or any other page output. The moment you send any sort of output, you are signalling the end of the HTTP headers. When that happens, you can no longer set any cookie. If you try, the setcookie() function will return FALSE, and the cookie will not be sent.
setcookie('cookie_username', $cookie_username, time() + (86400 * 30), "/"); // 86400 = 1 day, '/' denotes cookie available in entire directory.
and in another page:
$username = $_COOKIE['cookie_username'];
also make sure that browser is not blocking cookies.
If you want to use cookies in sub domain also:
setcookie('cookie_username', $cookie_username, time() + (86400 * 30), "/", ".subdomain.com"); // 86400 = 1 day, '/' denotes cookie available in entire directory.