0

I am here to ask about an script for getting user device i.e. Android or iOS or Web. I have tried something like but in android phone it showing Linux.

$agent = $_SERVER["HTTP_USER_AGENT"];
//print_r($agent);

if (preg_match('/Linux/', $agent))
$os = 'Linux';
elseif (preg_match('/Windows/', $agent))
$os = 'Windows';
elseif (preg_match('/Mac/', $agent))
$os = 'Mac';
elseif (preg_match('/Android/', $agent))
$os = 'Android';
elseif (preg_match('/Apple/', $agent))
$os = 'Apple';
else
$os = 'UnKnown';

echo $os;
iam
  • 973
  • 4
  • 11
  • 21

1 Answers1

0

As I stated in the comment, Androids have a linux kernel, so to again find if, the OS is specifically Android, Do something like:

$agent = $_SERVER["HTTP_USER_AGENT"];
//print_r($agent);

if (preg_match('/Linux/', $agent))
{
    $os = 'Linux';
    if (preg_match('/Android/', $agent))
    {
        $os = 'Android';
    }
}
elseif (preg_match('/Windows/', $agent))
{
    $os = 'Windows';
}
elseif (preg_match('/Mac/', $agent))
{
    $os = 'Mac';
}
elseif (preg_match('/Apple/', $agent))
{
    $os = 'Apple';
}
else
{
    $os = 'UnKnown';
}

echo $os;

What it does, is, that, when it gets, that the (OS) is linux, then it also checks if the the OS is android too.

In what you've used is, whenever it finds the (OS) is linux, it puts the value linux in the $os and exits all those bunches of elseif's. So it can't enter the elseif (preg_match('/Windows/', $agent)) {...}.

Hope it helps you!

Ikari
  • 3,176
  • 3
  • 29
  • 34