Jump to content

Recommended Posts

Slowly working through errors when trying PHP 8, I'm getting Undefined array key "HTTP_ACCEPT_CHARSET", the code snippet in question : -

 

function CreateSignature()
	{
		$sig = @$_SERVER["HTTP_USER_AGENT"].@$_SERVER["HTTP_ACCEPT"].@$_SERVER["HTTP_ACCEPT_LANGUAGE"].@$_SERVER["HTTP_ACCEPT_CHARSET"];
		$this->Signature = md5(Communication::GetIP() . $sig);
	}

I'm guessing the @ is the issue but how to fix?

@ will suppress errors/notifications. The problem you're running in to is that $_SERVER['HTTP_ACCEPT_CHARSET'] isn't set and therefor can't be used; you're seeing the error despite the @ operator because it won't suppress fatal errors. Your best bet is to use isset() to check each $_SERVER array key and then only use the ones that exist to build the key.

1 hour ago, maxxd said:

you're seeing the error despite the @ operator because it won't suppress fatal errors

But it isn't a fatal error, just a warning.

    echo "Version " . phpversion() . '<br><br>';
    
    $keys = ['HTTP_USER_AGENT', 'HTTP_ACCEPT', 'HTTP_ACCEPT_LANGUAGE', 'HTTP_ACCEPT_CHARSET'];

    foreach ($keys as $k) {
        echo "<b>$k</b>
              <br>
              {$_SERVER[$k]}
              <br><br>";
    }

Outputs ...

image.png.9120c4aa004c34a4cd974156910be595.png

Further, using the "@" suppresses the warning as expected when I run the OP's code

PS An alternative to using the "@" could be

 $keys = ['HTTP_USER_AGENT', 'HTTP_ACCEPT', 'HTTP_ACCEPT_LANGUAGE', 'HTTP_ACCEPT_CHARSET'];
    $sig = '';
    foreach ($keys as $k) {
        $sig .= $_SERVER[$k] ?? '';
    }

 

The programs error log gives me this : - 24.01.23 13:48:36 31.111.2.99 ERR# 2 Undefined array key "HTTP_ACCEPT_CHARSET" /home/website/public_html/online-support/_lib/objects.global.users.inc.php IN LINE 2175

 

Using your code example: -

Version 7.4.33

HTTP_USER_AGENT
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0

HTTP_ACCEPT
text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8

HTTP_ACCEPT_LANGUAGE
en-GB,en;q=0.5

HTTP_ACCEPT_CHARSET

 

Version 8.0.26

HTTP_USER_AGENT
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0

HTTP_ACCEPT
text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8

HTTP_ACCEPT_LANGUAGE
en-GB,en;q=0.5


Warning: Undefined array key "HTTP_ACCEPT_CHARSET"
HTTP_ACCEPT_CHARSET

 

I don't seem to be able to suppress the error with @?

OK, changing the original file to this : -

function CreateSignature()
	{
		$keys = ['HTTP_USER_AGENT', 'HTTP_ACCEPT', 'HTTP_ACCEPT_LANGUAGE', 'HTTP_ACCEPT_CHARSET'];
		$sig = '';
		foreach ($keys as $k) {
        $sig .= $_SERVER[$k] ?? '';
    }
		//$sig = @$_SERVER["HTTP_USER_AGENT"].@$_SERVER["HTTP_ACCEPT"].@$_SERVER["HTTP_ACCEPT_LANGUAGE"].@$_SERVER["HTTP_ACCEPT_CHARSET"];
		$this->Signature = md5(Communication::GetIP() . $sig);
	}

Cleared that error, now the programs logs have this error : -

24.01.23 15:00:38 31.111.2.99 ERR# 2 Undefined array key "p_gl_t" /home/website/public_html/online-support/_lib/objects.internal.inc.php IN LINE 303

 

Line 303 : -

if(($hash = substr(md5(Server::$Response->Typing),0,5)) != @$_POST["p_gl_t"] && strlen(Server::$Response->Typing) > 0)

😪

15 hours ago, ginerjm said:

You really should stop using the @ sign in any php code.  No sense in it.  If there is an error to be hidden it is better to program to handle it than ignore (hide) it.

This isn't my code, it's a third party chat system which I'm trying to get to work with PHP 8. I ALWAYS try & fix errors hence this thread!

How does this look?

 

$hash = substr(md5(Server::$Response->Typing),0,5);
if (!is_null($hash) && isset($_POST["p_gl_t"]) && $hash != $_POST["p_gl_t"] && strlen(Server::$Response->Typing) > 0) {
    Server::$Response->XML .= "<gl_typ h=\"".base64_encode($hash)."\">\r\n" . Server::$Response->Typing . "</gl_typ>\r\n";
}

 

The @ symbol in front of the variables in this code is called an error suppression operator. It is used to suppress error messages that would be generated when trying to access an undefined array key. However, this can make it difficult to identify and fix errors in your code.

Instead of using the error suppression operator, you can check if the key exists in the array before trying to access it. You can use the isset() function to check if a variable is set, and the array_key_exists() function to check if a key exists in an array.

Here is an example of how you can update the code to check for the existence of the key before accessing it:

function CreateSignature()
{
    $sig = "";
    if (isset($_SERVER["HTTP_USER_AGENT"])) {
        $sig .= $_SERVER["HTTP_USER_AGENT"];
    }
    if (isset($_SERVER["HTTP_ACCEPT"])) {
        $sig .= $_SERVER["HTTP_ACCEPT"];
    }
    if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
        $sig .= $_SERVER["HTTP_ACCEPT_LANGUAGE"];
    }
    if (isset($_SERVER["HTTP_ACCEPT_CHARSET"])) {
        $sig .= $_SERVER["HTTP_ACCEPT_CHARSET"];
    }
    $this->Signature = md5(Communication::GetIP() . $sig);
}

You can also use the ternary operator to shorten the code

function CreateSignature()
{
    $sig = isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : "";
    $sig .= isset($_SERVER["HTTP_ACCEPT"]) ? $_SERVER["HTTP_ACCEPT"] : "";
    $sig .= isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]) ? $_SERVER["HTTP_ACCEPT_LANGUAGE"] : "";
    $sig .= isset($_SERVER["HTTP_ACCEPT_CHARSET"]) ? $_SERVER["HTTP_ACCEPT_CHARSET"] : "";
    $this->Signature = md5(Communication::GetIP() . $sig);
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.