Reputation: 621
I have a cookie like this and the cookie is set using javascript.Now I need to read the cookies value using php This is how cookie stores.I am able to see the cokie values from mozila debugg tools as this
["as","asda","fdfdf"] - multiple values
Sometimes it can contain only single value ["as"].
It seems its a json representation.How can I read the value.My cookie name is "settings"
Solved this way https://stackoverflow.com/a/27075868/2505607
Upvotes: 1
Views: 1547
Reputation: 131
In php you use cookies just like other variables. Or at least as other superglobal variables. You read and write them like this:
$my_var = $_COOKIE["cookie_name"];
$_COOKIE["cookie_name"] = $some_value;
To loop through all of them, you can use a foreach loop:
$my_array = array();
print "<ul><li>";
foreach($_COOKIE as $cookie_name => $cookie_value) {
$my_array[$cookie_name] = $cookie_value;
print "$_COOKIE[" . $cookie_name . "] = " . $cookie_value . "</li><li>";
}
print "</li></ul>";
This piece of code will output all of the cookies into an array and print it out as an unordered list (<ul>
).
Also, the reason why you can have more than one values in one $_COOKIE
entry, is that you have an array in an "arrary", resulting in a multidimensional array. Other programmind languages have better support for this (for example in C++ int my_array[x][y][z];
creates a 3 dimensional int array.), but the advantage of php regarding arrays, that you can very easily create associative arrays ($my_array = array(["asdf"] => "a_value", ["b23"] => "another_value", ["another"] => "another_value");
).
Upvotes: 0
Reputation: 621
I have solved this way
<?php
$Cookie_name = $_COOKIE['settings'];
$decode = json_decode($Cookie_name);
$cnt = count($decode);
echo "<ul>";
for($i=0;$i<$cnt;$i++) {
echo "<li>".$decode[$i]."</li>";
}
echo "</ul>";
?>
Upvotes: 2
Reputation: 770
To diagnose the problem, we would also need to see the Javascript that is setting the cookie to ensure there are no errors there.
Accessing cookies in PHP can be done using the $_COOKIE superglobal. Examples specific to you are below:
isset($_COOKIE['settings']) // returns true if cookie with name 'settings' is set
var_dump($_COOKIE['settings']) // dump contents of settings cookie
It may also be useful to dump the contents of $_COOKIE, which will show what cookies are set. You can do this with the following code:
var_dump($_COOKIE);
Please post the relevant JS that is setting the cookie and I will update my answer. The issue is that how you set the cookie also determines how you access it. For example, there are options which allow you to specify the domain and path of the cookie when setting - this could be preventing you from accessing this in your PHP script, namely if the script is on a different page to the JS that set the cookie.
Upvotes: 0
Reputation: 627
$cookie_name = "user";
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' does not exist!";
} else {
echo "Cookie is named: " . $cookie_name . "<br>Value is: " . $_COOKIE[$cookie_name];
}
Try this out. Its the same way as using $_SESSION
Update:
To loop each cookie you can use something similar to this:
foreach ($_COOKIE as $cookie_name => $cookie_value) {
print "$cookie_name = $cookie_value<br>";
}
Put the loop inside your ul
tag and add the necessary li
tag to the print
. This way you are dynamically creating your list-items.
Upvotes: 0