How To Detect A Mobile Device In PHP The Simplest Way.

Checking screen size these days is tricky because of retina devices. What is the simplest way to tell if a user is using a mobile device to browse my site using PHP? I have come across many classes that you can use but I was hoping for a simple if condition!

You could put a javascript in the page that checks the screen resolution, but that would be client-side: http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

The code detects a user based on the user-agent string by preg_match()ing words that are found in only mobile devices user-agent strings after hundreds of tests. It has 100% accuracy on all current mobile devices and I'm currently updating it to support more mobile devices as they come out. The code is called isMobile and is as follows:

I found this solution while trying to solve a similar issue on this forum development, it was successfull.
So, I thought of sharing it for others developers who may find it useful from time to come.

function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}


You can use it like this:

// Use the function
if(isMobile()){
// Do something for only mobile users
}
else {
// Do something for only desktop users
}
To redirect a user to your mobile site, I would do this:

// Create the function, so you can use it
function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
// If the user is on a mobile device, redirect them
if(isMobile()){
header("Location: http://m.yoursite.com/");
}


Original answers. http://www.justindocanto.com/scripts/detect-a-user-on-a-mobile-browser-or-device

You can add latest solution in the end of this answer. Let me know if you have any questions and good luck!

1
1
203
Welcome to Nairaland Script - Download from here www.bit.ly/nlscript


RE: How to detect a mobile device in PHP the simplest way.

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

0
Welcome to Nairaland Script - Download from here www.bit.ly/nlscript

Reply to this article


Welcome, Guest: Join to reply login to reply!

♣ Post Statistic

1 guest(s) viewing this topic

Related Post

​how To Use The Ubersuggest Chrome Extension How To Accessibly Disable A Form Button When Submitting With Ajax Using Vanilla Js How To Make Imgur Api Work On Localhost Using Php How To Download And Crack Skinfiner 2.0 For Life Time How To Build An Event Booking App Using Html, Css, Javascript, And Firebase How To Show And Hide Elements With Vanilla Javascript

♣ CodeXpress Statistic

Welcome, Guest: Join CodeXpress Login

17 Members, 135 topics and 9 posts
Date: Friday 01 July 2022 at 02:35 AM

Top 5 Contributors

People who started the most discussions on codexpress.

  1. 17
  2. 92
  3. 3
  4. 11
  5. 1

Sections

CodeXpress / General:
Politics, Business, Investment, Career, Autos, Counties, Properties, Agriculture, Health, Travel, Family, Culture, Religion, Food, Pets, Romance, Diaspora, Africa, International Forum, World List Directory, How To,
Science/Technology:
Programing, Webmasters, Computers, Phones, Art, Graphics & Video, Technology Market, CSS, Innovation, PHP 7x, Vanilla JS, Software Updates,

♣ Users Online: (1)

0 Members and 1 Guests online in last 5 minutes!


Sections:

Business, Diaspora, International Forum, Politics, Food, Vanilla JS, Innovation, Technology Market, Phones, Programing, Literature, Gaming, Poetry, Forum Games, Gossip,

CodeXpress - Copyright © 2019 - 2022 Marshall Unduemi. All rights reserved. See How To Advertise.
Disclaimer: Every CodeXpress member is solely responsible for anything that he/she posts or uploads on CodeXpress.