I have one problem and I need help. I am doing some simple steps:
- i set a cookie with php(if it is not already set).
- I read this cookie again with php and it works fine (to all browsers!).
- I reset the cookie that i set previously, with javascript this time.
- I read the cookie setted by javascript with php and on firefox, chrome it works fine, but on explorer, opera, safari (i use the latest versions) it doesn't work. cookie can't be read. no error return but the field of the cookie are blank. plz see the code below.
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!