PDA

View Full Version : Browser version check (like yours)


Carl
03-23-2009, 04:15 PM
Hello,

I have noticed that you cannot use your website if you have a old browser version.
I think this is a great idea.

I wanted to implement this into my site but have a little banner show at the top of my page to alert the user that the website has not been fully tested and may not work correctly in a old browser, and ask them to upgrade to a modern browser.

I have a feeling that this is a javascript script that checks the browser. Yes?

Could you tell me how to do this.

I just want it to check to see if the user has any of the following browsers or later
Internet Explorer 7, Firefox 2.0, Safari 2.0 and Opera 9.0.

If anyone can let me know how to do this or point me in the right direction that would be great.

Thanks in advance

Carl

Ray Borduin
03-23-2009, 04:35 PM
You can analyze the server variable: HTTP_USER_AGENT

that will tell you what browser someone is using and what version... then you can specifically allow or reject based on that value.

Justin Nemeth
03-24-2009, 01:15 PM
For a client side method, look into using the window.navigator.userAgent string. More info can be found at https://developer.mozilla.org/en/DOM/window.navigator.userAgent.

Like Ray mentioned, in your case it might be cleaner to do this server side and use the $_SERVER['HTTP_USER_AGENT'] php variable.

Carl
03-25-2009, 04:55 PM
Thanks all for your help.

However, I have found a way to do this that will be more precise as to which browser is using and display to them what browser they are using.

Just incase anyone fancies using this script here is the code.

<!----------------------------------------------------------------------------------->
In a file called browsercheck.php in the folder script put this code

<?php
//--------------------------------------------------------
//
// This script checks to see if the user's browser is either IE5.5 or 6.0, Safari 2 or 1, Firefox 1.
// This will then put a yellow bar at the top of the users page to notify them that the website may not function correctly
// Use this code right after the body of the site
//
// if ($oldbrowser == 'yes') {
// (((div tags go here)))
// }
//
//--------------------------------------------------------

// This code finds the root of the server
$ServerRoot = $_SERVER["DOCUMENT_ROOT"];

// This code finds the user agent the user is uing
$browser = $_SERVER['HTTP_USER_AGENT'];

//Trying to find word IE5.5 if exists, then it is IE
if (preg_match("/MSIE 5.5/", $browser)) {
$browser = "Internet Explorer";
$version = "5.5";
$oldbrowser = "yes";
$browserversion = "Internet Explorer 5.5";
}
//Trying to find word IE6.0 if exists, then it is IE
if (preg_match("/MSIE 6.0/", $browser)) {
$browser = "Internet Explorer";
$version = "6.0";
$oldbrowser = "yes";
$browserversion = "Internet Explorer 6.0";
}
//Trying to find word 1.0 Safari if exists, then it is IE
if (preg_match("/Safari\/3/", $browser)) {
$browser = "Safari";
$version = "2";
$oldbrowser = "yes";
$browserversion = "Safari 2";
}
//Trying to find word Safari/419 if exists, then it is IE
if (preg_match("/Safari\/1/", $browser)) {
$browser = "Safari";
$version = "1";
$oldbrowser = "yes";
$browserversion = "Safari 1";
}
//Trying to find word Safari/419 if exists, then it is IE
if (preg_match("/Firefox\/1/", $browser)) {
$browser = "Firefox";
$version = "1";
$oldbrowser = "yes";
$browserversion = "Firefox 1";
}
?>


<?php
if ($oldbrowser == 'yes') {
include($ServerRoot."/script/browsercheck_text.php");
}
?>
<------------------------------------------------------------------------------------>


<!----------------------------------------------------------------------------------->
In a file called browsercheck_text.php in the folder script put this code (or any other text you want to use (the image file does not work, but it is just a warning symbol))

<style type="text/css">
#OldBrowser {
background-color: #FFFF66;
padding: 10px;
width: 90%;
border: 1px solid #CCCCCC;
color: #333333;
margin-right: auto;
margin-left: auto;
font-size: 1.3em;
}
#OldBrowser img {
float: left;
margin-right: 10px;
}
</style>
<div id="OldBrowser"><img src="<?php $ServerRoot ?>/images/symbols/warning35.jpg" width="41" height="34" />We have detected that your browser is <?php echo $browser ?> <?php echo $version ?>. This site has not been fully tested on your browser and some functions may not work correctly.<br />
To make the most of this site and benefit from the security and functionality of the new browsers, we recommend upgrading to the new <?php echo $browser ?> browser.</div>
<------------------------------------------------------------------------------------>


<!----------------------------------------------------------------------------------->
Then in the main file e.g. a template put this code
<?php
$ServerRoot = $_SERVER["DOCUMENT_ROOT"];
include($ServerRoot."/script/browsercheck.php");
?>
<------------------------------------------------------------------------------------>
Hope this script helps anyone

Carl