Reputation: 3642
I have one web page index.php where I am executing one ajax
//index.php
<script src="jquery-2.0.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.get('ajax.php', function(data){
console.log(data);
});
})
</script>
Noe on server side I am creating few cookies in ajax.php
//ajax.php
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
setcookie("Phone", '111111', time() + 86400);
print_r($_COOKIE);
?>
Now I am opening another page test.php and trying to get this cookie, But I am getting cookies, I tried to print cookies
//test.php
<?php
print_r($_COOKIE);
?>
but those cookies does't print..
Upvotes: 0
Views: 374
Reputation: 137
Cookies set via PHP over AJAX aren't going to get set in the client, because they are defined in the HTTP headers that the browser receives when the page loads. This is why you can't call setcookie after you've sent output.
Cookies are pretty easy to set with javascript though: http://www.w3schools.com/js/js_cookies.asp
If the PHP script is doing calculations that are needed in the cookies, just pass them back as JSON, and then set them via javascript.
I don't know how well it works, but here is a jQuery plugin for managing cookies: http://plugins.jquery.com/cookie/
Upvotes: 1