PHP에서 다음과 같은 다양한 버전의 Internet Explorer에 대해 조건을 붙이고 싶습니다.
if($browser == ie6){ //do this} elseif($browser == ie7) { //dothis } elseif...
비슷한 코드에 대해 많은 변형을 봐왔지만 간단한 if나 or other, 그리고 다른 일을 할 수 있는 매우 간단한 코딩을 찾고 있습니다.
고마워요.
편집: CSS 조건 등은 좋지 않기 때문에 사용자에게 다른 메시지를 표시하기 위해 필요합니다.
질문에 대한 답변
이것은 IE8 이하를 체크하는 바리에이션입니다.
if (preg_match('/MSIEs(?P<v>d+)/i', @$_SERVER['HTTP_USER_AGENT'], $B) && $B['v'] <= 8) {
// Browsers IE 8 and below } else {
// All other browsers }
IE10과 IE11 양쪽에서 계속 동작하는 버전:
preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $matches); if(count($matches)<2){
preg_match('/Trident/d{1,2}.d{1,2}; rv:([0-9]*)/', $_SERVER['HTTP_USER_AGENT'], $matches); }
if (count($matches)>1){
//Then we're using IE
$version = $matches[1];
switch(true){
case ($version<=8):
//IE 8 or under!
break;
case ($version==9
$version==10):
//IE9 & IE10!
break;
case ($version==11):
//Version 11!
break;
default:
//You get the idea
} }
HTTP_USER_AGENT 서버 변수를 확인할 수 있습니다.IE에 의해 전송된 사용자 에이전트에 MSIE가 포함되어 있습니다.
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) { ... }
특정 버전에 대해 조건을 확장할 수 있습니다.
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== false) { ... }
다음은 php에서 브라우저를 검출하기 위한 유용한 리소스입니다.http://php.net/manual/en/function.get-browser.php
다음으로 가장 간단한 예를 제시하겠습니다.
<?php function get_user_browser() {
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$ub = '';
if(preg_match('/MSIE/i',$u_agent))
{
$ub = "ie";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
$ub = "firefox";
}
elseif(preg_match('/Safari/i',$u_agent))
{
$ub = "safari";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$ub = "chrome";
}
elseif(preg_match('/Flock/i',$u_agent))
{
$ub = "flock";
}
elseif(preg_match('/Opera/i',$u_agent))
{
$ub = "opera";
}
return $ub; } ?>
나중에 코드 안에서 이렇게 말할 수 있을 거야
$browser = get_user_browser(); if($browser == "ie"){
//do stuff }
나는 이것을 한다.
$u = $_SERVER['HTTP_USER_AGENT'];
$isIE7
= (bool)preg_match('/msie 7./i', $u ); $isIE8
= (bool)preg_match('/msie 8./i', $u ); $isIE9
= (bool)preg_match('/msie 9./i', $u ); $isIE10 = (bool)preg_match('/msie 10./i', $u );
if ($isIE9) {
//do ie9 stuff }