帮助调试 php 页面

发布于 2024-10-27 08:24:18 字数 1263 浏览 6 评论 0原文

<?php
error_reporting(E_ALL);
// Getting the information 
$ipaddress = $_SERVER['REMOTE_ADDR']; 
$page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";
if(!empty($_SERVER['QUERY_STRING']) $page .= $_SERVER['QUERY_STRING'];
$referrer = $_SERVER['HTTP_REFERER'];
$datetime = mktime(); 
$useragent = $_SERVER['HTTP_USER_AGENT'];
$remotehost = getHostByAddr($ipaddress);

// Create log line
$logline = $ipaddress . '|' . $referrer . '|' . $datetime . '|' . $useragent . '|' . $remotehost . '|' . $page . "\n"; 
echo $logline;
// Write to log file: 
//$logfile = 'logfile.txt';
$logfile = '/home/www/agro-dive.onlinewebshop.net/logfile.txt';

// Open the log file in "Append" mode 
if (!$handle = fopen($logfile, 'a+')) {
    die("Failed to open log file"); 
} 

// Write $logline to our logfile. 
if (fwrite($handle, $logline) == FALSE) {
    die("Failed to write to log file"); 
} 

fclose($handle);  

?>

如果我尝试打开此 php,它会给我一个服务器错误

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

,并且我还按照以下步骤对其进行了测试 这里,但它没有产生任何日志消息

<?php
error_reporting(E_ALL);
// Getting the information 
$ipaddress = $_SERVER['REMOTE_ADDR']; 
$page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";
if(!empty($_SERVER['QUERY_STRING']) $page .= $_SERVER['QUERY_STRING'];
$referrer = $_SERVER['HTTP_REFERER'];
$datetime = mktime(); 
$useragent = $_SERVER['HTTP_USER_AGENT'];
$remotehost = getHostByAddr($ipaddress);

// Create log line
$logline = $ipaddress . '|' . $referrer . '|' . $datetime . '|' . $useragent . '|' . $remotehost . '|' . $page . "\n"; 
echo $logline;
// Write to log file: 
//$logfile = 'logfile.txt';
$logfile = '/home/www/agro-dive.onlinewebshop.net/logfile.txt';

// Open the log file in "Append" mode 
if (!$handle = fopen($logfile, 'a+')) {
    die("Failed to open log file"); 
} 

// Write $logline to our logfile. 
if (fwrite($handle, $logline) == FALSE) {
    die("Failed to write to log file"); 
} 

fclose($handle);  

?>

if i try to open this php it gives me a server error

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

and i have also tested it follow the steps here, but it didn't produce any log message

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧故 2024-11-03 08:24:18

“服务器错误”意味着“您必须在服务器 error_log 中查找特定错误消息”

调试代表读取错误消息,而不是观看代码或猜测。

"server error" means "you have to look into servers error_log for the particular error message".

Debugging stands for reading error messages, not watching the code nor guessing.

玩套路吗 2024-11-03 08:24:18

我无法回答您有关调试和日志记录的问题,但它的价值是什么。你的错误在第6行。
将其替换为以下内容:

if(!empty($_SERVER['QUERY_STRING']))
    $page .= $_SERVER['QUERY_STRING'];

I can't answer your question about debugging and logging but for what it's worth. Your error is on line 6.
Replace it by following:

if(!empty($_SERVER['QUERY_STRING']))
    $page .= $_SERVER['QUERY_STRING'];
一身仙ぐ女味 2024-11-03 08:24:18

鲍勃编码器在这里! 2019 更新

虽然这被标记为已解决/已接受/无论如何,但根据文档,答案似乎是错误的修复。许多新手和经验丰富的程序员都认为这是一个三元 if 语句。 (不管你拼写三元,哈哈。)但原始海报(op)的说法不同!

虽然我可能是错的,但我认为问题是:(

$page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}"; 
$page .= iif(!empty($_SERVER['QUERY_STRING']), "?{$_SERVER['QUERY_STRING']}", ""); 

其中 $page 是第二行代码的附加内容。)

许多新手和经验丰富的程序员都认为这是一个三元 if 语句。 (不管你拼写三元,哈哈。)

在原始文档中,OP声明这是一个函数调用。 https://www.go4expert.com/articles/track-visitors -using-php-t195/

引用文档中的OP........
“注意:我在上面的示例中使用了一个名为 iif() 的函数。您可以在 获取此函数http://www.phpit.net/code/iif-function。”

遗憾的是,到 iif() 函数的链接已失效,因此也许有人拥有该函数的旧代码,可以将其发布到此处或其他存储库。

虽然上面的其他更正可能允许代码测试真或假并失败并处理代码的其余部分,但我认为OP可能将其“iif()用于其他目的作为函数(函数的错误编程选择)称呼)。

BobDcoder here! 2019 update

While this is marked as solved/accepted/whatever, the answer appears to be the wrong fix according to the documentation. Many newbies and seasoned coders alike believe this to be a ternary if statement. ( however you spell ternary,lol.) but the original poster (op) states differently!

while I could be wrong I think the problem is:

$page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}"; 
$page .= iif(!empty($_SERVER['QUERY_STRING']), "?{$_SERVER['QUERY_STRING']}", ""); 

(which $page is an append to on the second line of code.)

many newbies and seasoned coders alike believe this to be a ternary if statement. ( however you spell ternary,lol.)

In the original documentation, the OP states that this is a function call. https://www.go4expert.com/articles/track-visitors-using-php-t195/

To quote the OP in the documentation........
"Note: I used a function in the above example called iif(). You can get this function at http://www.phpit.net/code/iif-function."

Sadly the link to the iif() function is dead, so maybe someone has the old code for this function and can post it here or at some other repository.

While the other correction above may allow the code to test true or false and fall through and process the rest of the code, I think the OP may have used it "iif() for other purposes as a function (bad programing choice of a function call).

抽个烟儿 2024-11-03 08:24:18
// Write to log file: 
//$logfile = 'logfile.txt';
$logfile = 'log.log';

其中 log.log 是您的日志文件

// Write to log file: 
//$logfile = 'logfile.txt';
$logfile = 'log.log';

where log.log is your log file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文