.net及php判断手机还是电脑访问网站

 

asp.net(C#) 中用以下代码

using System.Text.RegularExpressions;//头部引入正则的命名空间

//为了加强准确性,防止支持wap的浏览器如opera,加入操作系统验证。openwave|后为pc操作系统

string osPat = "mozilla|m3gate|winwap|openwave|Windows NT|Windows 3.1|95|Blackcomb|98|ME|X Window|Longhorn|ubuntu|AIX|Linux|AmigaOS|BEOS|HP-UX|OpenBSD|FreeBSD|NetBSD|OS/2|OSF1|SUN";

string uAgent = Request.ServerVariables["HTTP_USER_AGENT"];

Regex reg = new Regex(osPat);

if (reg.IsMatch(uAgent))

{

Response.Write("电脑访问");

}

else

{

Response.Write("手机访问");

}

Response.Write("<br/>" + uAgent);


_________________________________________________________________________________

PHP中用以下代码:

$uAgent = $_SERVER['HTTP_USER_AGENT'];

$osPat  = "mozilla|m3gate|winwap|openwave|Windows NT|Windows 3.1|95|Blackcomb|98|ME|X Window|ubuntu|Longhorn|AIX|Linux|AmigaOS|BEOS|HP-UX|OpenBSD|FreeBSD|NetBSD|OS\/2|OSF1|SUN";

if(preg_match("/($osPat)/i",  $uAgent )) //winwap 模拟WAP手机上网的一个浏览器; openwave|后面为各pc操作系统

{

echo "电脑访问";

}

else

{

echo "手机访问";

}

echo '<br/>'.$uAgent;


_________________________________________________________________________________

javascript中采用navigator.userAgent 来获取,但非智能机似乎不支持js。

你可能感兴趣的:(.net及php判断手机还是电脑访问网站)