PHP code to determine whether the user is a PC or a mobile port

Source: Internet
Author: User
Tags object object php code


Let's take a look at how to use PHP to identify users through the PC, iPad or mobile phone to access the site?

<?php
$uAgent = $_server[' http_user_agent '];
$osPat = "mozilla|m3gate|winwap|openwave| Windows nt| Windows 3.1|95| blackcomb|98| me| xwindow|ubuntu| longhorn| Aix| Linux| amigaos| beos| Hp-ux| openbsd| freebsd| netbsd| os\/2| osf1| SUN ";
if (Preg_match ("/($osPat)/i", $uAgent))
{
echo "PC Access";
}
Else
{
echo "Other terminal access";
}

?>

The following is Tencent through the JS statement to determine the Web site access to the computer or mobile phone

The matching code of Tencent network
How to judge the type of machine that accesses a Web site-how to judge the ipad
JS to determine the browser client type (ipad,iphone,android)

<script type= "Text/javascript" >
<!--
platforms, devices, and operating systems
var system = {
Win:false,
Mac:false,
Xll:false,
Ipad: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);
System.ipad = (Navigator.userAgent.match (/ipad/i)!= null)? True:false;
Jump statements, if it is a mobile phone access will automatically jump to the wap.baidu.com page
if (System.win | | system.mac | | system.xll| | System.ipad) {

} else {

Window.location.href = "http://www.jdpatro.com/3g/";
}
-->
</script>

The matching code of Tencent network

<script type= "Text/javascript" >
if (/applewebkit.*mobile/i.test (navigator.useragent) | | (/midp| symbianos| nokia| Samsung| Lg| nec| tcl| alcatel| bird| dbtel| dopod| philips| haier| lenovo| mot-| nokia| sonyericsson| sie-| amoi| Zte/.test (navigator.useragent))) {
if (Window.location.href.indexOf ("? mobile") <0) {
try{
if (/android|webos|iphone|ipod| Blackberry/i.test (navigator.useragent)) {
Window.location.href= "http://shipei.qq.com/index.htm";
}else if (/ipad/i.test (navigator.useragent)) {
}else{
window.location.href= "http://shipei.qq.com/simple/s/index/"
}
}catch (e) {}
}
}
</script>

<script type= "Text/javascript" >
var browser = {
Versions:function () {
var u = navigator.useragent, app = Navigator.appversion;
return {//Mobile terminal browser version information
Trident:u.indexof (' Trident ') >-1,//ie kernel
Presto:u.indexof (' presto ') >-1,//opera kernel
Webkit:u.indexof (' AppleWebKit ') >-1,//Apple, Google kernel
Gecko:u.indexof (' Gecko ') >-1 && u.indexof (' khtml ') = = 1,//Firefox kernel
Mobile:!! U.match (/applewebkit.*mobile.*/)
|| !! U.match (/applewebkit/),//Whether it is a mobile terminal
Ios:!! U.match (/\ (i[^;] +;( U;)? Cpu.+mac OS x/),//ios terminal
Android:u.indexof (' Android ') >-1 | | U.indexof (' Linux ') >-1,//android terminal or UC Browser
Iphone:u.indexof (' IPhone ') >-1 | | U.indexof (' Mac ') >-1,//whether for iphone or Qqhd browser
Ipad:u.indexof (' ipad ') >-1,//whether the ipad
Webapp:u.indexof (' Safari ') = = -1,//Whether the web should be programmed without head and bottom
Google:u.indexof (' Chrome ') >-1
};
}(),
Language: (Navigator.browserlanguage | | navigator.language). toLowerCase ()
}
Document.writeln ("language version:" +browser.language);
Document.writeln ("Whether for Mobile Terminal:" +browser.versions.mobile);
</script>

How to judge the type of machine that accesses a Web site-how to judge the ipad

The key to deciding whether to be an ipad browser is to see if there is an ipad in its User Agent. The IPad uses the Safari Mobile browser, and his user Agent is:

mozilla/5.0 (IPad; U CPU os 3_2 like Mac os X; En-US) applewebkit/531.21.10 (khtml, like Gecko) version/4.0.4 mobile/7b334b safari/531.21.10

function Is_ipad () {
[Object Object] [Object Object] [Object Object]var ua = Navigator.userAgent.toLowerCase ();
[Object Object] [Object Object] [Object Object]if (Ua.match (/ipad/i) = "IPad") {
[Object Object] [Object Object] [Object Object] [Object Object] [Object Object] [Object Object]return true;
[Object Object] [Object Object] [Object Object]} else {
[Object Object] [Object Object] [Object Object] [Object Object] [Object Object] [Object Object]return false;
[Object Object] [Object Object] [Object Object]}
}


$is _ipad = (bool) strpos ($_server[' http_user_agent '], ' iPad ');

Rewritecond%{http_user_agent} ^.*ipad.*$
Rewriterule ^ (. *) $ http://ipad.fairyfish.net [r=301]

JS to determine the browser client type (ipad,iphone,android)

<script type= "Text/javascript" >
var bforcepc
= Fgetquery ("dv") = = "PC";
function
Fbrowserredirect () {
var suseragent = Navigator.userAgent.toLowerCase ();
var bisipad = Suseragent.match (/ipad/i) = =
"ipad";
var Bisiphoneos = Suseragent.match (/iphone os/i) = = "iphone OS";
var BISMIDP = Suseragent.match (/midp/i) = = "MIDP";
var bIsUc7 = Suseragent.match (/rv:1.2.3.4/i) = = "rv:1.2.3.4";
var bisuc = Suseragent.match (/ucweb/i) = = "UCWeb";
var bisandroid = Suseragent.match (/android/i) = = "Android";
var bisce = Suseragent.match (/windows ce/i) = = "Windows CE";
var biswm = Suseragent.match (/windows mobile/i) = = "Windows
Mobile ";
if (Bisipad) {
var sURL =
Location.href;
if (!BFORCEPC) {
Window.location.href = "http://ipad.mail.163.com/";
}
}
if (Bisiphoneos | | bisandroid) {
var sURL =
Location.href;
if (!BFORCEPC) {
Window.location.href = "http://smart.mail.163.com/";
}
}
if (bismidp| | bisuc7| | bisuc| | bisce| | BISWM) {
var sURL =
Location.href;
if (!BFORCEPC) {
Window.location.href = "http://m.mail.163.com/";
}
}
}
function
Fgetquery (name) {//Get parameter value
var surl = window.location.search.substr (1);
var r = Surl.match (new RegExp (^|&) + name +
"= ([^&]*) (&|$)"));
return (r = = null? NULL: (R[2));
}
function
Fshowverblock () {
if (BFORCEPC) {
Document.getelementbyidx_x ("Dv_block"). Style.display = "block";
}
else{
Document.getelementbyidx_x ("Ad_block"). Style.display = "block";
}
}
Fbrowserredirect ();
</script>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.