我有一个index.php文件,用于(例如)
加载服务器详细信息$ip = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER['HTTP_USER_AGENT'];
在脚本末尾将显示一个jpg。
现在我希望index.php文件创建一个带有表格格式的html文件,其中数据从index.php导入/写入。
所以最后我想打开log.html,然后在html表中看到所有index.php提交。
请帮忙。
我已经尝试过:
$f=fopen($file, 'a');
fwrite($f,$data."\r\r\n");
fclose($f);
目前的情况是我写的代码,输出以纯文本形式传递给log.txt文件。每个提交都添加为换行符。
很抱歉给您带来不便,请填写以下完整代码:
<html>
<?php
$file = "log.txt";
$date = date("d-m-y");
$time = date("H:i:s");
$ip = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER['HTTP_USER_AGENT'];
$user = $_SERVER['REMOTE_USER'];
$remote_usr = $_SERVER['REDIRECT_REMOTE_USER'];
$host = $_SERVER['REMOTE_HOST'];
$method = $_SERVER['REQUEST_METHOD'];
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip1=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip1=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip1=$_SERVER['REMOTE_ADDR'];
}
return $ip1;
}
$ip2 = getRealIpAddr();
$data = "Date: ".$date." ,Time: ".$time.",User:".$user.", RM user: ".$remote_usr.", Host:".$host.",IP: ".$ip.", IP2: ".$ip2.", Browser: ".$browser;
$f=fopen($file, 'a');
fwrite($f,$data."\r\r\n");
fclose($f);
?>
<img src="img.JPG">
<!--http://php.net/manual/en/reserved.variables.server.php -->
</body>
</html>
----------------
i did a lot of googling, i solved my problem with the following: gen.php
<?php
$lines = file("log.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$data = array_map(function($v){
list($username, $score, $user, $user_rm, $host, $ip, $ip2, $browser) = explode("+", $v);
return [
"Date" => $username,
"Time" => $score,
"User" => $user,
"RM user" => $user_rm,
"Host" => $host,
"IP" => $ip,
"IP2" => $ip2,
"Browser" => $browser
];
}, $lines);
/* usort($data, function($a, $b){
if($a["score"] == $b["score"])
return 0;
return $a["score"] > $b["score"] ? 1 : -1;
});*/
?>
<table width="100%" border="1" >
<tr>
<td style="background-color:grey;"><b>Date</b></td>
<td style="background-color:grey;"><b>Time</b></td>
<td style="background-color:grey;"><b>User</b></td>
<td style="background-color:grey;"><b>RM User</b></td>
<td style="background-color:grey;"><b>Host</b></td>
<td style="background-color:grey;"><b>IP</b></td>
<td style="background-color:grey;"><b>IP2</b></td>
<td style="background-color:grey;"><b>Browser</b></td>
</tr>
<?php foreach($data as $user){ ?>
<tr>
<td height="100%"><?php echo $user["Date"]; ?></td>
<td><?php echo $user["Time"]; ?></td>
<td><?php echo $user["User"]; ?></td>
<td><?php echo $user["RM user"]; ?></td>
<td><?php echo $user["Host"]; ?></td>
<td><?php echo $user["IP"]; ?></td>
<td><?php echo $user["IP2"]; ?></td>
<td><?php echo $user["Browser"]; ?></td>
</tr>
<?php } ?>
</table>
和本页:index.php
<html>
<?php
$file = "log.txt";
$date = date("d-m-y");
$time = date("H:i:s");
$ip = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER['HTTP_USER_AGENT'];
$user = $_SERVER['REMOTE_USER'];
$remote_usr = $_SERVER['REDIRECT_REMOTE_USER'];
$host = $_SERVER['REMOTE_HOST'];
$method = $_SERVER['REQUEST_METHOD'];
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip1=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip1=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip1=$_SERVER['REMOTE_ADDR'];
}
return $ip1;
}
$ip2 = getRealIpAddr();
$data = "Date: ".$date." + Time: ".$time."+ User:".$user."+ RM user: ".$remote_usr."+ Host:".$host."+ IP: ".$ip."+ IP2: ".$ip2."+ Browser: ".$browser;
$f=fopen($file, 'a');
fwrite($f,$data."\r\r\n");
fclose($f);
?>
<img src="img.JPG">
<!--http://php.net/manual/en/reserved.variables.server.php -->
</body>
</html>
答案 0 :(得分:1)
我会创建2个单独的文件。 (我的文件被简化,以展示策略。
<强> results.php 强>
<html>
<title>The results</title>
<body>
<table>
<tr>
<td>IP Address</td>
<td>Browser</td>
</tr>
<?php include("contents.incl");
<table>
</body>
</html>
然后,在调用主脚本时,将html代码附加到contents.incl
$f=fopen($file, 'a');
$rowMarkup = '<tr>';
$rowMarkup .= ' <td>' . $ip . '</td>';
$rowMarkup .= ' <td>' . $browser . '</td>';
$rowMarkup = '</tr>';
fwrite($f,$rowMarkup."\r\r\n");
fclose($f);
答案 1 :(得分:1)
在这种情况下,最佳决策是&#34;每次index.php
运行时创建单个html文件&#34; 。您应该预先创建html
文件的基本静态结构,并在模板上进行分离。您只需添加带有填充单元格<tr>
的新表格行<td>
即可
在与当前脚本相同的级别上创建两个新的html
文件(尽管我最好将它们放在单独的文件夹中)。
第一个文件,例如base.html
,应该包含以下标记:
base.html文件:
<!DOCTYPE html>
<html>
<head>
<title>oop</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<table>
<tr>
<th>Date</th>
<th>Time</th>
<th>User</th>
<th>RM user</th>
<th>Host</th>
<th>IP</th>
<th>IP2</th>
<th>Browser</th>
</tr>
第二个文件,即bottom.html
,将是&#39;关闭&#39;预期html文件的片段,应包含以下标记:
的bottom.html:
</table>
</body>
</html>
然后重命名index.php
中的文件,如下所示:
$file = "log.html";
替换此代码:
$ip2 = getRealIpAddr();
$data = "Date: ".$date." ,Time: ".$time.",User:".$user.", RM user: ".$remote_usr.", Host:".$host.",IP: ".$ip.", IP2: ".$ip2.", Browser: ".$browser;
$f=fopen($file, 'a');
fwrite($f,$data."\r\r\n");
fclose($f);
以下内容:
$ip2 = getRealIpAddr();
$data = "<tr><td>".$date."</td><td>".$time."</td><td>".$user."</td><td> ".$remote_usr."</td><td>".$host."</td><td>".$ip."</td><td>".$ip2."</td><td> ".$browser."</td></tr>";
file_put_contents($file, $data, FILE_APPEND);
...
// To output the final html file use:
readfile('base.html');
readfile('log.html');
readfile('bottom.html');
这就是全部! )
答案 2 :(得分:0)
$data = "";
if (filesize($file)==0) {
$data .= "<html><body><table>";
}
$data .= "<tr><td>Date: ".$date."</td><td>Time: ".$time."</td><td>User:".$user."</td><td>RM user: ".$remote_usr."</td><td>Host:".$host."</td><td>IP: ".$ip."</td><td>IP2: ".$ip2."</td><td>Browser: ".$browser."</td></tr>";