A good way to start solving your problem is by identifying where PHP is registering the User-Agent variable. The User-Agent is an HTTP header and is sent on every request.
Start by making a page in PHP with the code:
<?php phpinfo(); ?>
This page comes with a lot of information about PHP: what version, whether it is installed on Apache server or any other and ... also has the headers and the User-Agent in turn passed by the browser! Look for your browser by doing a search with CTRL + F on the page.
Here in my example, we have the line:
PHP Variables
_SERVER["HTTP_USER_AGENT"] Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Notice that on my server, your code works! But it may be that your server is another, and so that variable may be different.
On something related, you can also make a code [1] and see all HTTP headers, including the User-Agent:
<?php
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
?>
But this only works on Apache-like servers.
[1] Source: link