2

I am trying to get the OS of the visitor in PHP(Codeingiter). I tried https://github.com/serbanghita/Mobile-Detect. It worked for me 70% of the time. But still it's not detecting accurately. A good amount of Android traffic is not detected as mobile or tablet.

entryton
  • 280
  • 5
  • 18
Maneesh M S
  • 332
  • 1
  • 4
  • 20

3 Answers3

3

There's no accurate way to detect the Operating system via php or any other server language.

This kind of detection normally relies on the browser agent, but it can be faked and most of the time does not have enought information.

Jaume Mussons Abad
  • 706
  • 1
  • 6
  • 20
  • I did 1 experiment. I put the goo.gl url instead of my original url. Then I can see they are tracking the OS perfectly without Javascript – Maneesh M S Jun 02 '17 at 06:22
  • I am redirecting to external websites my visitors according to the device type. Those websites also detecting these device type as mobile without having a javascript code. – Maneesh M S Jun 02 '17 at 06:34
1

You have to add checks for other devices like iPads and others that I missed...

$hua = filter_input(INPUT_SERVER, 'HTTP_USER_AGENT');
$os = 'I have no idea...';

if(preg_match('/android/i', $hua)) {
    $os = 'Android';
} elseif (preg_match('/linux/i', $hua)) {
    $os = 'Linux';
} elseif (preg_match('/iphone/i', $hua)) {
    $os = 'iPhone';
} elseif (preg_match('/macintosh|mac os x/i', $hua)) {
    $os = 'Mac';
} elseif (preg_match('/windows|win32/i', $hua)) {
    $os = 'Windows';
}
echo $os;
medicm
  • 60
  • 1
  • 9
  • Dear Friend, I used the same way before and after that I came along the library. See a UA https://github.com/serbanghita/Mobile-Detect/issues/662. This one refer to Android OS . – Maneesh M S Jun 02 '17 at 07:28
  • Its not possible for me to do via .htaccess. I have to redirect them through the php page itself. Because redirecting urls are dynamic – Maneesh M S Jun 02 '17 at 08:22
0

The User Agent Class Library in CodeIgniter Provides functions to carry out your task. You can find more details about them here : User Agent Class Library

Specifically look for platform() function to detect the OS of the visitor.

Viral Patel
  • 485
  • 1
  • 5
  • 11
  • I used the same in the code... Still its by passing ... and moreover this library is not 100% trustworthy. I can see the bot traffic is notdetecting prperly by this library. – Maneesh M S Jun 03 '17 at 06:58