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
PHP Forms Handling
PHP Send Mail
PHP header() Function
PHP mail() Function
Exercises & Assignments
Create a PHP Form with 2 fields Name and Hobbies
Interview Questions & Answers
What are the form method Attribute available for submitting?
How can we submit a form without a submit button?
What is the difference between GET and POST method in PHP?
How to redirect a page in PHP?
How to get the browser details of a visitor in PHP?
How to get the previous page (referrer) URL in PHP?
How to get the IP address of a visitor in PHP?