- PHP IE Detection
- Comments
- Something Useful
- strpos() must have returned non-false
- strpos() must have returned false
- strpos() must have returned non-false
- How to detect IE (Internet Explorer) 10 & 11 using PHP
- PHP IE Browser Detection code
- Use PHP to Detect Internet Explorer 11 and Below
- Related Posts
- Comments
- Leave a Reply Cancel reply
- Who is this “geek”?
- Recent Posts
- Archives
- Search Terms
- Проверка браузера на IE в php и js
- Обнаруживаем браузер Internet Explorer через JavaScript
- Обнаруживаем браузер Internet Explorer через PHP
PHP IE Detection
Psst! Create a DigitalOcean account and get $200 in free credit for cloud-based hosting and services.
Comments
Have a quick maybe stupid question. I need to detect all IE versions lower then 8. Can this just be done using the checks for if it’s ie 6 / 7 or is there an easier way
Browsers send their ID in a header file; I’m not sure if IE 8 sends anything unique from previous versions, but if you look at the header of it compared to other versions, you may find a useable text-string.
Make a script that echoes HTTP_USER_AGENT, find some folks who have earlier versions, have them send you the result, and see if any text string fits the bill.
Keep in mind, you may also want to differentiate between IEPC and IEMac.
I once used a switch statement with several browser options such as this snippet, and fed different CSS stylesheets to the output, depending on which browser was requesting a page. I didn’t parse between versions, however.
Note that the POSIX regex functions, such as eregi , are deprecated since PHP 5.3.0. Here’s a version using preg_match :
if (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']))
It doesn’t works for me, even the PHP5 version (from Geert). 🙁
I use this for detecting mobile browsers, and I wanted the same php file detects if its IE, and then redirects IE users to a directory on my site.
Could someone help me? I don’t know much PHP. :\
0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) < $mobile_browser++; >$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4)); $mobile_agents = array( 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac', 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno', 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-', 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-', 'newt','noki','oper','palm','pana','pant','phil','play','port','prox', 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar', 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-', 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp', 'wapr','webc','winw','winw','xda ','xda-'); if (in_array($mobile_ua,$mobile_agents)) < $mobile_browser++; >if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) < $mobile_browser++; >if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) < $mobile_browser = 0; >if ($mobile_browser > 0) < header("Location: http://tkpb.tk/m/"); >else < header("Location: http://tkpb.tk/home.html"); >?>
Something Useful
Let us do something more useful now. We are going to check what sort of browser the visitor is using. For that, we check the user agent string the browser sends as part of the HTTP request. This information is stored in a variable. Variables always start with a dollar-sign in PHP. The variable we are interested in right now is $_SERVER[‘HTTP_USER_AGENT’] .
Note:
$_SERVER is a special reserved PHP variable that contains all web server information. It is known as a superglobal. See the related manual page on superglobals for more information.
To display this variable, you can simply do:
Example #1 Printing a variable (Array element)
A sample output of this script may be:
There are many types of variables available in PHP. In the above example we printed an Array element. Arrays can be very useful.
$_SERVER is just one variable that PHP automatically makes available to you. A list can be seen in the Reserved Variables section of the manual or you can get a complete list of them by looking at the output of the phpinfo() function used in the example in the previous section.
You can put multiple PHP statements inside a PHP tag and create little blocks of code that do more than just a single echo. For example, if you want to check for Internet Explorer you can do this:
Example #2 Example using control structures and functions
if ( strpos ( $_SERVER [ ‘HTTP_USER_AGENT’ ], ‘MSIE’ ) !== FALSE ) echo ‘You are using Internet Explorer.
‘ ;
>
?>?php
A sample output of this script may be:
You are using Internet Explorer.
Here we introduce a couple of new concepts. We have an if statement. If you are familiar with the basic syntax used by the C language, this should look logical to you. Otherwise, you should probably pick up an introductory PHP book and read the first couple of chapters, or read the Language Reference part of the manual.
The second concept we introduced was the strpos() function call. strpos() is a function built into PHP which searches a string for another string. In this case we are looking for ‘MSIE’ (so-called needle) inside $_SERVER[‘HTTP_USER_AGENT’] (so-called haystack). If the needle is found inside the haystack, the function returns the position of the needle relative to the start of the haystack. Otherwise, it returns false . If it does not return false , the if expression evaluates to true and the code within its is executed. Otherwise, the code is not run. Feel free to create similar examples, with if, else, and other functions such as strtoupper() and strlen() . Each related manual page contains examples too. If you are unsure how to use functions, you will want to read both the manual page on how to read a function definition and the section about PHP functions.
We can take this a step further and show how you can jump in and out of PHP mode even in the middle of a PHP block:
Example #3 Mixing both HTML and PHP modes
if ( strpos ( $_SERVER [ ‘HTTP_USER_AGENT’ ], ‘MSIE’ ) !== FALSE ) ?>
strpos() must have returned non-false
You are using Internet Explorer
> else ?>
strpos() must have returned false
You are not using Internet Explorer
>
?>?php
A sample output of this script may be:
strpos() must have returned non-false
You are using Internet Explorer
Instead of using a PHP echo statement to output something, we jumped out of PHP mode and just sent straight HTML. The important and powerful point to note here is that the logical flow of the script remains intact. Only one of the HTML blocks will end up getting sent to the viewer depending on the result of strpos() . In other words, it depends on whether the string MSIE was found or not.
How to detect IE (Internet Explorer) 10 & 11 using PHP
If you need to detect Internet Explorer, specifically browser version 10 or 11 using PHP, then you can use the following PHP code to detect the Internet Explorer version. This script works with all IE versions, i.e., 8, 9, 10 and 11.
Using the conditional HTML comments can only detect IE 6/7/8/9 but they fail to detect IE 10 & 11. Because Microsoft has stopped sending «IE» or «MSIE» strings in the header information for IE 10 and IE 11 browser versions. So it is highly recommended to use a server side programming like PHP to detect the browser’s User Agent value from the HTTP header request. Continue reading below to see how you can use a simple PHP code to detect Internet Explorer version using PHP code.
PHP IE Browser Detection code
Using the below PHP IE detection code, you can find if a request is coming from Internet Explorer or any other browser. You can also add extra code to parse the IE version if need be.
For example, if you have a need of adding a custom CSS stylesheet rules for Internet Explorer versions, to have your webpage backward compatible, then using the example below you can add an extra class «ie» to your
tag. Later add some CSS rules to have different treatment for any child tag in the body.
if (isset($_SERVER['HTTP_USER_AGENT']) && ( (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false ) || (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false) ) )< // write you code here for IE // e.g. add classes to
or tag. i.e. // then write custom CSS rules. e.g. .ie h1 >
For more reading and advanced use of browser detection, you can learn more about an inbuilt PHP function get_browser(). Also please notice, the get_browser() function is considerably slow and may affect web server performance if running on PHP 5.
This tutorial is focused on IE detection in PHP. But if the scope of your requirements is wider and you wish to include all browsers detection, then get_browser() is recommended to use. Please make sure that you're running on PHP 7. The get_browser() function performs better on PHP 7.
Use PHP to Detect Internet Explorer 11 and Below
As I’ve mentioned before, Microsoft dropped conditional comment support in Internet Explorer 10 (and didn’t bring it back in IE 11). This means that you must use JavaScript or PHP to detect Internet Explorer 10 (IE10) or Internet Explorer 11 (IE11), which is a darn shame. It’s pretty easy to do, however. Especially if you are targeting ALL versions of Internet Explorer. Here’s what I currently do:
if (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false))It’s simple and it works well. The first part targets IE10 and below, while the second targets the newer IE11 user agent. Modify as needed.
Related Posts
Comments
I think the more germane issue, is to detect for IE10 and above. // IDENTIFY IE VERSIONS – returns ‘1’ if true
$isIE10 = (preg_match(‘/(?i)msie [10]/’,$_SERVER[‘HTTP_USER_AGENT’]));
$isIE11UP = (preg_match(‘/(?i)trident/7/’,$_SERVER[‘HTTP_USER_AGENT’]));
$isIE10UP = ($isIE10 == 1 || $isIE11UP == 1 ? 1 : 0);Leave a Reply Cancel reply
Who is this “geek”?
Using this blog, I hope to share information with others that may be useful to fellow web developers. My interests? All things related to the web and technology.
Recent Posts
Archives
Search Terms
- bootstrap dropdown hover
- CSS:HowtoEasilyHideContentOnOnlyMobileorOnlyDesktop|AGeekandHisBlog
- Bootstrap3:HowtoEnableHoverandToggleforNav|AGeekandHisBlog
- notepad lua
- bootstrap hide div on mobile
- error: The following untracked working tree files would be overwritten by checkout:
- error: The following untracked working tree files would be overwritten by merge:
- localStorage vs sessionStorage
- Bootstrap3:ChangeStackingOrder|AGeekandHisBlog
- List of 50 States Excel
Проверка браузера на IE в php и js
Обнаруживаем браузер Internet Explorer через JavaScript
/** * detect IE * returns version of IE or false, if browser is not Internet Explorer * функция возвращает версию IE, либо false(если браузер не Internet Explorer) * для её работы вызываем функцию и уже делаем то, что требуется */ function detectIE() let ua = window.navigator.userAgent; let msie = ua.indexOf('MSIE '); if (msie > 0) // IE 10 or older => return version number return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10); > let trident = ua.indexOf('Trident/'); if (trident > 0) // IE 11 => return version number let rv = ua.indexOf('rv:'); return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10); > let edge = ua.indexOf('Edge/'); if (edge > 0) // Edge (IE 12+) => return version number return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10); > // other browser return false; >
Обнаруживаем браузер Internet Explorer через PHP
// проверка на IE $ua = htmlentities($_SERVER['HTTP_USER_AGENT'], ENT_QUOTES, 'UTF-8'); if (preg_match('~MSIE|Internet Explorer~i', $ua) || (strpos($ua, 'Trident/7.0') !== false && strpos($ua, 'rv:11.0') !== false)) ?> div style="background-color: #b92a2a; color: #fff; padding: 20px; text-align: center;"> div style="color: #fff300; font-size: 28px; margin-bottom: 20px;">Вы зашли на сайт с помощью Internet Explorer!div> div>Часть информации может отображаться некорректноdiv> div>Рекомендуем использовать современный браузер: a style="color: #99cffd;text-decoration: underline;" href="https://www.google.com/chrome/?hl=ru">Google Chromea> или a style="color: #99cffd;text-decoration: underline;" href="http://www.mozilla.org/ru/firefox/new/">Mozilla Firefoxa>div> div> >?>
Приветствую вас на сайте ZENCOD.ru! Здесь вы найдете статьи по web-разработке, javascript, linux и прочим темам, которые могут быть полезны.