站內搜尋

Friday, April 13, 2012

[PHP] 透過 $_SERVER['HTTP_USER_AGENT'] 來判斷使用者是否使用IE

資料來源:PHP官網


<?php
function using_ie(){
    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    $ub = False;
    if(preg_match('/MSIE/i',$u_agent))
    {
        $ub = True;
    }
    return $ub;
}

function ie_box() {
    if (using_ie()) {
        ?>
        <div class="iebox">
            This page is not designed for Intenet Explorer.  If you want to see this webpage as intended, please use a standards compliant browser, such as <a href="http://www.google.com/chrome">Google Chrome</a>.
        </div>
        <?php
    return;
    }
}
?>

2 comments:

  1. $b = $_SERVER["HTTP_USER_AGENT"];
    $b_v = explode("(",$b);

    if( preg_match('/Windows NT 5.1/', $b)){
    $os = "Windows XP";
    }
    elseif( preg_match('/Windows NT 6.0/', $b)){
    $os = "Windows Vista";

    }
    elseif( preg_match('/Windows NT 6.1/', $b)){
    $os = "Windows 7/2008 R1";

    }
    elseif( preg_match('/Windows NT 5.0/', $b)){
    $os = "Windows 2000";

    }
    elseif( preg_match('/Mac OS X/', $b)){
    $os = "Apple Macintosh";

    }
    elseif( preg_match('/Linux/', $b) && preg_match('/Android/', $b)){
    $os = "Android";

    }
    elseif( preg_match('/Linux/', $b)){
    $os = "Linux";

    }
    else{
    $os = "Unknow";
    }
    //echo $os ." - ";

    if( preg_match('/MSIE/', $b)){
    $browser = "IE";
    }
    elseif( preg_match('/Safari/', $b) && !preg_match('/Chrome/', $b)){
    $browser = "Safari";
    }
    elseif( preg_match('/Safari/', $b) && preg_match('/Chrome/', $b)){
    $browser = "Chrome";
    }elseif( preg_match('/Opera/', $b)){
    $browser = "Opera";
    }
    elseif( preg_match('/Firefox/', $b)){
    $browser = "Firefox";
    }
    else{
    $browser = "Unknow";
    }

    ReplyDelete