ohno Posted January 24, 2023 Share Posted January 24, 2023 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? Quote Link to comment Share on other sites More sharing options...
maxxd Posted January 24, 2023 Share Posted January 24, 2023 @ 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. Quote Link to comment Share on other sites More sharing options...
ohno Posted January 24, 2023 Author Share Posted January 24, 2023 Thanks, that's what I thought but unsure how to code it? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 24, 2023 Share Posted January 24, 2023 if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) Quote Link to comment Share on other sites More sharing options...
Barand Posted January 24, 2023 Share Posted January 24, 2023 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 ... 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] ?? ''; } Quote Link to comment Share on other sites More sharing options...
ohno Posted January 24, 2023 Author Share Posted January 24, 2023 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 @? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 24, 2023 Share Posted January 24, 2023 Did you try the alternative that I added? Quote Link to comment Share on other sites More sharing options...
ohno Posted January 24, 2023 Author Share Posted January 24, 2023 I did but I still get : - Warning: Undefined array key "HTTP_ACCEPT_CHARSET" in /home/website/public_html/test.php on line 12 HTTP_ACCEPT_CHARSET Quote Link to comment Share on other sites More sharing options...
ohno Posted January 24, 2023 Author Share Posted January 24, 2023 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) 😪 Quote Link to comment Share on other sites More sharing options...
Barand Posted January 24, 2023 Share Posted January 24, 2023 Have you checked the content of the $_POST array? Have you checked if any data been posted to that page? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 24, 2023 Share Posted January 24, 2023 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. Quote Link to comment Share on other sites More sharing options...
maxxd Posted January 25, 2023 Share Posted January 25, 2023 13 hours ago, Barand said: But it isn't a fatal error, just a warning. Huh. I could've sworn I read somewhere that non-existent array indexes were promoted to fatal errors - I must be mistaken. Quote Link to comment Share on other sites More sharing options...
ohno Posted January 25, 2023 Author Share Posted January 25, 2023 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! Quote Link to comment Share on other sites More sharing options...
ohno Posted January 25, 2023 Author Share Posted January 25, 2023 15 hours ago, Barand said: Have you checked the content of the $_POST array? Have you checked if any data been posted to that page? Not sure how I can check this? The error appears if a webpage with the chat icon is accessed/refreshed. Quote Link to comment Share on other sites More sharing options...
ohno Posted January 26, 2023 Author Share Posted January 26, 2023 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"; } Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 27, 2023 Share Posted January 27, 2023 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); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.