Browser Detection in PHP: Identify Chrome, Firefox, Safari, and More


Browser Detection Script


This code detects if the user is browsing with Firefox, Internet Explorer, Chrome, Safari, Edge, or any other browser.

PHP Code
  1. <?php
  2. // Get the browser's user agent string
  3. $browser = $_SERVER['HTTP_USER_AGENT'];
  4.  
  5. // Check for specific browsers
  6. if (strpos($browser, 'Firefox') !== false) {
  7. echo "You are using Mozilla Firefox.";
  8. }
  9. else if (strpos($browser, 'MSIE') !== false || strpos($browser, 'Trident') !== false) {
  10. echo "You are using Internet Explorer.";
  11. }
  12. else if (strpos($browser, 'Edge') !== false || strpos($browser, 'Edg') !== false) {
  13. echo "You are using Microsoft Edge.";
  14. }
  15. else if (strpos($browser, 'Chrome') !== false && strpos($browser, 'Safari') !== false && strpos($browser, 'Edg') === false) {
  16. echo "You are using Google Chrome.";
  17. }
  18. else if (strpos($browser, 'Safari') !== false && strpos($browser, 'Chrome') === false) {
  19. echo "You are using Apple Safari.";
  20. }
  21. else {
  22. echo "You are using a different browser.";
  23. }
  24. ?>




How It Works


  • Firefox: Looks for "Firefox" in the user agent string.
  • Internet Explorer: Detects "MSIE" or "Trident" (used in IE 11+).
  • Microsoft Edge: Checks for "Edge" or "Edg" (modern Edge uses "Edg").
  • Google Chrome: Confirms "Chrome" and "Safari" appear together but excludes "Edg" (to avoid misidentifying Edge).
  • Apple Safari: Detects "Safari" but ensures "Chrome" is not present.
  • Other Browsers: Falls back to a default message for unrecognized user agents.




Conclusion


This script provides basic detection for major browsers,
User agent detection is helpful to identify which browsers the user are using for statistics.
Diablosblizz's Avatar
Views:
2,835
Rating:
Posted on Sunday 22nd December 2024 at 12:50 PM
DanielXP
DanielXP's Avatar
Updated to detect keywords in the user agent vs detecting the whole user agent, plus added a few extra common browsers to bring this up to date 😁
Posted on Sunday 23rd September 2007 at 08:40 PM
darklight19
darklight19's Avatar
This won't work if you have a tool bar installed that renames the user agent.
Posted on Thursday 24th May 2007 at 11:58 PM
ShadowMage
ShadowMage's Avatar
no prob
Posted on Thursday 24th May 2007 at 11:40 PM
Jon
Jon's Avatar
nvm fixed it, I changed the "en-US" to "en-GB"...

Thanks guys.
Posted on Thursday 24th May 2007 at 11:37 PM
Jon
Jon's Avatar
Yes
Posted on Thursday 24th May 2007 at 11:32 PM
ShadowMage
ShadowMage's Avatar
it works for me also, does your host support PHP?
Posted on Thursday 24th May 2007 at 11:15 PM
Jon
Jon's Avatar
I'm on Firefox.
Posted on Thursday 24th May 2007 at 05:44 AM
SkillMaster
SkillMaster's Avatar
Works fine for me. what browser are you using it seems to work fine in IE and mozilla for me.
Posted on Wednesday 23rd May 2007 at 09:21 PM
Jon
Jon's Avatar
Basically, nothing shows up when I go on it:

https://scription.net/browser.php
Posted on Friday 18th May 2007 at 09:45 PM
SkillMaster
SkillMaster's Avatar
Yes it does work for me. :S what is your exact problem?