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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测