帮助调试 php 页面
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“服务器错误”意味着“您必须在服务器 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.
我无法回答您有关调试和日志记录的问题,但它的价值是什么。你的错误在第6行。
将其替换为以下内容:
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:
鲍勃编码器在这里! 2019 更新
虽然这被标记为已解决/已接受/无论如何,但根据文档,答案似乎是错误的修复。许多新手和经验丰富的程序员都认为这是一个三元 if 语句。 (不管你拼写三元,哈哈。)但原始海报(op)的说法不同!
虽然我可能是错的,但我认为问题是:(
其中 $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:
(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).
其中 log.log 是您的日志文件
where log.log is your log file