How to detect old browsers in PHP and show a obsolete browser notice
put this in a file called common.
Class App_Util {
function is_old_browser() {
$old = 0;
if (!empty($_SERVER['HTTP_USER_AGENT'])) {
// IE <= 7
// User Agent: Opera/9.80 (Windows NT 6.1; U; en) Presto/2.10.229 Version/11.61
if (preg_match('#msie\s+(\d+)\.(\d+)#si', $_SERVER['HTTP_USER_AGENT'], $matches)) {
if ($matches[1] <= 7) {
$old = 1;
}
}
// Firefox <= 7
// User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
elseif (preg_match('#Firefox/(\d+)\.(\d+)[\.\d]+#si', $_SERVER['HTTP_USER_AGENT'], $matches)) {
if ($matches[1] <= 7) {
$old = 1;
}
}
// Safari < 5
// User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.52.7 (KHTML, like Gecko) Version/5.1.2 Safari/534.52.7
elseif (preg_match('#Version/(\d+)[\.\d]+ Safari/[\.\d]+#si', $_SERVER['HTTP_USER_AGENT'], $matches)) {
if ($matches[1] < 5) {
$old = 1;
}
}
// opera < 11
// User Agent: Opera/9.80 (Windows NT 6.1; U; en) Presto/2.10.229 Version/11.61
elseif (preg_match('#Opera/[\.\d]+.*?Version/(\d+)[\.\d]+#si', $_SERVER['HTTP_USER_AGENT'], $matches)) {
if ($matches[1] < 11) {
$old = 1;
}
}
}
return $old;
}
}
How to perform the check
file: block_old_browser.php
<?php if (App_Util::is_old_browser()) : ?>
<div class="app_old_browser_notice">
<h2>Old Browser</h2>
<p>Please consider upgrading your browser. Doing so will improve experience with the site. <br/>
Please click one of them below.</p>
<ul>
<li><a href="http://www.mozilla.org/en-US/firefox/fx/" target="_blank">Mozilla Firefox</a></li>
<li><a href="https://www.google.com/chrome" target="_blank">Google Chrome</a></li>
<li><a href="http://www.apple.com/safari/" target="_blank">Safari</a></li>
<li><a href="http://www.opera.com/download/" target="_blank">Opera</a></li>
<li><a href="http://www.microsoft.com/windows/ie/" target="_blank">Internet Explorer (Windows)</a></li>
</ul>
</div>
<?php endif; ?>
Include the file above in all the files that you need to the noticed displayed.
<?php include('block_old_browser.php'); ?>
css styles
<style>
.app_old_browser_notice {
background-color:#ff0000;
color: white;
margin:20px;
padding:10px;
}
.app_old_browser_notice a {
color: white;
}
</style>
4 comments:
I have no words to express how useful your blog was to me in completing my job work successful. Thanks a lot.
I am a fan of your blog and articles. This text is basically interesting. Thanks for sharing this information with us.
This information is very helpful and I can use this formula to detect old browser in PHP. Thanks.
The way you shared your knowledge here is amazing!
Post a Comment