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 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

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
How to use Context in React?ReactHow to set opacity or transparency using CSS?CSSURL paths in DrupalDrupalWordPress: How to get field values in Advanced Custom Fields?WordPressHow to create a Star Ratings using CSS?CSSSolution to Could not cast value of type ‘NSTaggedPointerString’ to ‘NSNumber’Swiftfor @each loop in SCSSSCSSHow to get Current Year, Month and Date in JavaScript?JavaScriptHow to get query string values in JavaScript? (URL Manipulation using URLSearchParams)JavaScriptAccessing and Setting features of JavaScript ObjectsJavaScriptthe box-sizing property in CSSCSSHow to check if the page is the home page in WordPress?WordPress