为什么不能从whatwg-fetch请求中设置PHP $ _SESSION? [英] Why can't I set PHP $_SESSION from a whatwg-fetch request?

查看:118
本文介绍了为什么不能从whatwg-fetch请求中设置PHP $ _SESSION?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的提取请求,如下所示:

I have a simple fetch request that looks like this:

fetch('/includes/signon.php', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    Email: event.target.form.email.value,
    Password: event.target.form.password.value,
  })
})
.then((response) => {
  return response.text()
}).then((body) => {
  this.displayWelcomeMessage(body);
})

在后端,我有PHP可以像这样处理提取请求:

And on the backend, I have PHP that handles the fetch request like so:

<?php
require_once('database.php');
session_start();

$request_body = file_get_contents('php://input');
$json = json_decode($request_body);

if (!empty($json->Email) && !empty($json->Password)) {
  global $db, $json;
  $sql = "SELECT `user_id` from users where email = ? AND password = ?;";
  $stmt = $db->initialize_statement();
  if (!$stmt->prepare($sql)) {
    $error = $stmt->error;
    echo $stmt->error;
  } else {
    $email = $json->Email;
    $password = $json->Password; // Stored as a string for the sake of simplicity
    $stmt->bind_param('ss', $email, $password);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($result);
    while($stmt->fetch()) {
      if ($stmt->num_rows === 0) {
        echo "Incorrect Username or Password";
      } else {
        // Here lies the problem...
        $_SESSION['user_id'] = $result;
        echo "Welcome, User #".$result."!";
      }
    }
    $stmt->close();
  }
} else {
  var_dump($_SESSION); // For debugging purposes only
}

现在,这是问题所在!响应的主体显示欢迎使用,用户#12!".当我以应有的方式登录时,但$_SESSION['user_id']设置为12却没有存储在实际会话中...

Now, here's the problem! The body of the response displays "Welcome, User #12!" when I log in like it should, but $_SESSION['user_id'] is set to 12 without being stored in the actual session...

如果我向该页面发出GET请求,则会将$_SESSION转储出去,而找不到$_SESSION['user_id'].

If I make a GET request to the page, it dumps the $_SESSION out and $_SESSION['user_id'] is nowhere to be found.

为什么?我究竟做错了什么?我只想完成这个项目...

Why? What am I doing wrong? I just want to finish this project...

更新:页面底部的var_dump($_SESSION);正在转储与我用于存储"user_id"$_SESSION不同的会话.我想知道是什么原因造成的?为什么会转储与我要在其中存储变量的会话不同的会话?

UPDATE: The var_dump($_SESSION); at the bottom of the page is dumping a different session than the $_SESSION that I'm using to store "user_id". I wonder what is causing this? Why would it dump a different session than the one I'm storing variables in?

session_id():0q44kc1fkph8fkc8gt35gfabe6

session_id():a493j3v5vbnlon02s1s7bpkqj3

所以...每次设置"user_id"时,它都在创建一个新会话?为什么?

So... It's creating a new session every time I set "user_id"? Why?

推荐答案

问题很可能在于您为会话启用了cookie,并且cookie的生存期为0.例如,让我们尝试将其设置为7天在脚本的开头:

The issue most likely lies in the fact that you have cookies enabled for sessions, and the cookie lifetime is 0. For example, let's try making it 7 days instead at the beginning of your script:

ini_set('session.cookie_lifetime', 60 * 60 * 24 * 7);

如果这行得通,我建议将session.cookie_lifetime设置移到您的php.ini文件中,这样编写的每个脚本都不需要运行ini_set().

If this works, I would recommend moving the session.cookie_lifetime setting to your php.ini file so each script you write does not require running ini_set().

有关更多信息: https://stackoverflow.com/a/9797962/823549

这篇关于为什么不能从whatwg-fetch请求中设置PHP $ _SESSION?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆