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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b