douzi8112 2009-11-22 07:33
浏览 292
已采纳

用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'];

for assigning the value

$b=$_COOKIE['a'];

The cookie created using jQuery is

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

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

  • 写回答

3条回答 默认 最新

  • dsjmrpym220113739 2009-11-22 09:15
    关注

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

    ..however...

    It appears you are using jQuery in this way.

    What may be causing a problem is a few things:

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

    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 '/'.

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

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

    ...and search the array for your "a" 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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据