我正在根据下面的 PHP 代码捕获网页查看器日志...
$viewerlog = "../SiteLogFiles/live-smoothstreaming-" . date('Ymd') . ".txt";
$fp = fopen($viewerlog,"a");
if ($fp) {
fputs($fp,"-----------------------------------------------------------------------\r\n");
fputs($fp,"Server Date: $now\r\n");
fputs($fp,"IP Address: $ip\r\n");
fputs($fp,"City: $city\r\n");
fputs($fp,"State: $state\r\n");
fputs($fp,"Country Name: $countryname\r\n");
fputs($fp,"Continent: $continent\r\n");
fputs($fp,"-----------------------------------------------------------------------\r\n");
fclose($fp);
}
它会产生这样的日志......
-----------------------------------------------------------------------
Server Date: 2013-01-06T05:32:06-06:00
IP Address: 157.55.32.61
City: Unknown
State: Unknown
Country Name: United States
Continent: North America
-----------------------------------------------------------------------
-----------------------------------------------------------------------
Server Date: 2013-01-06T06:02:40-06:00
IP Address: 157.55.32.187
City: Unknown
State: Unknown
Country Name: United States
Continent: North America
-----------------------------------------------------------------------
但时不时地,日志会被损坏,看起来像这样......
-----------------------------------------------------------------------
Server Date: 2012-12-21T18:27:27-06:00
IP Address: 99.54.103.128
City: Columbus
State: Ohio
Country Name: United States
Continent: North America
-----------------------------------------------------------------------
-----------------------------------------------------------------------
Server Date: 2012-12-21T18:27:53-06:00
IP Address: 75.97.98.115
City: Palmerton
State: Pennsylvania
--------------------------------------Continent: North America
Server Date: 2012-12-21T18:27:54-06:00
---------------------
IP Address: 70.76.36.232
City: Saskatoon
State: Saskatchewan
Country Name: Canada
Continent: North America
-----------------------------------------------------------------------
-----------------------------------------------------------------------
Server Date: 2012-12-21T18:28:04-06:00
IP Address: 173.17.240.190
City: Springfield
State: Missouri
Country Name: United States
Continent: North America
-----------------------------------------------------------------------
值得注意的是,我使用http://www.easyjquery.com从 IP 地址获取位置信息。
因此,许多人建议使用 SQL 数据库进行日志记录,而不是纯文本日志。有了这些建议,我就能够建立上述 SQL 数据库。我能够在 1 秒内写入 7 条记录(尽可能快地按 F5),没有问题。
使用中的PHP代码如下...
try {
$dbh = new PDO("sqlsrv:Server=localhost;Database=ViewerLogs", "", "");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->query("INSERT INTO dbo.LiveSmoothStreaming (DateTime, Ip, City, State, Country, Continent) VALUES('$now', '$ip', '$city', '$state', '$countryname', '$continent')");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$dbh = null;
}
catch(PDOException $e) {
echo $e->getMessage();
}
不过,有一个问题。我的纯文本日志似乎比 SQL 日志捕获了更多的记录。
纯文本日志正在生成...
Tue 5:58pm 68.55.34.32 Laurel Maryland United States North America
Tue 5:58pm 70.88.57.62 Port Charlotte Florida United States North America
Tue 5:59pm 98.19.198.54 Eastview Kentucky United States North America
Tue 5:59pm 71.3.202.110 Cape Coral Florida United States North America
Tue 5:59pm 173.218.32.154 Branson Missouri United States North America
Tue 5:59pm 184.38.238.64 Unknown Unknown United States North America
Tue 5:59pm 67.239.18.161 Naples Florida United States North America
并且 SQL 日志正在生成...
2013-02-05T17:58:45-06:00 68.55.34.32 Laurel Maryland United States North America
2013-02-05T17:58:52-06:00 70.88.57.62 Port Charlotte Florida United States North America
2013-02-05T17:59:06-06:00 71.3.202.110 Cape Coral Florida United States North America
2013-02-05T17:59:23-06:00 67.239.18.161 Naples Florida United States North America
您可以看到,在短短几分钟内,就有了差异。在几个小时的过程中,差异可能达到数百个。
在这个特定的纯文本日志中没有损坏,所以我看不出它怎么可能是错误的。这将表明 SQL 日志存在问题。我查看了我的 PHP 错误日志中可能存在的问题,但没有发现任何明显的问题(我只是搜索了有问题的页面并查看了这些错误)。
关于为什么 SQL 不会捕获纯文本日志的记录,有什么想法吗?在我的代码中,SQL 日志记录直接出现在纯文本日志记录之后(也许将 SQL 放在纯文本之前会产生相反的结果?)。