Skip to content Skip to sidebar Skip to footer

How To Get A Cookie In Php Without Refreshing The Page?

I currently have this code and two problems: I get the users timezone with Javascript and post it to the timezone.php via ajax, which sets a cookie with the users time. If the cook

Solution 1:

You can use JQuery Plugin https://github.com/carhartl/jquery-cookie and check if the cookie is set in your $.post method like that

 $.post('timezone.php',{timezone: timezone}, function(data){
     if ($.cookie("time") != null) 
         location.reload(true);
});

Solution 2:

set the class with JavaScript, there is no need for the server.

Solution 3:

PHP script only runs when a request is made to the server. You could use JS and AJAX, or, if appropriate, an iframe.

Solution 4:

you can change index.php:

 $.post('timezone.php',{timezone: timezone}, function(data){ 
    if(data=='success'){
        location.reload(true)
    }
 });

and in timezone.php:

<?php$timezone = $_POST["timezone"];

if(isset($timezone)) {
    date_default_timezone_set('Etc/GMT'.($timezone <= 0 ? '' : '+').$timezone);
    $time = date("G") + (date("i")/60);
    setcookie("time", $time);
    echo"success";
}

?>

Post a Comment for "How To Get A Cookie In Php Without Refreshing The Page?"