Why can't I set PHP $_SESSION from a whatwg-fetch request?

南楼画角 提交于 2019-12-11 12:13:43

问题


I have a simple fetch request that looks like this:

JavaScript (React)

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);
})

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

PHP 5

<?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
}

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...

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...

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() on session I'm storing to: 0q44kc1fkph8fkc8gt35gfabe6

session_id() on session I'm dumping: a493j3v5vbnlon02s1s7bpkqj3

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


回答1:


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);

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().

For more information: https://stackoverflow.com/a/9797962/823549



来源:https://stackoverflow.com/questions/48836983/why-cant-i-set-php-session-from-a-whatwg-fetch-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!