Friday, 21 March 2014

Detecting a Visitors Browser

detect browsers using php
Detecting a visitors browser by php programming language. Firstly we will go through basic setups however to detect visitors browser. At first state you need to obtain a file called php.ini will be found in directory inside xampp or wamp which ever you using for php Apache package and you also had to modify your php.ini file and also you need to find file called browsercap.ini. If you don't find such file don't worry find on internet you will get such file re-download it.

php.ini file
xampp/php Screenshot1

browscap.ini file
xampp/php/browscap.ini Screenshot2

Now go to php.ini (Screenshot3)

php.ini Screenshot3

find [browscap]
browscap = ""


Change it to the broscap value to the location where your browscap.ini file exit for example my file is 

[browscap]
browscap = "C:\xampp\php\browscap.ini"


Now open xammp control panel restart services for Apache server therefore now our php.ini setting have been initialize  by our reset of xampp server.

Code for detecting a visitors browser (Screenshot4)

Detecting Visitors Browser in php
Detecting Visitors Browser Screenshot4




<?php



$browser = get_browser(null, true);

print_r($browser);



?>

Output(Screenshot5):-

detecting visitors browser
Screenshot5
For getting data of visitors browser i will introduce you to get_browser function. get_browser function takes two arguments. The first argument is the user agent we could obtain $agent = $_SERVER['HTTP_USER_AGENT'];. But here we will just pass value of null and the second argument that is we want to be return data in array or not and we do want to return in array so i had passed true we also can pass value 1 but true is better way to return because that how we can use the data then using print_r function we had echo out our array of visitors browser data. 

So, you can see it return quite few information about my browser in screenshot5. Now you see this Chrome 10.0. If you not seen such information in browser you may incorrectly downloaded browscap.ini or given wrong path into php.ini file.

Why we may want to detect the visitors browser. Its always important part as you viewing webpage format things correctly for users also you may want to notify user using another browser may be better to display your page. When you are creating a webpage it may be looks different in another browsers and if you want to say to user download google chrome it will look better then you recommend say users to switch there browser to view your page view lot better.

How we can do this by firstly we detect the visitors browser the display them message that you using this browser please download google chrome to view this website correctly and in lot better webpage may be better display into google chrome. 

Now as you see screenshot5 of browser it print every element of the array with key. It not make sense of the information. When you see all element inside you will find key browser and value is chrome because i am using chrome to display. If we want to display only browser name of the visitors there is very very useful key to get the browser name to the visitors

Code for detecting a visitors browser (Screenshot6):-

detecting visitors browser
Screenshot6


<?php



$browser = get_browser(null, true);

echo $browser['browser'];



?>

output (Screenshot7):-

usersbrowser
Screenshot7
Now as you can see the screenshot 5 we print the array which is associative array for the browser it had the key browser and the value is chorme there for in this code we echo out the key of the browser to only display the visitors data.

Now you can include condition statement and if else if statement to tell visitors use the google chrome or which ever website you want your visitors should view you website



No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates