当前位置:首页 > 代码·功能 > 正文

怎样用php获取用户浏览器、操作系统、本地天气

给大家发个获取访客 ip 浏览器 操作系统 天气情况的php代码

ip的没有直接加入,想要显示IP可以自己在引用即可~\(≧▽≦)/~啦

天气等都是用的新浪的接口

如果服务器足够强大,其实只要不是太菜的 直接放php文件即可

但是如果想给服务器减减压,就放javascript吧,直接用脚本 下面放的是php的文件,可以自己调试一下

文件的编码是UTF-8不要搞错哦~~

<pre><?php
 
header("Content-type: text/html; charset=utf-8");
 
$useragent=$useragent=$_SERVER['HTTP_USER_AGENT'];
 
/*取得客户端浏览器版本*/
function browser($ua) {
if (stripos($ua, "Googlebot")) {
$browser = "谷歌蜘蛛";
}
elseif(stripos($ua, "Baiduspider") !== false) {
$browser = "百度蜘蛛";
}
elseif(stripos($ua, "Yahoo!") !== false) {
$browser = "雅虎蜘蛛";
}
elseif(stripos($ua, "bingbot")) {
$browser = "必应蜘蛛";
}
elseif(stripos($ua, "YRSpider")) {
$browser = "云壤蜘蛛";
}
elseif(stripos($ua, "Yeti") !== false) {
$browser = "Naver蜘蛛";
}
elseif(stripos($ua, "Maxthon")) {
if (stripos($ua, "AppleWebKit")) {
$browser = "遨游浏览器(极速模式)";
}
elseif(stripos($ua, "Trident")) {
$browser = "遨游浏览器(兼容模式)";
}
elseif(stripos($ua, "MAXTHON 2.0")) {
$browser = "遨游浏览器2.0";
}
}
elseif(stripos($ua, "Firefox")) {
$browser = "火狐浏览器";
}
elseif(stripos($ua, "Opera") == 0 && stripos($ua, "Presto")) {
$browser = "Opera";
}
elseif(stripos($ua, "BIDUBrowser")) {
if (stripos($ua, "Trident")) {
$browser = "百度浏览器(兼容模式)";
}
elseif(stripos($ua, "AppleWebKit")) {
$browser = "百度浏览器(极速模式)";
}
}
elseif(stripos($ua, "Ruibin")) {
$browser = "瑞影浏览器";
}
elseif(stripos($ua, "qihu theworld")) {
if (stripos($ua, "Trident")) {
$browser = "世界之窗浏览器";
}
elseif(stripos($ua, "AppleWebKit")) {
$browser = "世界之窗浏览器(极速模式)";
}
}
elseif(stripos($ua, "MetaSr")) {
if (stripos($ua, "Trident")) {
$browser = "搜狗高速浏览器(兼容模式)";
}
elseif(stripos($ua, "AppleWebKit")) {
$browser = "搜狗高速浏览器(极速模式)";
}
}
elseif(stripos($ua, "LBBROWSER")) {
if (stripos($ua, "Trident")) {
$browser = "猎豹浏览器(兼容模式)";
}
elseif(stripos($ua, "AppleWebKit")) {
$browser = "猎豹浏览器(极速模式)";
}
}
elseif(stripos($ua, "YLMFBR")) {
$browser = "115浏览器";
}
elseif(stripos($ua, "QQBrowser")) {
if (stripos($ua, "Trident")) {
$browser = "QQ浏览器(兼容模式)";
}
elseif(stripos($ua, "AppleWebKit")) {
$browser = "QQ浏览器(极速模式)";
}
}
elseif(stripos($ua, "TencentTraveler")) {
$browser = "腾讯TT浏览器";
}
elseif(stripos($ua, "TaoBrowser")) {
if (stripos($ua, "Trident")) {
$browser = "淘宝浏览器(兼容模式)";
}
elseif(stripos($ua, "AppleWebkit")) {
$browser = "淘宝浏览器(极速模式)";
}
}
elseif(stripos($ua, "CoolNovo")) {
$browser = "枫树浏览器";
}
elseif(stripos($ua, "SaaYaa")) {
$browser = "闪游浏览器";
}
elseif(stripos($ua, "360SE")) {
$browser = "360安全浏览器";
}
elseif(stripos($ua, "360EE")) {
if (stripos($ua, "Trident")) {
$browser = "360极速浏览器(兼容模式)";
}
elseif(stripos($ua, "AppleWebkit")) {
$browser = "360极速浏览器(极速模式)";
}
}
elseif(stripos($ua, "Konqueror")) {
$browser = "Konqueror";
}
elseif(stripos($ua, "Chrome")) {
$browser = "谷歌浏览器";
}
elseif(stripos($ua, "Safari")) {
$browser = "Safari";
}
elseif(stripos($ua, "MSIE")) {
$ver = explode(";", substr($ua, stripos($ua, "MSIE") + 5, 4));
$ver = $ver[0];
$browser = "IE ".$ver;
}
elseif(stripos($ua, "UCWEB")) {
$browser = "UCWEB浏览器";
}
elseif(stripos($ua, "WAP")) {
$browser = "Mobile浏览器";
} else {
$browser = $ua;
}
if ($browser == '') $browser = $ua;
return $browser;
}
/*取得操作系统版本*/
function os($ua) {
$os = "";
if (stripos($ua, "Googlebot")) {
$os = "谷歌蜘蛛";
}
elseif(stripos($ua, "Baiduspider") !== false) {
$os = "百度蜘蛛";
}
elseif(stripos($ua, "Yahoo!") !== false) {
$os = "雅虎蜘蛛";
}
elseif(stripos($ua, "bingbot")) {
$os = "必应蜘蛛";
}
elseif(stripos($ua, "YRSpider")) {
$os = "云壤蜘蛛";
}
elseif(stripos($ua, "Yeti") !== false) {
$os = "Naver蜘蛛";
}
elseif(stripos($ua, "Windows NT")) {
switch (substr($ua, stripos($ua, "Windows NT") + 11, 3)) {
case 5.0:
$os = "Windows 2000";
break;
 
case 5.1:
$os = "Windows XP";
break;
 
case 5.2:
$os = "Windows 2003";
break;
 
case 6.0:
$os = "Windows Vista/2008";
break;
 
case 6.1:
$os = "Windows 7";
break;
 
case 6.2:
$os = "Windows 8";
break;
 
default:
$os = "Windows";
break;
 
}
if (stripos($ua, "WOW64")) {
$os.= "(X64)";
} else {
$os.= "(X86)";
}
}
elseif(stripos($ua, "Android")) {
$os = substr($ua, stripos($ua, "Android"), 11);
}
elseif(stripos($ua, "Linux")) {
if (stripos($ua, "i686")) {
$os = "Linux X86";
} else {
$os = "Linux";
}
if (stripos($ua, "X11")) {
$os.= "(X Window)";
}
}
elseif(stripos($ua, "Macintosh")) {
$os = "Mac";
}
elseif(stripos($ua, "IOS")) {
$os = "iOS";
}
elseif(stripos($ua, "ZTE")) {
$os = "ZTE";
}
elseif(stripos($ua, "Windows 98")) {
$os = "Windows 98";
} else {
$os = "未知系统";
}
return $os;
}
/*取出客户端IP地址及所在地区*/
function address($ip) {
$info = json_decode(file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip='.$ip.'&format=json'), false);
if ($info ->ret == 1) {
if ($info ->province != $info ->city) {
return $info ->country.",".$info ->province."(".$info ->city.") ".$info ->district." ".$info ->desc;
} else {
return $info ->country.",".$info ->province." ".$info ->district." ".$info ->desc;
}
} else {
return '地球';
}
}
 
// 复杂获取本地ip地址
/* if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$SA_IP = getenv('HTTP_CLIENT_IP');
} elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$SA_IP = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$SA_IP = getenv('REMOTE_ADDR');
} elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$SA_IP = $_SERVER['REMOTE_ADDR'];
} */
$SA_IP=$_SERVER['REMOTE_ADDR'];//简单获取本地ip地址
//定义一个函数根据ip获取城市名,使用新浪的天气预报
function getIPLoc_sina($queryIP){
$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=' . $queryIP;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING, 'utf8');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$location = curl_exec($ch);
$location = json_decode($location);
curl_close($ch);
$loc = "";
if ($location === FALSE)
return "";
if (empty($location->desc)) {
$loc = $location->city;
$full_loc = $location->province . $location->city . $location->district . $location->isp;
} else {
$loc = $location->desc;
}
return $loc;
}
$city = getIPLoc_sina("$SA_IP");
 
$citycode = mb_convert_encoding($city, "gb2312", "utf-8");
 
$doc = new DOMDocument();
if (!@$doc->load("http://php.weather.sina.com.cn/xml.php?city=" . $citycode . "&password=DJOYnieT8234jlsK&day=0")) {
echo "Get data failed!!\n";
return;
}
 
$city = $doc->getElementsByTagName("city")->item(0)->nodeValue;
$stat1 = $doc->getElementsByTagName("status1")->item(0)->nodeValue;
$chy_shuoming = $doc->getElementsByTagName("chy_shuoming")->item(0)->nodeValue;
$tmp1 = $doc->getElementsByTagName("temperature1")->item(0)->nodeValue;
$tmp2 = $doc->getElementsByTagName("temperature2")->item(0)->nodeValue;
$date = $doc->getElementsByTagName("savedate_weather")->item(0)->nodeValue;
$pollution_l = $doc->getElementsByTagName("pollution_l")->item(0)->nodeValue;
$gm_s = $doc->getElementsByTagName("gm_s")->item(0)->nodeValue;
 
echo "我猜你用的浏览器是:".browser($useragent);
echo "<br>我猜你的电脑操作系统是:".os($useragent);
echo"<br>我猜你来自:".address($_SERVER['REMOTE_ADDR']);
echo "<br>我猜今天是:" .$date . "<br>我猜今天的天气是:". $tmp1 ."~". $tmp2 ."℃" .$stat1 ."<br>我猜空气质量:".$pollution_l ."<br>我建议你穿这些:". $chy_shuoming ;
echo "<br>以上信息仅供参考!!请勿当真!!如有雷同,纯属巧合!!"
?></pre>

 

标签:
上一篇: 下一篇:

2 条评论

评论加载中...
  1. 沙发
    这家伙可能用了岛国的代理 未知浏览器 Unknow Os
    极国  

    都不会用

    2015年2月13日 20:23 评论

发表评论

不理你。不要啊!吃饭。吃惊。吃西瓜。飞吻!恭喜!Hi纠结!膜拜!OK抛媚眼。泡泡糖。抛钱。忍!生闷气!调皮。偷看。委屈。献花。疑问?抓狂!

小提示:Ctrl+Enter快速提交助您一臂之力~
加载中……