dshmkgq558192365 2016-10-19 06:55
浏览 35

在setcookie函数PHP中的路径后附加Hash片段

I'm trying to set path in setcookie for my websites cookie, which contains a string followed by #wall , I could see path only till the first string, it's not accepting the # fraction of the path.

code goes like this:

 $wall = array(
     $this->database,
     $this->response['grouplist'][0],
     $this->username
 );

 setcookie(
     "wall",
     json_encode($wall),
     time() + 3600 * 24 * 1000 ,
     "/" + $this->database + "/#wall",
     ".mywebsite.com",
     0
 );

 session_set_cookie_params(0, '/', '.mywebsite.com');

output :

mywebsite.com   /S71c9524b57ab1b3383bcb14478b570b6      2019-07-16T06:37:55.065Z        92  
  • 写回答

1条回答 默认 最新

  • dongmou2389 2016-10-19 07:16
    关注

    Fragment is not a part of Path

    The # character specifies the fragment part of a URL (RFC 3986):

      URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
    
      hier-part   = "//" authority path-abempty
                  / path-absolute
                  / path-rootless
                  / path-empty
    

    So the fragment (wall, particularly) is a different part of URL, and is not considered as part of the path:

    The path is terminated by the first question mark ("?") or number sign ("#") character, or by the end of the URI.

    Moreover, fragments depend on the document MIME type and are evaluated by the user agent (RFC 3986, 3.5. Fragment), i.e. fragments are never sent to the server:

    ...the fragment identifier is not used in the scheme-specific
    processing of a URI; instead, the fragment identifier is separated
    from the rest of the URI prior to a dereference, and thus the
    identifying information within the fragment itself is dereferenced
    solely by the user agent...

    In other words, URL fragments are not supposed to work in the cookie Path attributes, and the server is not supposed to know anything about fragments.

    PHP syntax

    Also note, + is an arithmetic operator, so your "/" + $this->database + "/#wall" is evaluated to 0. If you meant concatenation, use the . (dot) operator instead:

    $s = 'abc';
    echo "/" + $s + "/#wall", PHP_EOL;
    echo "/" . $s . "/#wall", PHP_EOL;
    echo "/{$s}/#wall", PHP_EOL;
    

    Output

    0
    /abc/#wall
    /abc/#wall
    

    So you should replace your "arithmetic" expression with "/{$this->database}/#wall".

    It may work... sometimes

    I have tested how the current version of Mozilla Firefox processes cookies for a path with different fragments. As it turns, it actually writes the cookies into document.cookie for different fragments, but one has to hard-reload the page in order to update the cookies. So I doubt that this is a useful feature.

    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch