php or JS judge site visitors from mobile phone or PC
Choose different website versions for different platforms, and the final summary is as follows:
One, JS version of the code:
<!--switch Mobile website--->
<script src= "Http://siteapp.baidu.com/static/webappservice/uaredirect.js" type= "Text/javascript" ></ Script>
<script type= "Text/javascript" >uaredirect ("http://www.baidu.com/");</script>
//js Method Two
<script type= "Text/javascript" >
<!--//platforms, devices and operating systems
var system ={
Win:false,
Mac:false,
Xll:false
};
Testing platform
var p = navigator.platform;
System.win = P.indexof ("win") = = 0;
System.mac = P.indexof ("mac") = = 0;
system.x11 = (P = = "X11") | | (P.indexof ("Linux") = = 0);
Jump statement, if it is mobile Access will automatically jump to the wap.baidu.com page
if (system.win| | system.mac| | SYSTEM.XLL) {
Window.location.href= "http://www.php186.com";
}else{
Window.location.href= "http://wap.php186.com";
}
-
</script>
Second, PHP code version:
<?php
//Judge mobile phone or computer Access website method one:
function IsMobile () {
if (Isset ($_server[' http_x_wap_profile '))
{
echo "Mobile device";
}else{
echo "PC";
}
}
//Judge mobile phone or computer Access website method two:
if (Isset ($_server[' Http_via '))
{
Not found for flase, otherwise true
if (Stristr ($_server[' Http_via '), "WAP"))
{
echo "Mobile device";
}else{
echo "PC";
}
}
//Judge mobile phone or computer Access website method Three:
if (Isset ($_server[' http_user_agent '))
{
$clientkeywords = Array (
' Nokia ',
' Sony ',
' Ericsson ',
' MOT ',
' Samsung ',
' HTC ',
' SGH ',
' LG ',
' Sharp ',
' Sie-',
' Philips ',
' Panasonic ',
' Alcatel ',
' Lenovo ',
' iphone ',
' ipod ',
' BlackBerry ',
' Meizu ',
' Android ',
' NetFront ',
' Symbian ',
' UCWeb ',
' WindowsCE ',
' Palm ',
' Operamini ',
' Operamobi ',
' Openwave ',
' Nexusone ',
' CLDC ',
' MIDP ',
' WAP ',
' Mobile '
);
Find keywords for your phone's browser from Http_user_agent
if (Preg_match ("/("). Implode (' | ', $clientkeywords). ")/I", Strtolower ($_server[' http_user_agent ')))
{
echo "Mobile device";
}else{
echo "PC";
}
}
?>
//Method four: Integrated above
<?php
$mobile _browser = ' 0 ';
if (Preg_match ('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/I ', Strtolower ($_SERVER[' HTTP_USER_ AGENT '])) {
$mobile _browser++;
}
if ((Strpos (Strtolower ($_server[' http_accept '), ' application/vnd.wap.xhtml+xml ') >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 (
' The ' 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-', ' googlebot-mobile ');
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:mobile.php"); Hand Machine version
}else {
Header ("Location:pc.php"); Computer edition
}
My Final Solution selection method four, combined with the previous three kinds:
Home page for PC version: http://www.zhuqiaodou.com
index.php Code:
<?php
/** determine if the mobile browser
* @return Boolean
*/
function is_moble () {
$mobile _browser = ' 0 ';
if (Preg_match ('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/I ', Strtolower ($_SERVER[' HTTP_USER_ AGENT '])) {
$mobile _browser++;
}
if ((Strpos (Strtolower ($_server[' http_accept '), ' application/vnd.wap.xhtml+xml ') >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 (
' The ' 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-', ' googlebot-mobile ');
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) {
return true;
Header ("Location:mobile.php"); Hand Machine version
}else {
return false;
Header ("Location:pc.php"); Computer edition
}
}
if (is_moble ()) {
Header ("location:http://www.zhuqiaodou.com"); Hand Machine version
Exit ();
}
?>
<!doctype html>
<meta charset= "Utf-8" >
<title> Pig Clever Bean </title>
<body>
PC Version homepage ....
</body>
PHP or JS Judge site visitors from mobile phone or PC