• 一首歌可以循环一整天《我们都是好孩子》
  • 在别人眼里是逃避,其实是不想牵累任何人,做孤独的自己。
  • 也许有一天我会背着包,独自漂流。
  • 既然做了决定,就不后悔,再见昆明!
  • 愿闻世间百态。
  • 外表的一切如常,内心的混乱又有谁能了解!
  • 突然想回安徽工作了!
  • 该来的还是来了,Uzi宣布退役!
  • 国内疫情已完全控制住,但是国外疫情大爆发,现在开始限制人员入境。
  • 啊啊啊,IP被墙了,好烦!

PHP获取网站访问者信息

教程 愿闻世间百态 6年前 (2019-03-16 12:41:18) 3125次浏览 已收录 0个评论

PHP 获取网站访问者信息
PHP 获取网站访问者信息

分析信息并写入数据库
  1.  
  2. <?php
  3. //连接数据库
  4. $conn=mysqli_connect("localhost","weask_win","123456789lt","weask_win");
  5. if(!$conn){
  6. die("链接失败".mysqli_errno());
  7. }
  8. //设置数据库编码方式
  9. mysqli_query($conn,"set names utf8") or die(mysqli_errno());
  10. //选择数据库
  11. //mysql_select_db("weask_win",$conn) or die(mysql_errno());
  12. //$adress=$_SERVER["REMOTE_ADDR"];
  13. $time=date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']);
  14. $user_agent=$_SERVER['HTTP_USER_AGENT'];
  15. $user_os=Get_Os();
  16. $USER_IP=Get_Ip_Addr();
  17. $brower=Get_Browser1();
  18. $b_language=Get_Lang();
  19. $ip_addr=Get_Ip_From_126($USER_IP);
  20. //print_r($ip_addr);
  21. $addr=Get_Ip_country($ip_addr).Get_Ip_xcountry($ip_addr);
  22. //$t=mb_strlen($user_agent,'UTF8');
  23. //echo"$t";
  24. //echo"IP:"."$adress"." 时间:"."$time"." 操作系统:"."$user_os"." User_Agent:"."$user_agent"."$brower"."$b_language"."$addr";
  25. $sql="insert into wp_count(ip,time,user_os,brower,b_language,addr,user_agent) values('$USER_IP','$time','$user_os','$brower','$b_language','$addr','$user_agent')";
  26. mysqli_query($conn,$sql);
  27.  
  28. //将本次访客的 ip 地址添加到数据库中
  29. /*
  30. $sql="select times from index_count where ip='$adress'";
  31. $res=mysqli_query($conn,$sql);
  32. if(!$row=mysqli_fetch_row($res)){
  33. $sql="insert into index_count(ip, times) values('$adress','1')";
  34. }else{
  35. $times = $row['0']+1;
  36. $sql="update index_count set times='$times' where ip='$adress'";
  37. }
  38. $res=mysqli_query($conn,$sql);
  39. //发送语句获取总数
  40. $sql="select count(ip) from index_count";
  41. $res=mysqli_query($conn,$sql);
  42. if($row=mysqli_fetch_row($res)){
  43. $num=$row['0'];
  44. }
  45. */
  46. //echo"您是第 "."$num"." 位访客"."您的 ip 地址是"."$adress";
  47. mysqli_close($conn);
  48.  
  49. function Get_Ip_country($ip_addr){
  50. if(empty($ip_addr)){return $ip_addr;}
  51. preg_match('/var lo="(.*?)", lc/',$ip_addr,$ip_country);
  52. if(empty($ip_country[1])){return 'ERROR';}
  53. else{return $ip_country[1];}
  54. }
  55.  
  56. function Get_Ip_xcountry($ip_addr){
  57. if(empty($ip_addr)){return $ip_addr;}
  58. preg_match('/lc="(.*?)";/',$ip_addr,$ip_xcountry);
  59. if(empty($ip_xcountry[1])){return 'ERROR';}
  60. else{return $ip_xcountry[1];}
  61. }
  62.  
  63.  
  64. function Get_Browser1() {
  65. $user_OSagent = $_SERVER['HTTP_USER_AGENT'];
  66. if (strpos($user_OSagent, "Maxthon") && strpos($user_OSagent, "MSIE")) {
  67. $visitor_browser = "Maxthon(Microsoft IE)";
  68. } elseif (strpos($user_OSagent, "Maxthon 2.0")) {
  69. $visitor_browser = "Maxthon 2.0";
  70. } elseif (strpos($user_OSagent, "Maxthon")) {
  71. $visitor_browser = "Maxthon";
  72. } elseif (strpos($user_OSagent, "Edge")) {
  73. $visitor_browser = "Edge";
  74. } elseif (strpos($user_OSagent, "Trident")) {
  75. $visitor_browser = "IE";
  76. } elseif (strpos($user_OSagent, "MSIE")) {
  77. $visitor_browser = "IE";
  78. } elseif (strpos($user_OSagent, "MSIE")) {
  79. $visitor_browser = "MSIE 较高版本";
  80. } elseif (strpos($user_OSagent, "NetCaptor")) {
  81. $visitor_browser = "NetCaptor";
  82. } elseif (strpos($user_OSagent, "Netscape")) {
  83. $visitor_browser = "Netscape";
  84. } elseif (strpos($user_OSagent, "Chrome")) {
  85. $visitor_browser = "Chrome";
  86. } elseif (strpos($user_OSagent, "Lynx")) {
  87. $visitor_browser = "Lynx";
  88. } elseif (strpos($user_OSagent, "Opera")) {
  89. $visitor_browser = "Opera";
  90. } elseif (strpos($user_OSagent, "MicroMessenger")) {
  91. $visitor_browser = "微信浏览器";
  92. } elseif (strpos($user_OSagent, "Konqueror")) {
  93. $visitor_browser = "Konqueror";
  94. } elseif (strpos($user_OSagent, "Mozilla/5.0")) {
  95. $visitor_browser = "Mozilla";
  96. } elseif (strpos($user_OSagent, "Firefox")) {
  97. $visitor_browser = "Firefox";
  98. } elseif (strpos($user_OSagent, "CriOS")) {
  99. $visitor_browser = "CriOS";
  100. } elseif (strpos($user_OSagent, "Safari")) {
  101. $visitor_browser = "Safari";
  102. } else {
  103. $visitor_browser = "其它";
  104. }
  105. return $visitor_browser;
  106. }
  107. ////获得访客浏览器语言
  108. function Get_Lang(){
  109. if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
  110. $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  111. $lang = substr($lang,0,5);
  112. if(preg_match("/zh-cn/i",$lang)){
  113. $lang = "简体中文";
  114. }
  115. elseif(preg_match("/zh/i",$lang)){
  116. $lang = "繁体中文";
  117. }
  118. else{
  119. $lang = "English";
  120. }
  121. return $lang;
  122. }
  123. else{
  124. return "unknow";
  125. }
  126. }
  127. ////获得访客真实 ip
  128. function Get_Ip_Addr(){
  129. if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  130. $ip = $_SERVER["HTTP_CLIENT_IP"];
  131. }
  132. if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //获取代理 ip
  133. $ips = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
  134. }
  135. if($ip){
  136. $ips = array_unshift($ips,$ip);
  137. }
  138. $count = count($ips);
  139. for($i=0;$i<$count;$i++){
  140. if(!preg_match("/^(10|172\.16|192\.168)\./i",$ips[$i])){//排除局域网 ip
  141. $ip = $ips[$i];
  142. break;
  143. }
  144. }
  145. $tip = empty($_SERVER['REMOTE_ADDR']) ? $ip : $_SERVER['REMOTE_ADDR'];
  146. if($tip=="127.0.0.1"){ //获得本地真实 IP
  147. return self::get_onlineip();
  148. }
  149. else{
  150. return $tip;
  151. }
  152. }
  153. /*
  154. ////根据 ip 获得访客所在地地名--网络不友好,不使用。
  155. function Get_Ip_From_taobao($USER_IP){
  156. if(empty($USER_IP)){
  157. $USER_IP = self::Getip();
  158. }
  159. $ip_json=@file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$USER_IP");//根据 taobao ip
  160. $ip_json=json_decode($ip_json);
  161. if($ip_json)
  162. {
  163. return $ip_json;
  164. }
  165. else
  166. {
  167. return $ip_json;
  168. //return "匹配失败!";
  169. }
  170. }
  171. */
  172. //根据 ip 获取网络所在地址。
  173. function Get_Ip_From_126($USER_IP){
  174. if(empty($USER_IP)){return 'IP Error';}
  175. $ip_json=@file_get_contents("http://ip.ws.126.net/ipquery?ip=$USER_IP");
  176. $encode = mb_detect_encoding($ip_json, array("ASCII","UTF-8","GB2312","GBK","BIG5"));
  177. // echo $encode;
  178. $ip_json = iconv($encode,"UTF-8//IGNORE",$ip_json);
  179. if($ip_json){return $ip_json;}
  180. else{return $ip_json;}
  181. }
  182.  
  183.  
  184. function get_os(){
  185. $agent = $_SERVER['HTTP_USER_AGENT'];
  186. $os = false;
  187. if (preg_match('/win/i', $agent) && strpos($agent, '95'))
  188. {
  189. $os = 'Windows 95';
  190. }
  191. else if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90'))
  192. {
  193. $os = 'Windows ME';
  194. }
  195. else if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent))
  196. {
  197. $os = 'Windows 98';
  198. }
  199. else if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent))
  200. {
  201. $os = 'Windows Vista';
  202. }
  203. else if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent))
  204. {
  205. $os = 'Windows 7';
  206. }
  207. else if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent))
  208. {
  209. $os = 'Windows 8';
  210. }else if(preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent))
  211. {
  212. $os = 'Windows 10';#添加 win10 判断
  213. }else if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent))
  214. {
  215. $os = 'Windows XP';
  216. }
  217. else if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent))
  218. {
  219. $os = 'Windows 2000';
  220. }
  221. else if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent))
  222. {
  223. $os = 'Windows NT';
  224. }
  225. else if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent))
  226. {
  227. $os = 'Windows 32';
  228. }
  229. else if (preg_match('/ipad/i', $agent))
  230. {
  231. $os = 'iPad';
  232. }
  233. else if (preg_match('/macintosh/i', $agent))
  234. {
  235. $os = 'Mac';
  236. }
  237. else if (preg_match('/iphone/i', $agent))
  238. {
  239. $os = 'iPhone';
  240. }
  241. else if (preg_match('/android/i', $agent))
  242. {
  243. $os = 'Android';
  244. }
  245. else if (preg_match('/linux/i', $agent))
  246. {
  247. $os = 'Linux';
  248. }
  249. else if (preg_match('/unix/i', $agent))
  250. {
  251. $os = 'Unix';
  252. }
  253. else if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent))
  254. {
  255. $os = 'SunOS';
  256. }
  257. else if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent))
  258. {
  259. $os = 'IBM OS/2';
  260. }
  261. else if (preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent))
  262. {
  263. $os = 'Macintosh';
  264. }
  265. else if (preg_match('/PowerPC/i', $agent))
  266. {
  267. $os = 'PowerPC';
  268. }
  269. else if (preg_match('/AIX/i', $agent))
  270. {
  271. $os = 'AIX';
  272. }
  273. else if (preg_match('/HPUX/i', $agent))
  274. {
  275. $os = 'HPUX';
  276. }
  277. else if (preg_match('/NetBSD/i', $agent))
  278. {
  279. $os = 'NetBSD';
  280. }
  281. else if (preg_match('/BSD/i', $agent))
  282. {
  283. $os = 'BSD';
  284. }
  285. else if (preg_match('/OSF1/i', $agent))
  286. {
  287. $os = 'OSF1';
  288. }
  289. else if (preg_match('/IRIX/i', $agent))
  290. {
  291. $os = 'IRIX';
  292. }
  293. else if (preg_match('/FreeBSD/i', $agent))
  294. {
  295. $os = 'FreeBSD';
  296. }
  297. else if (preg_match('/teleport/i', $agent))
  298. {
  299. $os = 'teleport';
  300. }
  301. else if (preg_match('/flashget/i', $agent))
  302. {
  303. $os = 'flashget';
  304. }
  305. else if (preg_match('/webzip/i', $agent))
  306. {
  307. $os = 'webzip';
  308. }
  309. else if (preg_match('/offline/i', $agent))
  310. {
  311. $os = 'offline';
  312. }
  313. else
  314. {
  315. $os = '未知操作系统';
  316. }
  317. return $os;
  318. }
  319. ?>
  320. ?>
  321.  
读取数据库并界面显示
  1.  
  2. <?php
  3. header("Content-Type: text/html; charset=utf-8");//防止界面乱码
  4. $con=mysqli_connect('localhost','weask_win','123456789lt','weask_win');//数据库用户名,密码
  5. if(!$con){
  6. die('connect failed!');
  7. }
  8. mysqli_query($con,"SET NAMES utf8");//解决数据库中有汉字时显示在前台出现乱码问题
  9. $result = mysqli_query($con,"SELECT * FROM wp_count order by id desc");
  10. //print_r(mysql_fetch_array($result));//取得第一条数据
  11.  
  12. echo ('
  13. <html xmlns="http://www.w3.org/1999/xhtml">
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  16. <title>访问记录日志</title>
  17. <meta name="keywords" content="访问记录日志"/>
  18. <meta name="description" content="访问记录日志"/>
  19. <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
  20. <link rel="stylesheet" type="text/css" href="../css/common.css" />
  21. </head>
  22. <body>
  23. <div class="container">
  24. <div class="demo">
  25. <h2 class="title">
  26. <center>访问记录日志</center>
  27. </h2>
  28. <div style="width:800px;margin:auto">
  29. <table class="table table-hover">
  30. <thead>
  31. <tr>
  32. <th>ID</th>
  33. <th>IP 地址</th>
  34. <th>IP 地区</th>
  35. <th>OS 操作系统</th>
  36. <th>浏览器</th>
  37. <th>语言</th>
  38. <th>Time 访问时间</th>
  39. </tr>
  40. </thead>
  41. <tbody>
  42. ');
  43.  
  44. while($row = mysqli_fetch_array($result))//转成数组,且返回第一条数据,当不是一个对象时候退出
  45. {
  46. echo "<tr>";
  47. echo "<td>" . $row['id'] . "</td>";
  48. echo "<td>" . $row['ip'] . "</td>";
  49. echo "<td>" . $row['addr'] . "</td>";
  50. echo "<td>" . $row['user_os'] . "</td>";
  51. echo "<td>" . $row['brower'] . "</td>";
  52. echo "<td>" . $row['b_language'] . "</td>";
  53. echo "<td>" . $row['time'] . "</td>";
  54. echo "</tr>";
  55. }
  56. echo "</tbody></table></div></div></div></body></html>";
  57.  
  58. mysqli_close($con);
  59. ?>
  60.  

WECV.CN , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP 获取网站访问者信息
喜欢 (0)
[]
分享 (0)

您必须 登录 才能发表评论!