Site icon Nabtron

Detect users internet browser (firefox, safari, iphone, ipod, ipad, internet explorer, opera ) and redirect to different page specific to it

Some times we need to detect the browser of the user in order to redirect them to browser specific page, or to do any other task that is related to displaying that website for that particular user’s browser, e.g firefox, opera, internet explorer, safari, or are coming from an iphone or an ipod.

Edit: You can also use the code if your query includes detect ipad browser and detect user agent / browser

There are various ways to detect the browser that the visitor is using. Such as:

JavaScript

If you want to detect which browser the user is using, use the following code to get it done via javascript. Add this javascript code to detect the user browser in the head of the document to direct the user to particular page specific to that browser.

(Note: the code checks if the user is coming from and iphone/ ipod and redirects the user to a particular page specific to iphone/ipod browser, however you can modify the code to check for any other browser such as opera, safari, internet explorer etc.)

<script language=javascript>

<!--
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
{
location.replace("iphone-version.html");
}
-->
</script>

The above code when added/present in the head of the document / page will check through javascript that if the user is coming from an iphone/ipod internet browser, and if positive will load the specific page, and redirect the user to that page, for example mobile or iphone/ipod specific page. However the logic can be used for any other purpose too, such as showing , hiding, including a specific file, or part of page, or a css file specific to the current browser being used by the user.

php

If you want to do the same via php, you can use following code. The benefit of this is that the page does not need to load twice and also that it will not be apparent to the user, as the code will run on the server side.

So if you want to use php to detect the browser of the visitor and act accordingly, like if the user is coming from firefox, opera, internet explorer, safari or iphone/ipod browser, and after detecting them, you want to direct them to specific page, such as mobile page for your site etc, use the following code.

Please note, the example below uses the php code for detection of browsers to detect if the user is coming from the iphone/ipod interent browser , and redirects the user to iphone/ipod specific page. You can use the same logic for detection of other browsers and any other code implementation like loading certain file or part of code etc .

<?php

//setting the variables
$ipod = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iphone = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");

//detecting device
if ($ipod == true || $iphone == true){
header( 'Location: http://site-1/' ) ;
} else {
header( 'Location: http://site-2/' ) ;
}
?>

Conditional Comments

One more way to detect the user browser , if its safari, opera, firefox or internet explorer, and what is its version etc, is to use the conditional comments statements in the html of your page.

This works like this:

<!--[if expression]> HTML <![endif]-->

So if you want to check if the user is coming from specific browser such as internet explorer, then you can use this code:

<!--[if IE]>

some html, css code here

<![endif]-->

There is detail article on each of them seperately on the site. Check them in related articles or through search feature of the site.

We have separate tutorial if you’re looking for how to redirect user to iPhone / iPod version of site using PHP

Exit mobile version