Browser Detector Script in PHP Free Source code and Tutorial

Browser Detector Script in PHP Free Source code and Tutorial

Introduction

This tutorial will teach you how to detect your browser using PHP. This can be useful for customizing your web page to display different content depending on which browser is being used. You will need to have a basic understanding of PHP syntax to follow this tutorial.

In order to detect your browser using PHP, we need to first understand the various functions that are available. In PHP, there is a global variable known as $_SERVER. This variable is mostly used for printing the server and the environment information.

This article will guide you through the development of detecting the browser in PHP. Let’s get started with the development.

Objectives

By the end of this tutorial, you will be able to:

  1. Create a PHP script that detects and display the browser used by user.
  2. Use the necessary function to retrieve the server or browser information.
  3. To integrate and apply the source code in your projects.

Relevant Source code

Requirements

  • XAMPP
  • Text editor (VS Code, Sublime, Brackets), download and install a text editor of your choice
Browser Detector Script in PHP Free Source code and Tutorial - source code
Browser Detector Script in PHP Free Source code and Tutorial – source code

Line 19-33 – this is the PHP script that will detect and display the browser used.

Line 20-31 – we have created a function and the name is detect.

Line 21 – we have declared a variable $info that will hold the value of $_SERVER.

$_SERVER is a PHP variable that holds all information about the web server. It’s referred to as a superglobal.

The user agent string is a text that browsers transmit to the webserver to identify themselves, allowing websites to provide alternative content depending on the browser or browser compatibility.

The strpos() function finds the position of the first occurrence of a string inside another string.

Line 32 – we are ready to call the function. Take note that a function or a method will not run automatically until it is being called.

Browser Detector Script in PHP Free Source code and Tutorial - output
Browser Detector Script in PHP Free Source code and Tutorial – output
<?php
function detect(){
$info = $_SERVER['HTTP_USER_AGENT'];
if(strpos($info,"Chrome") == true)
echo "<h4>You are using Google Chrome</h4>";
if(strpos($info,"Firefox") == true)
echo "<h4>You are using Firefox</h4>";
elseif(strpos($info,"Trident/7") == true)
echo "<h4>You are using Internet Explorer</h4>";
else{
echo "<h4>Other Browser</h4>";
}
}
detect();
?>

Complete Source Code

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Browser Detector in PHP</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
</head>

<body>
<div class="row">
<div class="col-md-8 offset-md-2">
<div class="card">
<div class="card-header bg">
<h1>Browser Detector in PHP</h1>
</div>
<div class="card-body">
<?php
function detect(){
$info = $_SERVER['HTTP_USER_AGENT'];
if(strpos($info,"Chrome") == true)
echo "<h4>You are using Google Chrome</h4>";
if(strpos($info,"Firefox") == true)
echo "<h4>You are using Firefox</h4>";
elseif(strpos($info,"Trident/7") == true)
echo "<h4>You are using Internet Explorer</h4>";
else{
echo "<h4>Other Browser</h4>";
}
}
detect();
?>
</div>
</div>
</div>
</div>
<script src="assets/js/jquery.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

Video Demo

Browser Detector Script in PHP Free Source code and Tutorial
Browser Detector Script in PHP Free Source code and Tutorial

FREE DOWNLOAD SOURCE CODE

FREE DOWNLOAD PDF TUTORIAL

Summary

This tutorial covers the explanation of relevant source code used in creating a PHP script that detects the user’s browser. For the complete reference please watch the video presentation on how to create a browser detector in PHP.

We hope you found this tutorial to be helpful! Wishing you the best of luck with your projects! Happy Coding!

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive  free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

Related Topics and Articles:

How to Generate Random Password in PHP Free Source code and Tutorial

Calculate Sum of Column in PHP and MySQL

Email Validation in PHP Free Source code and Tutorial

Image Upload in PHP and MySQL Free Tutorial and Source code

Password Indicator in PHP Free Source code and Tutorial

, , , , , , , , , , , , , ,

Post navigation