簡體   English   中英

PHP fwrite()未寫入日志文件

[英]PHP fwrite() not writing to log file

作為我的第一個PHP項目之一,我正在創建一個IP記錄腳本,該腳本記錄用戶的IP地址。 由於某種原因,我的fwrite()函數似乎未寫入我的日志文件。

有人可以幫我嗎?

<?php
// IP Logger Script
// By Sam Lev
// sam@levnet.us
$iplogfile = 'iplog.txt';
$ipaddress = $_SERVER['REMOTE_ADDR'];
$webpage = $_SERVER['SCRIPT_NAME'];
$timestamp = date('m/d/Y h:i:s');
$browser = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen($iplogfile, 'a+');
chmod($iplogfile, 0777);
fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$webpage.' '.$browser. "\r\n");
fclose($fp);
echo "IP ADDRESS: $ipaddress <br />\n";
echo "TIMESTAMP: $timestamp <br />\n";
echo "BROWSER: $browser <br />\n";
echo "Information logged to server. <br />\n";
?>

運行腳本后,iplog.txt仍為空白。 一切都很好。

謝謝

不應該

$fp = fopen($file, 'a');

$fp = fopen($iplogfile, 'a');

因為我看不到$file的定義。

您的代碼簽出了,這是一個權限問題。

手動將文件更改為0777

或添加chmod($iplogfile, 0777); $fp = fopen($iplogfile, 'a');

chmod是標准的服務器命令,並非PHP專有。

這是我的日志記錄代碼,如果有幫助,但是在添加代碼chmod后將日志文件添加到0777,否則它將無法正常工作,因為添加代碼無法使其正常工作,並且會顯示php錯誤,因此為什么不能在代碼中寫btw您可能必須創建手動記錄文件,但很容易,這是我的網站www.nzquakes.maori.nz謝謝您的幫助

<!-- Below Code Logs ONLY User's IP Address & Time Stamp & Browser Info To http://www.example.com/logs/ip-address-mainsite.txt -->

<?php
$iplogfile = 'full path to your logs goes here eg http://www.example.com/logs/ip-address-mainsite.txt';
$ipaddress = $_SERVER['REMOTE_ADDR'];

//load the file
$file = file_get_contents($iplogfile);

//check to see if the ipaddress is already in the file
if ( ! preg_match("/$ipaddress/", $file )) {
//nope, log it!
$webpage = $_SERVER['SCRIPT_NAME'];
$timestamp = date('d/m/Y h:i:s');
$browser = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen($iplogfile, 'a+');
fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$browser. "\r\n");
fclose($fp);
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM