I'm trying to create a php cookie using
setcookie('usrid', $user_id, time()+3600);
When I checked this with the browser, the cookie is set with the correct value passed with the variable. But I couldn't retreive the value using $_COOKIE['usrid']
I tried to delete the cookie using setcookie("usrid", "", time()-3600);,
but its not getting deleted.
Now when I try to get the value of cookie, it shows the value.
Can anyone tell why is this happening?
Set Cookie
Reload page
Read Cookie
Finally I found the solution.
The path parameter was missing in setcookie function. When I set the path to "/", it worked.
Why it didn't work before is that I didn't provide the path parameter, so the cookie was only accessible from the path it was created. By setting the path parameter as "/", the cookie is accessible from any path of the domain.
Below is the code.
setcookie('usrid', $user_id, time()+3600, "/");
Check your php version, do something like this to get the value in your cookie:
$getCookie = ((int)phpversion() >= 5) ? $_COOKIE['usrid'] : $HTTP_COOKIE_VARS["usrid"];
Related
(PHP) I set the cookie in my login.php page in this way:
setcookie('cookie_id',$id);
I print the cookie and I see the correct value but when I change page with:
header($login_url);
I lose the all cookie and I don't know why. Anybody can help me?
You have to specify / as path in setcookie() function, so cookie will be available on every path of your site. To do this:
setcookie('cookie_id', $id, 0, '/');
Note that third argument is expire time which is set to 0 as default. According to documentation it means that:
If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
If you have human urls or subfolders (like www.domain.com/path1/path2/), then you must set cookie path to / to work for all paths, not just current one.
setcookie('cookie_id', $cookie_id, time() + 60*60*24*30, '/');
From PHP manual:
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.
I am facing some trouble with a conditional statement that uses cookies in PHP.
I would like to change the state of an image based on whether a cookie is set or not. Here is my code (in file MAIN.PHP):
$cookie = "d_content_vote_".$databaseArray['id'];
if(!isset($_COOKIE[$cookie])) {
// display image 1 if cookie is not set
}
else {
// display image 2 if cookie is set
}
The cookie value (of the timestamp) is set in ../INCLUDES/RATING.PHP, and I make an ajax call to that file. In order to debug, I did a print_r($_COOKIE) in RATING.PHP which gave me this:
Array
(
[d_content_vote_1] => 1402726678
[d_content_vote_4] => 1402727148
[PHPSESSID] => effa8778efbe1b3dfb5bb301e359997d
)
However, when I do a print_r($_COOKIE) in MAIN.PHP I do not get the d_content_vote_* cookies, only the session info.
How do I transfer the cookie that is set in rating.php so that I can use it in main.php. I have never faced this problem before.
[Additional info: I'm building the site on a MAMP server now]
I realised that my cookie was being set and the print_r was done in a file in a subdirectory (/includes) and therefore cannot be used in the root directory. In order to make it work in the root directory, I needed to add another attribute to the function:
setcookie($name, $value, $time, "/");
The last parameter "/" ensures that the cookie can be used in the root directory.
What about the cookie remove when a user make the log-out process? In case we use 4 parameters when setting the cookie, do we need to adopt the 4 parameters both in the log-out process?
setcookie($NomeCookieLogOut, "", time()-3600);
OR
setcookie($NomeCookieLogOut, "", time()-3600, "/");
Is there any difference between setting a cookie via setcookie() and $_COOKIE ?
Sometimes,when setting a cookie via setcookie,i don't get the value of that cookie via $_COOKIE['cookie_name'].But js console.log immediately after setcookie,shows that cookie is set but if i try to get the value of the cookie via $_COOKIE,i don't get the updated value.
I'm confused..!!
You can't actually "set" a cookie with some code like this:
$_COOKIE['cookie'] = $my_var;
All this does is add a new value to the $_COOKIE array. No Set-Cookie HTTP header is sent back to the client (browser) in the response and no cookie will be created on the client.
Use the setcookie() function to set cookies.
The current accepted answer correctly points out that $_COOKIE is set/initialized at the start of the PHP process and isn't updated after that. You can update it yourself but don't expect that value to stick on the next request.
In setcookie function you can only set the cookie name.
If you want to get that cookie value then you can take it via the $_COOKIE['name']
Be sure that when you create cookie you need to set domain name in setcookie function as well.
In PHP, we can set a cookie with the function setcookie(). The syntax of the function is
setcookie(name,value,expire,path,domain,secure)
For example, setcookie('name',$name,0,'/');
will create a cookie named name with value of the variable $name in the root directory '/'. Inorder to access the cookie, we can use $_COOKIE['cookiename'];
With setcookie you can only set cookie in php :
setcookie("myCookie", $value, time() + 3600);
But if you want to get or use that cookie you can use $_COOKIE, like if you want to get some cookie value use:
echo $_COOKIE['cookie_name'];
This PHP code is supposed to write to a file in a folder specified by a cookie:
$user = $_COOKIE["username"];
if( $xml = file_get_contents("$user/docs.xml") ) {
But it just says the file /docs.xml (not specifying the folder) just the file and not the cookie value, apparently doesn't exist, because it's not getting the cookie but why?
Could it be that I'm trying to get it from a different domain?
Try setting the cookie like so
setcookie("Name", "Value", $time, "/");
The / at the end make sure the cookie works throughout the whole site, not just the folder where it was set.
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!