Yogesh Chauhan's Blog

How to detect if browser supports WebP format on server side PHP script?

in PHP on April 25, 2020

We can make use of PHP global variable $_SERVER to do so.

Here’s another related post about PHP global variable $_SERVER.

How to get the full URL of current page in PHP?

Using $_SERVER[‘HTTP_ACCEPT’] we can get the detailed contents of the Accept: header, if there is one available.

Then we can use PHP strpos() function to check if it has image/webp in it. If so, it’s supported.

Here’s how we can do that:



if( strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) === true ) {
    // webp is supported!
}


Just change the true to false if you want to check if webp is not supported OR just use else in the previous example.



if( strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) === false ) {
    // webp is supported!
}


Use this code to check if webp is supported or the browser is chrome



if( strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) == true || strpos( $_SERVER['HTTP_USER_AGENT'], ' Chrome/' ) == true ) {
    // webp is supported!
}


Credit: Stack Overflow


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
SQL ALL OperatorSQL/MySQLVariables and Distance Functions in SCSS (Sass)SCSSForcing the domain to serve securely using HTTPSMiscellaneousHow to Access a Global Variable From Inside a Function in PHP?PHPThe :last-of-type selectorCSSThe order Property in Flex Items in CSSCSSHow to vertically align a html radio button to it’s label?CSSIs Software Really Eating The World? I don’t think soMiscellaneousHow to Make a Simple Module with a Form and Menu Link in Drupal 7.x?DrupalIs monitoring an employee’s work on a computer a desirable or undesirable activity?MiscellaneousHow to Recognize an Array in JavaScript?JavaScriptHow to enable GD library support for PHP on windows server?PHP