简体   繁体   中英

How to read jquery cookie value in php cookie value?

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; ?>);

Here 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.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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM