dongtiao5094 2016-01-12 21:21
浏览 132
已采纳

使用PHP创建HTML文件

I have a index.php file that loads server details with (for example)

$ip         = $_SERVER['REMOTE_ADDR'];
$browser    = $_SERVER['HTTP_USER_AGENT'];

at the end of the script an jpg will be displayed.

Now I would like the index.php file to create a single html file with a table format in it where the data gets imported / written to from the index.php.

So in the end I would like to open log.html and then see all the index.php submits in a html table.

Please help.

I already tried:

$f=fopen($file, 'a');
fwrite($f,$data."
");
fclose($f);

the current situation is that the code I wrote, the output goes to the log.txt file as plain text. Every submit is added as newline.

Sorry for inconvenience, below full code:

    <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."
");
    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>

and this page: 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."
");
fclose($f);
?>
<img src="img.JPG">
<!--http://php.net/manual/en/reserved.variables.server.php -->
</body>
</html>
  • 写回答

3条回答 默认 最新

  • dongshen6060 2016-01-12 22:11
    关注

    The optimal decision in such case would be NOT "to create a single html file" each time when index.php get to run. You should create base static structure of your html file beforehand with separation on templates. All you need is to append is new table row <tr> with filled cells <td>.
    Create the two new html files on the same level as your current script (though I would better place them into separate folder). The first file, lets say base.html, should contain the following markup:
    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>
    

    The second file, lets say bottom.html, will be the 'closing' fragment of intended html file and should contain the following markup:
    bottom.html:

           </table>
        </body>
    </html>
    

    Then rename the file in your index.php like this:

    $file = "log.html";
    

    Replace this code:

    $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."
    ");
    fclose($f);
    

    with the following:

    $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');
    

    That's all! )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路