: How can I stop this PHP Notice or fix the problem that clutters up the debug.log after switching to PHP 7? EDIT: I am the administrator of several websites and when I turn debug on to track
EDIT: I am the administrator of several websites and when I turn debug on to track down a problem on one of the sites, the debug.log is cluttered with hundreds of lines of PHP notices each day about a PHP problem in a plugin. The repeated notices obscure the debug information I am looking for to fix an important problem. I tried contacting the author of the plugin through the plugin's support forum to get a fix so I can stop the buildup of the log but there are no responses to questions in the forum for the plugin.
What do I need to do to suppress or fix this undefined index error in the WordPress plugin so it stops adding hundreds of PHP notices in the debug.log when I have debug turned on?
The plugin has a function to check if the browser is mobile. Since switching to PHP 7, I started getting the following PHP Notice:
Undefined index: HTTP_ACCEPT in /plugins/dynamic-to-top/inc/dynamic-to-top-class.php on line 440
This notice was not generated with PHP 5.6 so I thought something had changed in PHP 7 for this line to generate that notice? The answer below says it is not a change in PHP 7 that generated the notice but a more thorough reporting method. The following is the line that is called out in the notice.
if( preg_match( "/wap.|.wap/i", $_SERVER["HTTP_ACCEPT"] ) )
return true;
I checked the PHP Manual and HTTP_ACCEPT is a correct element for $_SERVER.
The full function is
function is_mobile() {
if( isset( $_SERVER["HTTP_X_WAP_PROFILE"] ) )
return true;
if( preg_match( "/wap.|.wap/i", $_SERVER["HTTP_ACCEPT"] ) )
return true;
if( isset( $_SERVER["HTTP_USER_AGENT"] ) ) {
$user_agents = array(
"midp", "j2me", "iphone", "avantg", "docomo", "novarra", "palmos",
"palmsource", "240x320", "opwv", "chtml", "pda", "windows ce", "mmp/",
"blackberry", "mib/", "symbian", "wireless", "nokia", "hand", "mobi",
"phone", "cdm", "up.b", "audio", "SIE-", "SEC-", "samsung", "HTC",
"mot-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC",
"philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover",
"pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal",
"kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "dddi", "moto" );
foreach( $user_agents as $user_string ) {
if( preg_match( "/" . $user_string . "/i", $_SERVER["HTTP_USER_AGENT"] ) )
return true;
}
}
do_action( 'mv_dynamic_to_top_check_mobile' );
return false;
}
Why isn't it a defined index?
More posts by @Shakeerah822
1 Comments
Sorted by latest first Latest Oldest Best
HTTP_ACCEPT may well be the correct environment var name, but that header won't necessarily be set, which is the cause of the warning. This is not a difference between PHP 5.6 and PHP 7 but more a difference in PHP's error reporting setting (the default of which might have changed between versions).
Since the header may not be set, checking whether or not it is before performing the regular expression check will fix the warning:
if( isset($_SERVER["HTTP_ACCEPT"]) && preg_match( "/wap.|.wap/i", $_SERVER["HTTP_ACCEPT"] ) )
return true;
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.