I am trying to diagnose an error in my cookies, but the names of the cookies are not what they should be. Is there a way in PHP to print all the cookies that have been set by my domain?
Have you tried:
print_r($_COOKIE)
foreach ($_COOKIE as $key=>$val)
{
echo $key.' is '.$val."<br>\n";
}
<pre><?php print_r( $_COOKIE ); ?></pre> will do what you want. You might also try phpinfo().
echo $_COOKIE["cookie_name"]; // Print an individual cookie
print_r($_COOKIE); // Another way to debug/test is to view all cookies
You can display all cookies defined by running the following php function:
var_dump($_COOKIE);
if($_COOKIE) {
print_r($_COOKIE); //print all cookie
}
else
{
echo "COOKIE is not set";
}
As with any inputs, security practices should include filtering and validation. Since all cookies are strings, sanitize the strings:
var_dump(filter_input_array(INPUT_COOKIE, FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY))
PHP Docs: https://www.php.net/manual/en/function.filter-input-array.php
if($_COOKIE) {
foreach ($_COOKIE as $key=>$val)
{
echo $key.' is '.$val."<br>\n";
}
}
else
{
echo "No Cookies are Set";
}
This will check if any cookies are set, if found will iterate through each and print out the cookie name and value
Related
I know how to set PHP cookies in an array but can I clear it without a loop?
For example I'm setting these four cookies
// set the cookies
setcookie("cookie[four]", "cookiefour");
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");
// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
$name = htmlspecialchars($name);
$value = htmlspecialchars($value);
echo "$name : $value <br />\n";
}
}
OUTPUT:
four : cookiefour
three : cookiethree
two : cookietwo
one : cookieone
To clear the cookies, i use the following loop
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
setcookie ("cookie[".$name."]", "", time() - 1);
}
}
Are there any way I could clear the cookies without a loop? Thanks in advance
No, you pretty much need to loop through existing cookies as the way to delete/expire a cookie, as you have in your code via setcookie, is one cookie at a time.
Could try
if (isset($_COOKIE['cookie'])) {
setcookie ("cookie", array());
}
or even
if (isset($_COOKIE['cookie'])) {
setcookie ("cookie", array('one'=>'','two'=>'','three'=>'','four'=>'',));
}
if you need the indexes
I was trying to figure out how isset() and empty() related to setcookie() and $_COOKIE[]. But I came upon a road-block on the way.
Here is my test.php
<?php
//initialize cookie
$expiry = time()+60*60*9000;
setcookie('name1', '4', $expiry, '/', '', '', TRUE);
if (isset ($_COOKIE['name1'])) {
echo 'cookievalue ' . $_COOKIE['name1'];
} else {
echo 'cookie value not set';
}
if (!empty ($_COOKIE['name1'])) {
echo 'cookievalue ' . $_COOKIE['name1'];
} else {
echo 'cookie value empty';
}
?>
Here is my test1.php
<?php
if (isset ($_COOKIE['name1'])) {
echo 'cookievalue ' . $_COOKIE['name1'];
} else {
echo 'cookie value not set';
}
if (!empty ($_COOKIE['name1'])) {
echo 'cookievalue ' . $_COOKIE['name1'];
} else {
echo 'cookie value empty';
}
echo 'cookievalue ' . $_COOKIE['name1'];
?>
When I first load test.php, and then test1.php, everything seems to work fine. That is, test1.php is able to read the $_COOKIE[] variable that was set in test.php via setcookie(). (Although, as expected, test.php had to be refreshed once before cookie values were output in test.php.)
However, if I close the browser, and reopen it, and then just run test1.php, I get an "Undefined Index" notice on name1 in $_COOKIE['name1'].
Why can't test1.php pick up the $_COOKIE variable defined before the browser was closed? The cookie should still be stored in the computer. Why can't it pull up the cookie value from it after closing and reopening the browser?
Answering my own question.
Thanks to #Dagon tried it using a different browser. It works in another browser (Firefox). It wasn't working in my Chrome browser (I suspect I have some anti-cookie extension on Chrome that's deleting the cookie -- or something like that).
You dont need isset() its as simple as
if ($_COOKIE['name1']) {
i have installed phpmyadmin on mac but there are problem , when i using $_COOKIE[] i see this message in my web page
Notice: Undefined variable: coo in
how can i fix this problem ?
thank you .
I think this could help:
First, you have to set a cookie value:
<?php
$expire=time()+60*60*24*30;
setcookie("user", "Alex Porter", $expire);
?>
After that, you can retrieve this value:
<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>
Source: http://www.w3schools.com/php/php_cookies.asp
user1219391 :
You can check if the cookie value exists using this code:
if (array_key_exists('user', $_COOKIE))
{
$userStatus=$_COOKIE['user'];
}
else
{
$userStatus='Not Set';
}
echo 'The value of Cookie user is '. $userStatus;
With this you can solve your issue...
I want to display a link to a user when he has an cookie named abc in his browser
<?php
$cookie = abc;
if (!$cookie)
{
echo <br><center>click here</center></b>;
}
?>
how could I do this?
thanks
The PHP global $_COOKIE could be used to check the user's cookies
<?php
if (isset($_COOKIE["abc"]))
{
echo '<br><center>click here</center></b>';
}
?>
check http://www.w3schools.com/php/php_cookies.asp
You need to check if it is set in the $_COOKIE variable:
<?php
$cookie = abc;
if(isset($_COOKIE[$cookie]))
{
echo <br><center>click here</center></b>;
}
?>
study the php tag $_COOKIE on php.net
and add the "" into the right places.... like: echo "...
Can anyone tell me why I keep getting the else and alert is not being triggered at all? The cookie is being set just while the browser is open.
<?php
$setcookie = setcookie('version', 'nova');
$browser = get_browser(null, true);
if(!isset($setcookie)){
if($browser["MSIE"] < 8.0){
// display message or alert!
echo "<script language=\"JavaScript\">\n";
echo 'alert("Please upgrade to version 8.0+ in order to view this site.");';
echo "</script>";
}
}
else
{
echo "Browser is current:";
}
?>
Your if condition is wrong. You want to check the $_COOKIE array, not the return value of setcookie.
if(!isset($_COOKIE['version'])) {
...
}
I don't think get_browser() returns what you think it returns? See the manual: http://php.net/manual/en/function.get-browser.php
You want to look at the [browser] and [version] fields.
PHP get_browser() method is very slow. Better use something like
$useragent = $_SERVER['HTTP_USER_AGENT'];
then you can do some preg_match as below
if(preg_match('/MSIE/i',$useragent)){
//echo something here
}
else{
//do something else
}