Home > Net >  PHP Deprecated: header(): php 8.014
PHP Deprecated: header(): php 8.014

Time:12-26

When I click on log Off in my admin, this error aooears in my log. I do not understand what is deprecated php 8.0.14 thank you for your help

my function

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

the error

[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

CodePudding user response:

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

must be

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

CodePudding user response:

Try to replace by this code:

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

The third argument of the header function must not be of type null, hence the deprecated message.

  • Related