简体   繁体   English

如何在php cookie值中读取jquery cookie值?

[英]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? 我不确定是否有可能在php cookie值中读取jquery cookie值?

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). 这是一个简单的演示,演示了如何使用JavaScript创建cookie并在重新加载页面时使用PHP读取它(因为JavaScript是在PHP之后执行的,因为它是客户端的)。 Copy this code onto a new PHP document, upload it to your server, and try it. 将此代码复制到新的PHP文档中,将其上载到您的服务器,然后尝试。

<!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. Cookie是存储在浏览器中并与网站相关联的一段数据。 Every time the browser requests something from a website, it includes the cookies in the request headers. 每次浏览器从网站请求内容时,它都会在请求标头中包含Cookie。

You can read and write cookies with client side JavaScript. 您可以使用客户端JavaScript读写cookie。 (NB: It is possible to mark cookies as http_only, in which case they can't be read with client side JS). (注意:可以将cookie标记为http_only,在这种情况下,无法使用客户端JS读取它们)。

You can read (by examining the request headers) and write (using response headers) cookies with server side code. 您可以阅读(通过检查请求标头)并使用服务器端代码编写(使用响应标头)cookie。

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. 使用服务器端代码设置cookie需要您发出响应标头。 PHP doesn't let you issue response headers after you have started outputting the response body. 在您开始输出响应正文后,PHP不允许您发出响应标头。

You are trying to call setcookie too late. 你试着打电话给setcookie太晚了。 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. 将JavaScript代码存储在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. 您无法使用客户端代码生成服务器端代码,因此如果这是尝试将current值写入cookie,则为时已晚。
 setcookie("bgx","document.write(current)"); echo $_COOKIE["bgx"]; 

$_COOKIE is populated with data from the request. $_COOKIE填充了请求中的数据。

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. 简而言之:要使用JavaScript读取cookie集,您必须设置发出新的HTTP请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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