获取PHP中的cookie值? [英] Get the cookie value in PHP?

查看:72
本文介绍了获取PHP中的cookie值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jQuery中创建cookie.我可以在Firefox中验证它.但是,当我尝试打印cookie或将其分配给另一个值时,我无法获取它.我也使用会话.然后,我开始使用PHP进行会话. 我的打印或分配Cookie值的代码如下所示:

I create the cookie in jQuery. I can verify it in Firefox. But when I try to print the cookie or assign into another value I cant get it. I also use sessions. And I started the session in PHP. My code to print or assign the cookie value is shown below

echo $_COOKIE['a'];

用于分配值

$b=$_COOKIE['a'];

使用jQuery创建的cookie是

The cookie created using jQuery is

$.cookie("a",$(this).val());

我在哪里弄错了?如何获取Cookie值?

Where did I make a mistake? How can I get the cookie value?

推荐答案

完全可以在没有jQuery的情况下完全在PHP中设置cookie.

It is possible to set the cookie in PHP entirely without jQuery at all..

..但是...

看来您正在此方式中使用jQuery.

可能导致问题的原因有几件事:

What may be causing a problem is a few things:

a)$(this).val()可能返回NULL.

a) $(this).val() possibly might be returning NULL.

b)您没有在cookie上设置路径和到期时间.如果您有子目录,通常最好设置一个主cookie 作为根路径'/'.

b) You are not setting the path and expiration on the cookie. If you have subdirectories it is normally good to set a master cookie that is the root path '/'.

要使用PHP读取Cookie,请尝试以下操作...

To read your cookie using PHP, try this...

  $cookies = explode(';', $_SERVER['HTTP_COOKIE']);

...并在数组中搜索您的"a" cookie.

...and search the array for your "a" cookie.

有关完全使用PHP设置和读取Cookie的其他文档,请在此处查看.

Further docs on setting and reading cookies using entirely PHP, check here.

在将任何其他代码回显到页面之前,必须先调用此函数. (在标题之前)

You must call this function before any other code is echoed to the page. (before headers)

这篇关于获取PHP中的cookie值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆