How to get the browser details of a visitor in PHP?

The browser sends a text called user agent with every request. In PHP you can read it from $_SERVER["HTTP_USER_AGENT"].

<?php
echo $_SERVER["HTTP_USER_AGENT"];
?>

Output:

Sample output, yours will be different

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

PHP also has the get_browser() function, which tells the browser name, version and platform in an array. But it needs a browscap.ini file configured in php.ini, which is not available on most servers.

  • The user agent is sent by the browser, so a visitor can change or fake it.
  • Do not use it for security. For features like JavaScript or cookies, test the feature in the browser instead of checking the browser name.

Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.