Khurram Ijaz
Khurram Ijaz

Reputation: 1864

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

Upvotes: 13

Views: 33160

Answers (6)

MixMax
MixMax

Reputation: 1

The desktop browser usually sends requests with the following header:

Sec-CH-UA-Mobile: ?0

The browser on the mobile device usually sends requests with the following header:

Sec-CH-UA-Mobile: ?1

So, you could check this using code like the following:

if (isset($_SERVER['HTTP_SEC_CH_UA_MOBILE']) &&
          $_SERVER['HTTP_SEC_CH_UA_MOBILE']=='?1')
{
   ...
}

Upvotes: 0

jotaelesalinas
jotaelesalinas

Reputation: 1497

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.

Upvotes: 0

Leon
Leon

Reputation: 139

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.

Upvotes: 1

Shameer
Shameer

Reputation: 3066

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;
}

Upvotes: 16

user501869
user501869

Reputation: 21

You can try The WURFL PHP API or Tera-Wurfl

Upvotes: 1

Mike Lewis
Mike Lewis

Reputation: 64147

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).

Upvotes: 12

Related Questions