how to check if the request came from mobile or computer in php

i need to check if the request came from a mobile phone device or desktop computer using php please help. thanks

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I am using a function to identify mobile browsers in my projects, which can detect almost all major Mobile Operating systems and browsers.

    function ismobile() {
    $is_mobile = '0';

    if(preg_match('/(android|up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
        $is_mobile=1;
    }

    if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
        $is_mobile=1;
    }

    $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
    $mobile_agents = array('w3c ','acs-','alav','alca','amoi','andr','audi','avan','benq','bird','blac','blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno','ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-','maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-','newt','noki','oper','palm','pana','pant','phil','play','port','prox','qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar','sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-','tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp','wapr','webc','winw','winw','xda','xda-');

    if(in_array($mobile_ua,$mobile_agents)) {
        $is_mobile=1;
    }

    if (isset($_SERVER['ALL_HTTP'])) {
        if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
            $is_mobile=1;
        }
    }

    if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
        $is_mobile=0;
    }

    return $is_mobile;
}

Solution 2

Check the $_SERVER['HTTP_USER_AGENT'] for mobile user agents.

You should check out http://detectmobilebrowser.com/ for already existing scripts to detect for mobile browsers (it just uses the user agents).

Solution 3

MobileESP is comprehensive but nice & easy to use.

Solution 4

You can try The WURFL PHP API or Tera-Wurfl

Solution 5

http://mobiledetect.net/ is another lightweight php class, but you mist keep in mind that checking User-agent is a good but not perfect way, the issue is rules are constantly out-dated and incomplete so you need to change the detection code continuously.
Also check https://modernizr.com/ for another way to detect.

Solution 6

Nowadays, what do you consider “a mobile device”?

User agent parsers will give very good results, except for rare edge cases. The problem is that you have to constantly update them if you store the data locally or depend on the service being online if you use it “in the cloud”.

It is better to use a functionality detection library, e.g. Modernizr, send to your server information about the browser capabilities on first visit and serve appropriate content based on what the browser can or cannot do. Or even better, delegate that to Javascript.

Solution 7

This is the simplest way to do it in PHP.

if(stristr($_SERVER['HTTP_USER_AGENT'],'Mobile')){
    //Put code here when device is mobile
}else{
    //Put code here when device is not a mobile
}

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply