【问题标题】:PHP Deprecated: header(): php 8.014 [closed]不推荐使用 PHP:header():php 8.014 [关闭]
【发布时间】:2022-01-25 13:35:22
【问题描述】:

当我在我的管理员中单击注销时,此错误会出现在我的日志中。 我不明白什么是弃用的 php 8.0.14 谢谢你的帮助

我的功能

 public static function redirect(?string $url, ?string $http_response_code = null)
    {
      if ((strstr($url, "\n") === false) && (strstr($url, "\r") === false)) {
        if (str_contains($url, '&')) {
          $url = str_replace('&', '&', $url);
        }

        header('Location: ' . $url, true, $http_response_code);
      }

      exit;
    }

错误

[25-Dec-2021 15:32:00 UTC] PHP Deprecated:  header(): Passing null to parameter #3 ($response_code) of type int is deprecated in /home/www/test/includes/OM/HTTP.php on line 63
[25-Dec-2021 15:32:00 UTC] PHP Stack trace:
[25-Dec-2021 15:32:00 UTC] PHP   1. {main}() /home/www/test/admin/login.php:0
[25-Dec-2021 15:32:00 UTC] PHP   2. OM\OM::redirect() /home/www/test/admin/test.php:133
[25-Dec-2021 15:32:00 UTC] PHP   3. OM\HTTP::redirect($url = 'http://localhost/test/admin/index.php', $http_response_code = *uninitialized*) /home/www/test/includes/OM/::OM.php:329
[25-Dec-2021 15:32:00 UTC] PHP   4. header($header = 'Location: http://localhost/test/admin/index.php', $replace = TRUE, $response_code = NULL) /home/www/test/includes/OM/HTTP.php:63
[25-Dec-2021 15:32:12 UTC] PHP Deprecated:  header(): Passing null to parameter #3 ($response_code) of type int is deprecated in /home/www/test/includes/admin/OM/HTTP.php on line 63
[25-Dec-2021 15:32:12 UTC] PHP Stack trace:
[25-Dec-2021 15:32:12 UTC] PHP   1. {main}() /home/www/test/admin/login.php:0
[25-Dec-2021 15:32:12 UTC] PHP   2. OM\OM::redirect('index.php', '') /home/www/test/admin/login.php:100
[25-Dec-2021 15:32:12 UTC] PHP   3. OM\HTTP::redirect($url = 'http://localhost/admin/admin/index.php', $http_response_code = *uninitialized*) /home/www/test/includes/OM/OM.php:329
[25-Dec-2021 15:32:12 UTC] PHP   4. header($header = 'Location: http://localhost/test/admin/index.php', $replace = TRUE, $response_code = NULL) /home/www/test/includes/OM/HTTP.php:63

【问题讨论】:

  • 不推荐将null 作为响应代码参数传递的行为——正如错误消息所说的那样

标签: php php-8


【解决方案1】:
public static function redirect(?string $url, ?string $http_response_code = null)

必须

public static function redirect(?string $url, int $http_response_code = 0)

【讨论】:

    【解决方案2】:

    尝试用此代码替换:

    public static function redirect(?string $url, ?string $http_response_code = null)
    {
        if ((strstr($url, "\n") === false) && (strstr($url, "\r") === false)) {
            if (str_contains($url, '&')) {
                $url = str_replace('&', '&', $url);
            }
    
            if ($http_response_code === null) {
                header('Location: ' . $url);
            } else {
                header('Location: ' . $url, true, $http_response_code);
            }
        }
    
        exit;
    }
    

    header 函数的第三个参数不能是 null 类型,因此是不推荐使用的消息。

    【讨论】:

    • 您在函数 redirect() 上的第三个参数仍然接受字符串。 header() 仅接受 response_code 参数作为整数。您的条件if ($http_response_code === null) 表示如果开发人员将第三个参数输入为'blabla',那么它将作为header('Location: url', true, 'blabla'); 发送,并且错误仍然存​​在。最好在if条件下更改类型提示并检查变量类型。
    • @vee,我完全同意。在答案中,我更喜欢保留原始方法签名。恕我直言,这已经超出了这个问题的范围。
    猜你喜欢
    • 1970-01-01
    • 2018-08-03
    • 2018-12-21
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多