Olivier
Olivier

Reputation: 63

Using PHP to create HTML file

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

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."\r\r\n");
fclose($f);
?>
<img src="img.JPG">
<!--http://php.net/manual/en/reserved.variables.server.php -->
</body>
</html>

Upvotes: 1

Views: 5702

Answers (3)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

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."\r\r\n");
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! )

Upvotes: 1

crafter
crafter

Reputation: 6296

I would create 2 separate files. (My files are simplified, to demonstrate the strategy.

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>

Then, as your main script is called, you append html code to 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);

Upvotes: 1

Gavriel
Gavriel

Reputation: 19237

$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>";

Upvotes: 0

Related Questions