【问题标题】:saving a Cookie in javascript with the current url saves it with double quotes in PHP $_COOKIE使用当前 url 在 javascript 中保存 Cookie 在 PHP $_COOKIE 中使用双引号保存
【发布时间】:2013-12-28 18:25:37
【问题描述】:

我正在使用 jQuery Cookie 来保存一个 cookie,然后在 PHP 中访问它。

$( document ).on( 'click', '#ai1ec-calendar-view .ai1ec-load-event', function( e ) {
    e.preventDefault();
    $.cookie.raw = true;
    $.cookie( 'ai1ec_calendar_url', document.URL );
    window.location.href = this.href;
} );

但是如果我将它转储到 PHP 中,我会添加双引号,如下图所示。发生了什么事?

【问题讨论】:

  • 你能做一个console.log(document.URL)吗?
  • @MarcelKorpel 我猜问题是 Cookie 是由 jQuery.Cookie 字符串化的。
  • 你能用原生 JavaScript cookie 函数测试它吗?
  • 你说得对,看看github.com/carhartl/jquery-cookie/blob/master/…(这也是我讨厌我不懂的第3方代码的原因)。
  • @MarcelKorpel 他说 RFC2068 要求字符串化并不知道这一点。无论如何感谢您的帮助。

标签: javascript php jquery cookies


【解决方案1】:

问题与应该引用 cookie 字符串的事实有关,因此 jQuery Cookie 添加了双引号。 PHP中的解决方案是使用json_decode

$href = json_decode( $_COOKIE['ai1ec_calendar_url'] );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 2022-01-02
    • 2017-04-17
    • 1970-01-01
    • 2018-09-25
    • 2012-02-27
    • 2021-09-28
    相关资源
    最近更新 更多