【问题标题】:POST request with session id gets stuck without error带有会话 ID 的 POST 请求被卡住而没有错误
【发布时间】:2016-02-01 14:40:51
【问题描述】:

我正在尝试使用 php 创建一个简单易用的 API,但我遇到了多个问题。我正在开发一个执行基本 CRUD 内容的用户 api。

如果我使用 cURL 向我的用户 api (user.php) 发出 POST 请求,则会在执行 user.php 期间使用新的会话 ID。

为了解决这个问题,我尝试将当前会话 ID 与 POST 请求一起发送到 user.php。我现在遇到的问题是,在使用session_id($_POST['session']) 设置id 然后使用session_start() 开始我的会话后,我的服务器将在执行该代码时卡住并最终抛出内部服务器错误500。

我试图让我的服务器向我显示ini_set('display_errors', 1); 和一个 .htaccess 文件(内容:php_flag display_errors 1)的错误,但无济于事,服务器只是卡住了。

test.php

<?php
    session_start();

    // Check for available session
    if (!isset($_SESSION['id'])) {
        header('location: index.php');
    } else {
        // Initialize cURL
        $curl = curl_init();

        // Set parameters for POST request
        curl_setopt_array($curl, [
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => 'http://localhost/api/user.php',
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => http_build_query([
                'session' => session_id(),
                'username' => 'testuser',
                'password' => 'testalot',
                'name' => 'testuser',
                'isAdmin' => 0
            ])
        ]);

        // Execute POST request
        $response = curl_exec($curl);

        /* <<< Doesn't get beyond this point. */

        // Dump JSON
        var_dump($response);

        // Close cURL session
        curl_close($curl);
    }
?>

用户.php

<?php
  // Declare integer checking function
  function isInteger($input) {
    return ctype_digit(strval($input));
  }

  // Declare result object
  $output = ['success' => false, 'data' => [], 'error' => ''];

  // Action on POST
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Check if session id was sent
    if (!empty($_POST['session'])) {
      // Set session id
      session_id($_POST['session']);

      // Start session with received session id
      session_start();

      /* <<< Freezes at this point. */

      // Check session for available user id
      if (isset($_SESSION['id'])) {
        // Only admins are allowed to execute POST requests
        if ($_SESSION['isAdmin'] == 1) {
          // ... more code ...

我当然也会使用不同的方法来访问我的 API,不需要我发送当前会话 ID(如果有的话)。有什么想法吗?

更新:

在我的问题下查看@Neok 的评论。这就是解决方案。只是为可能有同样问题的其他人指出。

【问题讨论】:

  • 在页面开头添加此代码行以报告错误。它可能会有所帮助。 error_reporting(E_ALL);
  • 您可以尝试发送 $_SESSION['id'] 而不是 session_id() 吗?并注释掉 session_start();在用户 php?
  • @Neok 我正在调查。
  • 首先你必须检查 echo session_id()。它返回的东西?。

标签: php api session curl


【解决方案1】:

确保 php.ini 设置为记录所有错误

error_reporting(E_ALL);

ini_set('error_reporting', E_ALL);

【讨论】:

  • 我无法访问我正在使用的服务器上的 php.ini :( 我尝试使用 xampp 在本地运行所有内容,但这也没有帮助。没有抛出错误,执行只是卡住了......你可以说是无限循环。
  • 所以,在 php 中使用 'ini_set('error_reporting', E_ALL)' 进行操作
【解决方案2】:

PHP Documentation 表示session_id() 返回当前会话的会话 ID,如果没有当前会话(不存在当前会话 ID),则返回空字符串 ("")。所以不要使用$_SESSION['id'],试试这个:

// Check for available session
if (session_id() == "") {
    header('location: index.php');
} else {
    ...
}

否则您的代码会在循环中重定向到 index.php,从而导致错误 500。

【讨论】:

    猜你喜欢
    • 2016-03-07
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多