duanche2007
2018-06-08 19:42
浏览 198

使用fetch()发布时未设置Cookie

Alright, so I have come across a rather weird problem.

I am creating a login instance for users.

When I post the login form without fetch() it works. My browser (both Firefox and Chrome) sets the cookie properly and I can be "authenticated".

However, when I send the data via fetch() it does not setcookie() in the backend.

NOTE: The assumption is that the credentials are correct.

This is my PHP login script

$user = htmlentities($_POST['username'], ENT_QUOTES);
$pass = htmlentities($_POST['password'], ENT_QUOTES);
$remember = isset($_POST['rememberme']) ? true : false;

$login = $PHPAuth['auth']->login($user, $pass, $remember);

/**
 * $PHPAuth == [
 *   'config' => PHPAuthConfig(),
 *   'auth'   => PHPAuth()
 */ 


if($login['error']) {
    echo json_encode($login);
} else {
    // create new cookie
   setcookie(
       $PHPAuth['config']->cookie_name,
       $login['hash'],
       $login['expire'],
       '/',          # path
       'localhost',  # domain
       false,        # HTTPS
       true          # HTTP-ONLY
    );
    echo json_encode($login);
}

The fetch() script is at its bare minimum, only requesting body and method. I removed any extra headers to see if it would help. It didn't:

function postForm(url, data) {
  return fetch(url, {
      body: data,
      method: 'post'
    })
    .then(response => response.json());
}

FOR REFERENCE: I am using this PHPAuth library.

图片转代码服务由CSDN问答提供 功能建议

好吧,所以我遇到了一个相当奇怪的问题。

I 我正在为用户创建一个登录实例。

当我发布没有 fetch()的登录表单时,它可以工作。 我的浏览器(包括Firefox和Chrome)正确设置了cookie,我可以进行“身份验证”。

但是,当我通过 fetch()发送数据时 在后端没有 setcookie()

注意:假设凭据是正确的。 \ n

这是我的PHP登录脚本

  $ user = htmlentities($ _ POST ['username'],ENT_QUOTES); 
 $ pass = htmlentities($  _POST ['password'],ENT_QUOTES); 
 $ remember = isset($ _ POST ['rememberme'])?  true:false; 
 
 $ login = $ PHPAuth ['auth']  - > login($ user,$ pass,$ remember); 
 
 / ** 
 * $ PHPAuth == [
  *'config'=>  PHPAuthConfig(),
 *'auth'=>  PHPAuth()
 * / 
 
 
if($ login ['error']){
 echo json_encode($ login); 
} else {
 //创建新cookie 
 setcookie(
  $ PHPAuth ['config']  - > cookie_name,
 $ login ['hash'],
 $ login ['expire'],
'/',#path 
'localhost',#domain 
  false,#HTTPS 
 true#HTTP-ONLY 
); 
 echo json_encode($ login); 
} 
   
 
 

fetch( )脚本是最低限度,仅请求 body method 。 我删除了任何额外的标题,看看它是否会有所帮助。 它没有:

  function postForm(url,data){
 return fetch(url,{
 body:data,
 method:'post'
  })
 .then(response => response.json()); 
} 
   
 
 

FOR REFERENCE:我正在使用 PHPAuth 库。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhi6365 2018-06-08 19:48
    已采纳

    By default, fetch does not have cookies enabled, you can do so by adding the credentials option and setting it to same-origin:

    function postForm(url, data) {
      return fetch(url, {
          credentials: "same-origin",
          body: data,
          method: 'post'
        })
        .then(response => response.json());
    }
    

    Read more about Request.credentials at MDN.

    打赏 评论

相关推荐 更多相似问题