I have one problem and I need help. I am doing some simple steps:
php code:
<?php
if (isset($_COOKIE["parameters"])){
$UserParam = json_decode($_COOKIE["parameters"],true);
print_r($UserParam); // when the cookie is set by php it prints the array (on all browsers), but when cookie is set by javascript nothing print on explorer,opera,safari.
echo"<script>function readCookie(){ alert(\"the cookie was there with radius: ".$UserParam['radius']." type: ".$UserParam['type']."\"); //again when the cookie set with php the variables have values on every browser, but when the cookie set with javascript no values to the variables.
getLocationFunction(\"".$UserParam["radius"]."\",\"".$UserParam["type"]."\",\"".$UserParam["date"]."\",\"".$UserParam["name"]."\")}</script>";
}
else {
$defaultParam = array("radius" => "ως 50km","type" => "all","date" => "unlimited", "name" => "all");
$defaultParamSerialized = json_encode($defaultParam);
setcookie("parameters","$defaultParamSerialized",time()+3600);
echo"<script>function readCookie(){ alert(\"there was no cookie so i set it: radius ".$defaultParam["radius"]."\");
getLocationFunction(\"".$defaultParam["radius"]."\",\"".$defaultParam["type"]."\",\"".$defaultParam["date"]."\",\"".$defaultParam["name"]."\")
}
</script>";
}
?>
javascript code:
function renewCookie(){
var myArray = {radius: radius, type: type, date: price , name: company };
var valueSerialized = JSON.stringify(myArray);
createCookie('parameters',valueSerialized,999);
}
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
Again, I read the cookie in all situation with php, but when I create it with php it can be read lovely to all browsers, when i create it with javascript it can be read fine to firefox, chrome but not on explorer, opera and safari.
note1: there is no syntax error.
note2: browsers are the latest versions.
plz help. thank you in advance!
Try that,
Change this
if (isset($_COOKIE["parameters"])){
with this
$COOKIE = isset($_COOKIE["parameters"]) ? $_COOKIE["parameters"] : "";
And use print_r($_COOKIE)
with any browsers to see the difference.