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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to add multiple Columns in CSS like Bootstrap?CSSContent types in DrupalDrupalThe actual difference between indexOf() and search() in JavaScriptJavaScriptHow to insert Bootstrap 4 in Angular 9 app?AngularWhat is IPS(Intrusion Prevention System), How Does It Work and What are the Detection Types?MiscellaneousHow does while loop work in SCSS?SCSSHow to convert an HTML radio buttons into a toggle switch using CSS?CSSHow to select an element using its ID without the high specificity of the ID selector?CSSHow to render Lists in React?ReactWhat’s a Web Storage API in JavaScript?JavaScript4 different ways to create JavaScript ObjectsJavaScriptUse inline if to make a shorter conditional syntax in ReactReact