setcookie(name, value, expire, path, domain, secure, httponly);
When I try to echo $_COOKIE['name'], it print a blank
This should work for you:
<?php
if(!isset($_COOKIE['action'])) {
$cookie_value = "menuopen";
setcookie("action", $cookie_value, time()+3600, "/", ".acvd.com");
}
if(!empty($_COOKIE['action']))
echo $_COOKIE['action'];
?>
You have to look that your using the right name of the cookie also that acvd.com is your domain! And you can't have a secure connection but httponly!
<?php
$cookie_value = "menuopen";
if(!empty($cookie_value))
{
setcookie("action", $cookie_value, time()+3600, "/", "acvd.com", 1, 1); ?>
}
?>
I hope it will work for you.
first Check the cookie name you have used during setcookie method
You can also check your cookie value in browser following are the steps:
1 open debugger tool
2 go to resource tab
3 then go to cookies
4 then go to your domain name
there you will find the cookie name with corresponding value, check for your name is it there and what is the name whether it is name or action.
if name then echo $_COOKIE['name']
if action then echo $_COOKIE['action']
Related
when i try to create a cookie with PHP they are not saved when the website is online.
When i'm on the localhost, its all okay.
For example:
setcookie('user_register', 'username', time() + 3600, '/');
How can i troubleshoot this? Is there any config in cpanel to disable cookie creation ?
Thankyou
You say it works on localhost, which alerts me to think that you have placed the setcookie below output like this for example:
<?php
// top of file
Echo "saving cookie";
setcookie('user', 'username', 3600, '/');
?>
This may work on localhost but will always fail on a real php server.
Setcookie is a header and must be placed before any output as stated in the manual http://php.net/manual/en/function.setcookie.php
To make the code above work you need to chaange place of the echo and setcookie so that it first sets the cookie and then outputs the text:
<?php
// top of file
setcookie('user', 'username', 3600, '/');
Echo "saving cookie";
?>
Then to read the cookie you need to go to a new page or refresh the page.
Cookies can't be set and read at the same time in php.
So to complete the code we can do like this:
<?php
// top of file
If($_COOKIE(['user']){
Echo $_COOKIE(['user']);
}Else{
setcookie('user', 'username', 3600, '/');
Echo "saving cookie";
}
?>
Now you can set the cookie and read it on the same page if you refresh the page.
Cookies are used next time you visit the page. So there are 3 general solutions: 1. Save it to cookie and for first time, just echo POST variables instead of COOKIE. Code will look like this:
setcookie('user', $_POST['user'], time()+3600); // notice time() function
setcookie('pass', $_POST['pass'], time()+3600); // you cant use absolute value here
if (!isset($_COOKIE['user'])) echo $_POST['user'] . ' ' . $_POST['pass'];
else echo $_COOKIE['user'] . ' ' . $_COOKIE['pass'];
, but if you really want to store password in cookie (very bad idea), then at least hash it. Code with hashing could look like this:
setcookie('pass', hash(whirlpool/*algorithm*/, $_POST['pass']/*text*/), time()+3600);
, then when you check the password, just hash it and compare hashes.
You cannot send and receive cookie in the same request. Here is a modification you should try
if(isset($_POST['user']) && isset($_POST['pass'])){
setcookie("user",$_POST['user'],3600);
setcookie("pass",$_POST['pass'],3600);
header("location:".$_SERVER['PHP_SELF']);
}
if(isset($_COOKIE['user']) && isset($_COOKIE['user']))
{
echo $_COOKIE["user"] . " " . $_COOKIE["pass"]; // This is line 28
}
However, it advisable to use sessions instead. Start by going through PHP_SESSION
I am trying to set a cookie like so:
<?php
$cookie_name = "user";
$cookie_value = "James";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<?php if(!isset($_COOKIE['user'])) { ?>
//Do Something
<?php } ?>
but Its not setting, I am using chrome and when I check my cookies, they are not there :(
From the setcookie documentation:
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.
You will not be able to immediately access cookies on the initial page load. Additionally, make sure that you are not outputting any HTML before setting the cookie (protocol restriction).
If you absolutely need to access the cookie immediately, you have a few options:
After setting the cookie, reload the page immediately with header
If this is not what you want to do, you can use JavaScript to set cookies, but you should note that some people disable JS in their browsers. I would not recommend this solution, personally.
$_COOKIE[] variable is set when the page loads, due to the stateless nature of the web.
Here's what the documentation says:
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.
But there are a number of word arounds to access it immediately after setting it.
you can manually set the value for $_COOKIE[] right when you set the cookie to access it or you could use an intermediate variable like so:
<?php
$cookie_name = "user";
$cookie_value = "James";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
$_COOKIE[$cookie_name] = $cookie_value;
?>
if(!isset($_COOKIE['user'])) will return true now.
Another less efficient solution world be to reload the page right after setting the cookie.
like:
$cookie_name = "user";
$cookie_value = "James";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
header("Location: your_script.php");
?>
When the page reloads, if(!isset($_COOKIE['user'])) will return true again.
Hope this helps! :)
As said in the comments, the cookie will not be visible within the same request that set it. You need to do a redirect in order to access the cookie, e.g.
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
header('Location: ' . $your_script_location_path);
exit;
Also, the code inside your check
if(!isset($_COOKIE['user'])) {
// Do something
}
will only get executed when the cookie is not set.
can someone please how what hell this
cookie stops at 2 ?
<?php
if (isset($_COOKIE["count"]))
{
$cookie = ++$_COOKIE['count'];
}
else {
echo "Welcome guest!<br>";
setcookie("count", 1, time()+3600);
}
ECHO $cookie;
?>
thank you all
$cookie = ++$_COOKIE['count']; is only called once. If $_COOKIE[count] has a numerical value, $cookie will store that value plus 1.
Also, the following is not strictly correct:
echo "Welcome guest!<br>";
setcookie("count", 1, time()+3600);
You cannot call an echo before a header. I recommend you change it to this:
setcookie("count", 1, time()+3600);
echo "Welcome guest!<br>";
You cannot change cookie value by incrementing $_COOKIE[xxx], you have to use setcookie() function for that. This will work:
<?php
$cookie = isset($_COOKIE["count"]) ? $_COOKIE["count"] : 0;
setcookie('count', $cookie + 1, time()+3600);
ECHO $cookie;
You cannot update a cookie that way. However you can overwrite it.
See setcookie for more info.
If you set a cookie, it won't retrieved until the next request and so the data won't be present in $_COOKIE.
So setting a cookie and accessing it cannot be in same instance. You need to redirect or refresh after setting it.
Simply use setcookie() to increment.
to increment you place the ++ after the string, not before it.
setcookie("test","sonali",time()+3600,"../php/"," ",false,false);
echo $_COOKIE["test"];
Notice:Undefined index:test in
D:\projects\Trainee2014\Sonali\php\cookiedemo.php on line 3
if possible please give me solution
<?php
setcookie("test","sonali",time()+3600,"../php/"," ",false,false);
$check = $_COOKIE["test"];
if (isset($check)){
echo 'Cookie not set';
}
else{
echo 'Cookie set';
}
?>
Try the above code to handle the notice.
Change your code to this and try,
setcookie("test","sonali",time()+3600,"../php/"," ",false,false);
echo isset($_COOKIE["test"]) ? $_COOKIE["test"] : '';
Problem is at the first time $_COO KIE["test"] is not set. so you need to refresh. in order to avoid this problem you should simply use the if condition with isset().
Try like this:
Syntax setcookie(name,value,expire,path,domain,secure);
Use like this in page :
<?php
$value = "cookie value";
// send a simple cookie
setcookie("user",$value);
OR
// send a cookie that expires in 24 hours
setcookie("user",$value, time()+3600*24);
?>
Access it on page like this :
<?php
echo $_COOKIE["user"];
OR
echo $HTTP_COOKIE_VARS["user"];
// Print all cookies
print_r($_COOKIE);
?>
Try this :
setcookie("test","sonali",time()+3600,"../php/",false,false);
print_r($_COOKIE);
echo $_COOKIE["test"];
Please set setcookes function only 5 parameter
setcookie(name, value, expire, path, domain);
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 '/php/', the cookie will only be available within the /php/ directory and all sub-directories such as /php/../ of domain. The default value is the current directory that the cookie is being set in.
Ref: http://php.net/setcookie
setcookie("test","sonali",time()+3600,"/"," ",false,false);
echo $_COOKIE["test"];
Is it possible to remove cookie that was set on front via JS with PHP?
I'm doing this:
*FRONT (JS):
if ($.cookie('myCookie'))
{
console.log('Cookie.. :( ');
}
else
{
console.log('Yaay! No cookie!');
$.cookie('myCookie', '123');
}
BACK (PHP):
if (isset($_REQUEST['removeCookie']))
{
setcookie("myCookie", "", time()-3600);
unset($_COOKIE['myCookie']);
}
Result:
Seems like it's a mistery
You can't force the browser to delete the cookie file. You can, however, delete the contents of the cookie and expire it. Which is exactly what you're doing with your code above. I would probably tweak it slightly:
setcookie('myCookie', '', 1, '/'); // no need to calculate one hour ago.
If client time is wrong, setting cookie expire with time() function may not work as expected.
To unset cookie try,
setcookie('myCookie', 'blah blah', 1);
Source: A Comment in php setcookie docs
It would depend on the users PC deleting the cookie after the timeout. Personally I wouldn't trust that. I'd set the cookie to an empty value, or set it to DELETED then in your test code check if it is set and then check if the value is not blank or not DELETED
To completely remove cookie from browser by PHP, Try this code
$name = 'exists_cookie';
unset($_COOKIE[$name]);
// Set empty value
$blank = setcookie($name, '', time() - 3600);
Can someone run up this code on a test machine I'm a tad confused as to why cookie can be "unset" but cannot be value changed or expired etc in php it seems like setcookie() isn't working at all.
<script type="text/javascript" src="jquery.js"></script>
<script src="jquery.cookie.js"></script>
<script>
$.cookie('myCookie', '123' ,'/');
console.log("Created myCookie");
</script>
<?php
echo $_COOKIE['myCookie'];
//Comment/uncomment below as required
//setcookie("myCookie", "BLAH", time()-430000);
//$_COOKIE['myCookie'] = "BLAH";
setcookie('myCookie', '', 1, '/');
echo "<br />unset myCookie";
echo "<br />".$_COOKIE['myCookie'];
?>
<script>
console.log($.cookie('myCookie').length);
if ($.cookie('myCookie').length>0)
{
console.log('Cookie exists ');
console.log($.cookie('myCookie'));
}
else
{
console.log('Yaay! No cookie!');
}
</script>
You seem to be able to create a php <> JS cookie mismatch ie: 2 cookies called exactly the same but storing different data.