前言:最近想记录主页和博客主页来访者的信息,于是开始在网上学习 php 基本语法,并借鉴其他人的代码。
主要功能:1、记录来访者的请求时间($_SERVER[‘REQUEST_TIME’])。
2、分析$_SERVER[‘HTTP_USER_AGENT’]请求头信息。
3、将信息分类写入数据库。
4、单独页面读取数据库,并显示出来。
分析信息并写入数据库
<?php
//连接数据库
$conn=mysqli_connect("localhost","weask_win","123456789lt","weask_win");
if(!$conn){
die("链接失败".mysqli_errno());
}
//设置数据库编码方式
mysqli_query($conn,"set names utf8") or die(mysqli_errno());
//选择数据库
//mysql_select_db("weask_win",$conn) or die(mysql_errno());
//$adress=$_SERVER["REMOTE_ADDR"];
$time=date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']);
$user_agent=$_SERVER['HTTP_USER_AGENT'];
$user_os=Get_Os();
$USER_IP=Get_Ip_Addr();
$brower=Get_Browser1();
$b_language=Get_Lang();
$ip_addr=Get_Ip_From_126($USER_IP);
//print_r($ip_addr);
$addr=Get_Ip_country($ip_addr).Get_Ip_xcountry($ip_addr);
//$t=mb_strlen($user_agent,'UTF8');
//echo"$t";
//echo"IP:"."$adress"." 时间:"."$time"." 操作系统:"."$user_os"." User_Agent:"."$user_agent"."$brower"."$b_language"."$addr";
$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')";
mysqli_query($conn,$sql);
//将本次访客的 ip 地址添加到数据库中
/*
$sql="select times from index_count where ip='$adress'";
$res=mysqli_query($conn,$sql);
if(!$row=mysqli_fetch_row($res)){
$sql="insert into index_count(ip, times) values('$adress','1')";
}else{
$times = $row['0']+1;
$sql="update index_count set times='$times' where ip='$adress'";
}
$res=mysqli_query($conn,$sql);
//发送语句获取总数
$sql="select count(ip) from index_count";
$res=mysqli_query($conn,$sql);
if($row=mysqli_fetch_row($res)){
$num=$row['0'];
}
*/
//echo"您是第 "."$num"." 位访客"."您的 ip 地址是"."$adress";
mysqli_close($conn);
function Get_Ip_country($ip_addr){
if(empty($ip_addr)){return $ip_addr;}
preg_match('/var lo="(.*?)", lc/',$ip_addr,$ip_country);
if(empty($ip_country[1])){return 'ERROR';}
else{return $ip_country[1];}
}
function Get_Ip_xcountry($ip_addr){
if(empty($ip_addr)){return $ip_addr;}
preg_match('/lc="(.*?)";/',$ip_addr,$ip_xcountry);
if(empty($ip_xcountry[1])){return 'ERROR';}
else{return $ip_xcountry[1];}
}
function Get_Browser1() {
$user_OSagent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_OSagent, "Maxthon") && strpos($user_OSagent, "MSIE")) {
$visitor_browser = "Maxthon(Microsoft IE)";
} elseif (strpos($user_OSagent, "Maxthon 2.0")) {
$visitor_browser = "Maxthon 2.0";
} elseif (strpos($user_OSagent, "Maxthon")) {
$visitor_browser = "Maxthon";
} elseif (strpos($user_OSagent, "Edge")) {
$visitor_browser = "Edge";
} elseif (strpos($user_OSagent, "Trident")) {
$visitor_browser = "IE";
} elseif (strpos($user_OSagent, "MSIE")) {
$visitor_browser = "IE";
} elseif (strpos($user_OSagent, "MSIE")) {
$visitor_browser = "MSIE 较高版本";
} elseif (strpos($user_OSagent, "NetCaptor")) {
$visitor_browser = "NetCaptor";
} elseif (strpos($user_OSagent, "Netscape")) {
$visitor_browser = "Netscape";
} elseif (strpos($user_OSagent, "Chrome")) {
$visitor_browser = "Chrome";
} elseif (strpos($user_OSagent, "Lynx")) {
$visitor_browser = "Lynx";
} elseif (strpos($user_OSagent, "Opera")) {
$visitor_browser = "Opera";
} elseif (strpos($user_OSagent, "MicroMessenger")) {
$visitor_browser = "微信浏览器";
} elseif (strpos($user_OSagent, "Konqueror")) {
$visitor_browser = "Konqueror";
} elseif (strpos($user_OSagent, "Mozilla/5.0")) {
$visitor_browser = "Mozilla";
} elseif (strpos($user_OSagent, "Firefox")) {
$visitor_browser = "Firefox";
} elseif (strpos($user_OSagent, "CriOS")) {
$visitor_browser = "CriOS";
} elseif (strpos($user_OSagent, "Safari")) {
$visitor_browser = "Safari";
} else {
$visitor_browser = "其它";
}
return $visitor_browser;
}
////获得访客浏览器语言
function Get_Lang(){
if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$lang = substr($lang,0,5);
if(preg_match("/zh-cn/i",$lang)){
$lang = "简体中文";
}
elseif(preg_match("/zh/i",$lang)){
$lang = "繁体中文";
}
else{
$lang = "English";
}
return $lang;
}
else{
return "unknow";
}
}
////获得访客真实 ip
function Get_Ip_Addr(){
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //获取代理 ip
$ips = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
}
if($ip){
$ips = array_unshift($ips,$ip);
}
$count = count($ips);
for($i=0;$i<$count;$i++){
if(!preg_match("/^(10|172\.16|192\.168)\./i",$ips[$i])){//排除局域网 ip
$ip = $ips[$i];
break;
}
}
$tip = empty($_SERVER['REMOTE_ADDR']) ? $ip : $_SERVER['REMOTE_ADDR'];
if($tip=="127.0.0.1"){ //获得本地真实 IP
return self::get_onlineip();
}
else{
return $tip;
}
}
/*
////根据 ip 获得访客所在地地名--网络不友好,不使用。
function Get_Ip_From_taobao($USER_IP){
if(empty($USER_IP)){
$USER_IP = self::Getip();
}
$ip_json=@file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$USER_IP");//根据 taobao ip
$ip_json=json_decode($ip_json);
if($ip_json)
{
return $ip_json;
}
else
{
return $ip_json;
//return "匹配失败!";
}
}
*/
//根据 ip 获取网络所在地址。
function Get_Ip_From_126($USER_IP){
if(empty($USER_IP)){return 'IP Error';}
$ip_json=@file_get_contents("http://ip.ws.126.net/ipquery?ip=$USER_IP");
$encode = mb_detect_encoding($ip_json, array("ASCII","UTF-8","GB2312","GBK","BIG5"));
// echo $encode;
$ip_json = iconv($encode,"UTF-8//IGNORE",$ip_json);
if($ip_json){return $ip_json;}
else{return $ip_json;}
}
function get_os(){
$agent = $_SERVER['HTTP_USER_AGENT'];
$os = false;
if (preg_match('/win/i', $agent) && strpos($agent, '95'))
{
$os = 'Windows 95';
}
else if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90'))
{
$os = 'Windows ME';
}
else if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent))
{
$os = 'Windows 98';
}
else if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent))
{
$os = 'Windows Vista';
}
else if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent))
{
$os = 'Windows 7';
}
else if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent))
{
$os = 'Windows 8';
}else if(preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent))
{
$os = 'Windows 10';#添加 win10 判断
}else if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent))
{
$os = 'Windows XP';
}
else if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent))
{
$os = 'Windows 2000';
}
else if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent))
{
$os = 'Windows NT';
}
else if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent))
{
$os = 'Windows 32';
}
else if (preg_match('/ipad/i', $agent))
{
$os = 'iPad';
}
else if (preg_match('/macintosh/i', $agent))
{
$os = 'Mac';
}
else if (preg_match('/iphone/i', $agent))
{
$os = 'iPhone';
}
else if (preg_match('/android/i', $agent))
{
$os = 'Android';
}
else if (preg_match('/linux/i', $agent))
{
$os = 'Linux';
}
else if (preg_match('/unix/i', $agent))
{
$os = 'Unix';
}
else if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent))
{
$os = 'SunOS';
}
else if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent))
{
$os = 'IBM OS/2';
}
else if (preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent))
{
$os = 'Macintosh';
}
else if (preg_match('/PowerPC/i', $agent))
{
$os = 'PowerPC';
}
else if (preg_match('/AIX/i', $agent))
{
$os = 'AIX';
}
else if (preg_match('/HPUX/i', $agent))
{
$os = 'HPUX';
}
else if (preg_match('/NetBSD/i', $agent))
{
$os = 'NetBSD';
}
else if (preg_match('/BSD/i', $agent))
{
$os = 'BSD';
}
else if (preg_match('/OSF1/i', $agent))
{
$os = 'OSF1';
}
else if (preg_match('/IRIX/i', $agent))
{
$os = 'IRIX';
}
else if (preg_match('/FreeBSD/i', $agent))
{
$os = 'FreeBSD';
}
else if (preg_match('/teleport/i', $agent))
{
$os = 'teleport';
}
else if (preg_match('/flashget/i', $agent))
{
$os = 'flashget';
}
else if (preg_match('/webzip/i', $agent))
{
$os = 'webzip';
}
else if (preg_match('/offline/i', $agent))
{
$os = 'offline';
}
else
{
$os = '未知操作系统';
}
return $os;
}
?>
?>
读取数据库并界面显示
<?php
header("Content-Type: text/html; charset=utf-8");//防止界面乱码
$con=mysqli_connect('localhost','weask_win','123456789lt','weask_win');//数据库用户名,密码
if(!$con){
die('connect failed!');
}
mysqli_query($con,"SET NAMES utf8");//解决数据库中有汉字时显示在前台出现乱码问题
$result = mysqli_query($con,"SELECT * FROM wp_count order by id desc");
//print_r(mysql_fetch_array($result));//取得第一条数据
echo ('
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>访问记录日志</title>
<meta name="keywords" content="访问记录日志"/>
<meta name="description" content="访问记录日志"/>
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="../css/common.css" />
</head>
<body>
<div class="container">
<div class="demo">
<h2 class="title">
<center>访问记录日志</center>
</h2>
<div style="width:800px;margin:auto">
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>IP 地址</th>
<th>IP 地区</th>
<th>OS 操作系统</th>
<th>浏览器</th>
<th>语言</th>
<th>Time 访问时间</th>
</tr>
</thead>
<tbody>
');
while($row = mysqli_fetch_array($result))//转成数组,且返回第一条数据,当不是一个对象时候退出
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['ip'] . "</td>";
echo "<td>" . $row['addr'] . "</td>";
echo "<td>" . $row['user_os'] . "</td>";
echo "<td>" . $row['brower'] . "</td>";
echo "<td>" . $row['b_language'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "</tr>";
}
echo "</tbody></table></div></div></div></body></html>";
mysqli_close($con);
?>

