How to read jQuery cookie value in php cookie value?

I'm not sure if it is possible to read the jquery cookie value in the 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; ?>);

      

+3


source to share


2 answers


Here is a simple demo that shows how to create a cookie with JavaScript and read it using PHP on page reload (since JavaScript is executed after PHP, as it is client side). Copy this code to a new PHP document, upload it to your server and try it out.



<!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.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/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>

      

+2


source


A cookie is a piece of data that is stored by the browser and associated with a website. Every time the browser requests something from the website, it includes cookies in the request headers.

You can read and write cookies using JavaScript on the client side. (NB: It is possible to mark cookies as http_only, in which case they cannot be read from the JS client side).

You can read (by looking at the request headers) and write (using the response headers) cookies with server side code.

The usual synchronization rules apply:

show<?php echo $keys; ?><?php setcookie(

      

Setting a cookie with server side code requires you to send a response header. PHP does not allow you to emit response headers after you have started to output the response body.

You are calling too late setcookie

. You need to call it before you start displaying any content.

"document.write(current)"

      



I'm not sure what you are trying to achieve here.

  • It doesn't make sense to store JavaScript code in a cookie.
  • You cannot generate server side code using client side code, so if this is an attempt to write a value current

    to a cookie, it is too late.
setcookie("bgx","document.write(current)"); 
echo $_COOKIE["bgx"];

      

$_COOKIE

filled with data from the request.

It won't have data in it that your current response is going to ask the client to store. You will not get this new value until next .


In short: To read a set of JavaScript cookies, you must make a new HTTP request after it has been set.

+2


source







All Articles