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
What is the difference between Loosely Typed Language and Strongly Typed Language?MiscellaneousWhat’s Interpolation in SCSS (Sass)?SCSSRIGHT JOIN in PostgresPostgresHow to link/add CSS file to HTML Document?CSSThe 8 Golden Rules of Programming I Have Learned in My CareerMiscellaneousCasting in PostgreSQLPostgresWordPress: How to print ACF repeater field values?WordPressHow to create ‘share on LinkedIn’ link using just HTML?HTMLMiddleware in NextJSNextJSHow to Create a Copy of a Table in SQL and MySQL?SQL/MySQLKanban vs Scrum: The two frameworks of agile principlesMiscellaneousHow to Block IPs and User Agents using code in Drupal or WordPress?Drupal