3

我正在尝试拥有一个安全的登录页面,该页面在使用 XAMPP 时运行良好,但现在我已切换到 Docker,我收到此错误:

Warning: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time in /var/www/inc/cUser.php on line 30

我不确定,如果我需要对容器本身进行一些设置,但到目前为止我还没有找到解决方案

我从表单中获取 $_POST 数据并将其传递给我的 process.php:

require_once(dirname($_SERVER['DOCUMENT_ROOT']) . '/inc/autoloader.php');
  $user = new cUser();

  $user -> sec_session_start();

cUser.php:

protected function sec_session() {
    define("SECURE", true);
    $session_name = 'sec_session_id';   
    $secure = SECURE;
    $httponly = true;
    if (ini_set('session.use_only_cookies', 1) === FALSE) {
    //header("Location: /error.php?err=Could not initiate a safe session (ini_set)"); 
// here i got the same error twice, so i commented it out and used the code below
        echo("<script>location.href = /error.php?err=Could not initiate a safe session (ini_set);</script>");
            exit();
        }
        $cookieParams = session_get_cookie_params();
        session_set_cookie_params($cookieParams["lifetime"],
            $cookieParams["path"],
            $cookieParams["domain"],
            $secure,
            $httponly);
        session_name($session_name);
        session_start();  
        session_regenerate_id();
    }

以上工作正常,我回到我的 process.php:

if ($login) {
              // Login successful
              exit(header('Location: /index.php'));
          } else {
              // Login not successful
              exit(header('Location: /error.php?error=1'));
          }

我成功地重定向到 index.php。在我的 index.php 中,我再次调用相同的方法,以确保我拥有正确的用户并有权查看该页面:

require_once(dirname($_SERVER['DOCUMENT_ROOT']) . '/inc/autoloader.php');
  $user = new cUser();

  $user -> sec_session_start();

现在我偶然发现了错误。这是行:

if (ini_set('session.use_only_cookies', 1) === FALSE) {

我不确定这里有什么问题,有人可以帮我吗?

4

1 回答 1

1

我认为关键是要了解导致错误Headers already sent的原因,以便深入了解您的错误是什么。

当有人请求一个网页时,在发送任何形式的 HTML 之前页面的第一位是一组标头。这些键值对包含有关页面的关键信息,例如状态码(200 表示成功,404 表示未找到等)。

它还包含 cookie 信息,这是 PHP 会话如何工作的关键(我怀疑你可能知道你正在执行的命令)。

由于标头是在请求开始时发送的,因此如果您将任何与标头无关的输出(甚至是<?php标签之外的换行符)放在页面上,这将阻止发送任何其他标头并抛出您的错误正在看。

鉴于您看到的症状(这以前有效,在 Docker 中无效),最可能的原因是在错误报告方面存在配置差异(Docker 容器中的错误报告设置为高于它在 XAMPP 中)或 PHP 版本(可能在 Docker 容器中使用比在 XAMPP 上更新的 PHP 版本)。

您发布的所有代码看起来都不会出错,所以我假设您的autoloader.php文件中发生了这个潜在的错误。

于 2019-09-02T22:03:18.457 回答