I am not sure is it possible to read jquery cookie value in php cookie value?
var current = $.cookie("count");
show<?php echo $keys; ?><?php setcookie("bgx","document.write(current)"); echo $_COOKIE["bgx"]; ?>now.play();
$.cookie('count', <?php echo $count; ?>);
I am not sure is it possible to read jquery cookie value in php cookie value?
var current = $.cookie("count");
show<?php echo $keys; ?><?php setcookie("bgx","document.write(current)"); echo $_COOKIE["bgx"]; ?>now.play();
$.cookie('count', <?php echo $count; ?>);
Share
Improve this question
asked Jul 14, 2015 at 10:31
FOOUTYFOOUTY
991 gold badge1 silver badge11 bronze badges
5
- Yes, cookies created client-side with JavaScript are accessible by server-side PHP, these are just different ways to create and read cookies. The code you posted is not a very good example, though. Note: for PHP to be able to read a cookie created with JS, you would first need to have the user visit the page, and only then, the cookie exists and can be read by PHP. Your JS is not being executed on the server-side. – blex Commented Jul 14, 2015 at 10:37
- I don't understand and if yes then how? This example not working @blex – FOOUTY Commented Jul 14, 2015 at 10:39
- Please feel free to edit my question @blex – FOOUTY Commented Jul 14, 2015 at 10:40
- How to executed those on server side? give me an example @blex – FOOUTY Commented Jul 14, 2015 at 10:43
- You mean when the user visit the page 2nd time then js cookie read by php ? @blex – FOOUTY Commented Jul 14, 2015 at 10:46
2 Answers
Reset to default 3Here is a simple demo that shows how to create a cookie with JavaScript and read it with PHP when you reload the page (because JavaScript is executed after PHP, as it is client-side). Copy this code onto a new PHP document, upload it to your server, and try it.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<h1>Cookie demo</h1>
<p>This demo shows how you can create a cookie with Javascript (using jQuery), and read it with PHP.</p>
<?php
/* THIS PORTION IS EXECUTED ON THE SERVER */
// if the cookie "myCookie" is set
if(isset($_COOKIE['myCookie'])){
echo "<p><b>PHP found this value for <i>myCookie</i>: " . $_COOKIE['myCookie'] . "</b></p>";
}
else{
echo "<p><b>PHP did not find a value for <i>myCookie</i>. Give it a value below.<b></p>";
}
?>
<input type="text" id="myInput"/><button id="myButton">Change the cookie value using JS</button>
<!-- make sure you load jQuery and the jQuery cookie plugin -->
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script>
/* THIS PORTION OF CODE IS ONLY EXECUTED WHEN THE USER SEES THE PAGE (CLIENT-SIDE) */
$(function(){
$('#myButton').click(function(){
$.cookie('myCookie', $('#myInput').val());
alert(
'The value of myCookie is now "'
+ $.cookie('myCookie')
+ '". Now, reload the page, PHP should read it correctly.'
);
});
});
</script>
</body>
</html>
A cookie is a piece of data that is stored in a browser and associated with a website. Every time the browser requests something from a website, it includes the cookies in the request headers.
You can read and write cookies with client side JavaScript. (NB: It is possible to mark cookies as http_only, in which case they can't be read with client side JS).
You can read (by examining the request headers) and write (using response headers) cookies with server side code.
The usual rules of timing apply:
show<?php echo $keys; ?><?php setcookie(
Setting a cookie with server side code requires you to issue a response header. PHP doesn't let you issue response headers after you have started outputting the response body.
You are trying to call setcookie
too late. You need to call it before you start outputting any content.
"document.write(current)"
I'm not sure what you are trying to achieve here.
- It doesn't make much sense to store JavaScript code in a cookie.
- You can't generate server side code using client side code, so if this is an attempt to write the value of
current
into a cookie, then it is too late.
setcookie("bgx","document.write(current)"); echo $_COOKIE["bgx"];
$_COOKIE
is populated with data from the request.
It won't have data in it that your current response is about to ask the client to store. You won't get that new value until the next request.
In short: To read a cookie set with JavaScript, you have to make a new HTTP request after you have set it.