简体   繁体   English

使用 PHP 获取/读取 Javascript cookie

[英]Get/Read Javascript cookie with PHP

I have a theoretical question..我有一个理论问题..

I know that you can get/read a PHP cookie with javascript by using: document.cookie我知道您可以使用document.cookie获取/读取带有 javascript 的 PHP cookie

Is there a similar way to do this in PHP? PHP有类似的方法吗?

Can PHP get/read a cookie that is created i JavaScript? PHP 可以获取/读取在 JavaScript 中创建的 cookie 吗? If yes, then how can you do that?如果是,那你该怎么做?

You can use $_COOKIE , the superglobal.您可以使用$_COOKIE ,超级全局变量。 Just reference it like you would any array, where $_COOKIE['key_name'] is the cookie you want to access.就像引用任何数组一样引用它,其中$_COOKIE['key_name']是您要访问的 cookie。

See the PHP API documentation .请参阅PHP API 文档

To see them you can do this:要查看它们,您可以这样做:

foreach($_COOKIE as $v){
  echo htmlentities($v, 3, 'UTF-8').'<br />';
}

For a single cookie it's just:对于单个 cookie,它只是:

echo htmlentities($_COOKIE['cookieName'], 3, 'UTF-8');

Feel free to change the quote style (3 being ENT_QUOTES) and charset to suit your needs.随意更改引用样式(3 是 ENT_QUOTES)和字符集以满足您的需要。

Note: The cookie has to have been set on the same domain for you to access it.注意:cookie 必须设置在同一个域上,您才能访问它。

PHPglue's answer is good, but has several typos in it. PHPglue 的回答很好,但有几个错别字。 It also doesn't tell you what the index is which can be very helpful.它也不会告诉您索引是什么,这可能非常有用。

foreach($_COOKIE as $key=>$value)
{
  echo "key: ".$key.'<br />';
  echo "value: ".$value.'<br />';
};

My issue was that I was setting my cookie in Javascript with periods in the name.我的问题是我在 Javascript 中设置了我的 cookie,名称中带有句点。 These were being converted to underscores.这些被转换为下划线。 For example cookie name, facebookLogin.myapp.com was being changed to facebookLogin_myapp_com.例如 cookie 名称,facebookLogin.myapp.com 被更改为 facebookLogin_myapp_com。 Once I ran the code above, I was able to see that the name was different than expected and read the name from PHP correctly.运行上面的代码后,我能够看到名称与预期不同,并从 PHP 中正确读取了名称。

Javascript can set cookies in two ways (of i know) window.cookie and document.cookie . Javascript 可以通过两种方式设置 cookie(我知道) window.cookiedocument.cookie if use window.cookie php cannot assess the cookie, php can only assess cookie set by document.cookie .如果使用window.cookie php 无法评估 cookie,则 php 只能评估document.cookie设置的 cookie。

javascript javascript

document.cookie = 'name=value'; document.cookie = 'name=value';

PHP PHP

print_r($_COOKIE);打印_r($_COOKIE);

you will see your javascript created cookie inside, among other created by php.你会在里面看到你的javascript创建的cookie,以及由php创建的其他cookie。

or或者

echo $_COOKIE['name'];回声 $_COOKIE['name'];

Assess php cookie with javascript use same document.cookie to assess document (php) cookies and javascript cookies created with document.cookie.使用 javascript 评估 php cookie 使用相同的document.cookie来评估文档 (php) cookie 和使用 document.cookie 创建的 javascript cookie。

Javascript only can assess window.cookies created cookies Javascript 只能评估 window.cookies 创建的 cookie

HTTP is a stateless protocol. HTTP 是一个无状态协议。 During Client Server communication to maintain state we use some techniques.在维护 state 的客户端服务器通信期间,我们使用了一些技术。 Cookie is one of them.饼干就是其中之一。 Generally we use Cookies to identify an user.一般我们用Cookies来标识一个用户。 When first time user send request to the server in response server sends a set of Cookies. Which browser stores in Client machine.当用户第一次向服务器发送请求时,服务器会发送一组 Cookies。浏览器存储在客户端机器中。 Next time when the same user request again browser send the information store in Cookies. Through which server get to identify the user.下次同一用户再次请求时,浏览器发送信息存储在 Cookies 中。通过该服务器可以识别用户。 Create, Remove or Read a PHP Cookie using Setcookie method使用 Setcookie 方法创建、删除或读取 PHP Cookie

I think this is a good way to set all intimation in one cookie and use an symbol between your information.我认为这是在一个 cookie 中设置所有暗示并在您的信息之间使用符号的好方法。 Then use for example this in "PHP" after set cookie by JavaScript然后在通过 JavaScript 设置 cookie 后在“PHP”中使用例如

$mystr = print_r($_COOKIE);
$way=explode(";",$mystr);

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

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