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'];
Related
I'm trying to access a cookie's value (using $_COOKIE) immediately after calling the setcookie() function in PHP. When I do so, $_COOKIE[$cookiename] isn't set after login. Why?
if(isset($_SESSION)){
$_SESSION['email']=$username;
$_SESSION['id']=$query['id'];
$_SESSION['name']=$query['name'];
$_COOKIE[$cookiename]=$query['name'];
$cookiename="user";
$cookie_value = "John Doe";
setcookie($cookiename,$cookie_value, time() + (86400 * 30),"/");
if (!$session->Check()){
echo $cookiename ;
}else {
echo $_COOKIE[$cookiename];
}
This is because when you call setcookie() you are attaching the cookie to the current response(when the current request completes).
$_COOKIE will have a value when a request is made with the cookie header already set i.e cookie is sent to the server.
So, when only #1 happens you don't have anything in $_COOKIE, but once #2 happens you will have a value in $_COOKIE
EDIT: Almost everything you need is mentioned in the manual
http://php.net/manual/en/function.setcookie.php
setcookie() defines a cookie to be sent along with the rest of the
HTTP headers Once the cookies have been set, they can be accessed on
the next page load with the $_COOKIE array. Cookie values may also
exist in $_REQUEST.
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"];
I'm exploring the source code of Kohana framework, and it has the following logic when deleting the cookie on browser:
public static function delete($name)
{
// Remove the cookie
unset($_COOKIE[$name]);
// Nullify the cookie and make it expire
return setcookie($name, NULL, -86400, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
}
I understand that the function setcookie will set cookie's name to deleted (as displayed in browser cookies view) and expire it so that the browswer doesn't send it next time. So why is the first part with unset is there?
setcookie add cookie to HTTP response headers. Whereas $_COOKIE presents cookies from request headers. So setcookie doesn't affect on cookies of $_COOKIE array (on the current page load). Therefore we have to unset cookie in $_COOKIE too in order to ensure that this cookie won't present in $_COOKIE array if we'll want to get it in further (on the current page load).
both are same and deleting cookie. If we remove unset, Surely it will work and delete the cookie.
Let's say the user logs in, a cookie is sent and the user is redirected to another page.
How do I make it where I can convert the cookie value into a variable in PHP (on the page they are redirected to)?
I want to write the cookie value to a .txt file.
Thanks.
From the docs:
Any cookies sent to you from the client will automatically be included into a $_COOKIE auto-global array if variables_order contains "C".
Well cookies live in the $_COOKIE global. (When received.)
And writing them to a file would be as boring as:
file_put_contents("var/cookie.txt", $_COOKIE["cookiename"]);
You don't need to convert them just retrieve into a php variable.
<?php
if(isset($_COOKIE['lastVisit']))
$visit = $_COOKIE['lastVisit'];
else
echo "You've got some stale cookies!";
echo "Your last visit was - ". $visit;
?>
see here tutorial
Cookies stored in the $_COOKIE array. So if you want to use it, just assign an appropriate cookie key value to your variable, for example:
$name = $_COOKIE['name'];
Cookies can be accessed in the global $_COOKIE array.
Use this:
$newvar = $_COOKIE['cookiename'];
Cookie is available in $_COOKIE as $_COOKIE['key']; get that value and store it in a variable, which can be output to a file.
I am trying to set a cookie for a site if it does not exist. It is not working.
if(isset($_COOKIE['about'])){
$_COOKIE['about'] += 1;
}
if(!isset($_COOKIE['about'])){
setcookie("about", 1, time()+3600);
}
I have also tried
if(empty($_COOKIE['about'])){
setcookie("about", 1, time()+3600);
}
The $_COOKIE superglobal is only available for you to read values from. Writing to it does not update the cookie, since that requires a new Cookie header to be sent to the browser. You would probably be better served by sessions backed by cookies, since PHP allows you to modify the session without explicitly saving/setting the cookie.
You can only read stuff from the $_COOKIE superglobal, try setting it normally:
setcookie("about",$_COOKIE['about']+1,time()+3600);
So all together:
if(isset($_COOKIE['about'])){
$_COOKIE['about'] += 1;
}else{
setcookie("about", 1, time()+3600);
}
Note the else, you've checked before if the cookie isset, so there is no need to check again as either it is or it isn't.
Make sure you have not sent any information to the user yet as the setcookie call is just an alias to header() (but with a specific schema to follow). You may have error output disabled and are missing the message, so it appears to work but is failing in the background.
setcookie should be one of the first calls on your page, up there with starting a session and setting a header.