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 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?