Jump to content

How to format the display when summoning entries in a text file?


Recommended Posts

Hello:

Here's a summary. I have a small script writing the IP address and access date to a simple text file. Next I created another little script to display that text file. However, it just displays all the content of the text file in a lump. How would I have it display the content in a nicely formatted one-listing-per-line format?

Here's the code of the display:

<?php //IP logger file viewer - set variables
$filename = "../pictures/ips.txt"; //edit location here
$path = "/home2/xxxxxxt/public_html/pictures/";
$title = "<center><h4>IP Log File - Unauthorized Access Results</h4></center><p>";
$data = file_get_contents($filename) or die('Cannot open $filename');

echo $title;

print "<table align='center' width='600' border='0'><tr><td>";
fopen($filename, 'r') or die("cannot open $filename");
echo $data;
echo "</tr></td><tr><td><p><font face='Verdana' size='2'><strong>When you are through viewing these results click below to close this file.</strong></font><p></td></tr></table>";
?>
<center><form action="closefile.php" method="POST">
<input type="submit" name="submit" value="Close File">
</form></center>

This works pretty much the way I wanted it to but the 'echo $data' just puts the glob in the middle of the page.

I'd search the forums for this answer but I honestly don't have a clue what search term would pull the results. Thanks in advance for the help!
heh heh... well, i'm using those cuz i'm a total noob. Just learning this stuff and fighting my way from tutorial to tutorial and forum to forum making these little cheesbrain scripts. Sort of the crawl before I run kinda thing.

I'm using fopen since the description of that function is what I needed it to do. Same with the file_get_contents. That did the trick. But, I'm sure there's much better ways to do this but I just don't have the knowledge... yet ;)

An example of the proper code would set me on the right track!
Ok, just tell us the format of the file and it should be easy enough.
If each line is like this:
[code]
214.168.245.222 2005/07/06
[/code]

Then what you might do is:
[code]
$cont = file('ips.txt');
foreach($cont as $line)
{
  $spl = explode(" ",$line);
  $ip = $line[0];
  $time = $line[1];
echo('Access from IP: '.$ip.' at: '.$time.'<br/>');
}
[/code]

It all depends on the line format.
If you just want to read and dump the contents of the file to the screen, the following code will work fine:
[code]<?php
$inp = file('ips.txt');
echo '<pre>';
foreach($inp as $line)
  echo htmlentities($line);  // just in case you have things that look like HTML tags
echo '</pre>';
?>[/code]

Ken
 
Shogun, your guess on the file contents was right on. It actually looks like this:

IP: 71.231.197.177 - DATE: Sunday 09 July 12:24:31

I used your code and this is the display that resulted, however:

Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:
Access from IP: I at: P
Access from IP: at:

So, something's amiss there. It's splitting the first two letters and displaying those. It would be great if it laid out the way your code logic is outlined whereas it would be 'Someone at IP 444.44.44.444 hit this page on Monday July 10 2006 at 9:45am' showing on each line.

Kenrbnsn, your code worked ok on the first line. Then the rest of them were that jumbled mess. Here's a snapshot:

[quote]IP Log File - Unauthorized Access Results

IP: 71.231.197.177 - DATE: Sunday 09 July 11:50:03

IP: 71.231.197.177 - DATE: Sunday 09 July 12:01:25 IP: 71.231.197.177 - DATE: Sunday 09 July 12:02:06 IP: 71.231.197.177 - DATE: Sunday 09 July 12:23:45 IP: 71.231.197.177 - DATE: Sunday 09 July 12:23:47 IP: 69.93.229.186 - DATE: Sunday 09 July 12:28:35 IP: 69.93.229.186 - DATE: Sunday 09 July 12:39:24 IP: 69.93.229.186 - DATE: Sunday 09 July 12:45:51 IP: 69.93.229.186 - DATE: Sunday 09 July 12:45:54 IP: 71.231.197.177 - DATE: Sunday 09 July 13:40:43 IP: 81.157.199.76 - DATE: Sunday 09 July 13:44:23 IP: - DATE: Sunday 09 July 17:18:10 IP: - DATE: Sunday 09 July 17:19:01 IP: - DATE: Sunday 09 July 17:19:58 IP: - DATE: Sunday 09 July 17:20:36 IP: - DATE: Sunday 09 July 17:20:38 IP: - DATE: Sunday 09 July 17:20:39 IP: - DATE: Sunday 09 July 17:20:40 IP: 71.231.197.177 - DATE: Sunday 09 July 17:22:12 IP: 71.231.197.177 - DATE: Sunday 09 July 17:23:43 IP: 71.231.197.177 - DATE: Sunday 09 July 17:24:05 IP: 71.231.197.177 - DATE: Sunday 09 July 17:24:33 IP: 71.231.197.177 - DATE: Monday 10 July 11:45:21 IP: 71.231.197.177 - DATE: Monday 10 July 11:47:47 IP: 71.231.197.177 - DATE: Monday 10 July 11:52:54 IP: 71.231.197.177 - DATE: Monday 10 July 11:52:56 IP: 71.231.197.177 - DATE: Monday 10 July 15:04:03[/quote]


We're really close! Any ideas? :)
Hold the presses! Ok, Kenrbnsn... in your code I noticed you left out the { }'s in the foreach statement. I didn't know if that was intentional or not so I originally left them in which produced the first line being formatted as shown above. After my post I removed them, reupoaded and then ran the script and the formatting came out fine on each one. Like this (IP's removed on purpose):

IP:  - DATE: Sunday 09 July 17:18:10

IP:  - DATE: Sunday 09 July 17:19:01

IP:  - DATE: Sunday 09 July 17:19:58

IP:  - DATE: Sunday 09 July 17:20:36

IP:  - DATE: Sunday 09 July 17:20:38

IP:  - DATE: Sunday 09 July 17:20:39

IP:  - DATE: Sunday 09 July 17:20:40


So, this works! But, I don't know WHY it works. Could I trouble you for a quick explanation of what that line of code does/means so I can put it in my memory banks? Thanks!
Here's what I wound up with:

[code]<?php //IP logger file viewer - set variables
$filename = "../pictures/ips.txt"; //edit location here
$path = "/home2/wwwxxx/public_html/pictures/";
$title = "<center><h4>IP Log File - Unauthorized Access Results</h4></center><p>";
$data = file($filename) or die('Cannot open $filename');

echo $title;

print "<table align='center' width='600' border='0'><tr><td>";
// loop through array and print each line
echo '<pre>';
foreach ($data as $line)
  echo htmlentities($line);
  echo '</pre><br/>';

echo "</tr></td><tr><td><p><font face='Verdana' size='2'><strong>When you are through viewing these results click below to close this file.</strong></font><p></td></tr></table>";
?>[/code]


This produced the required results. The first attempt included the brackets around your echo($data as $line) etc etc section like this:

[code]<?php //IP logger file viewer - set variables
$filename = "../pictures/ips.txt"; //edit location here
$path = "/home2/wwwxxx/public_html/pictures/";
$title = "<center><h4>IP Log File - Unauthorized Access Results</h4></center><p>";
$data = file($filename) or die('Cannot open $filename');

echo $title;

print "<table align='center' width='600' border='0'><tr><td>";
// loop through array and print each line
echo '<pre>';
foreach ($data as $line) {
  echo htmlentities($line);
  echo '</pre><br/>';
}
echo "</tr></td><tr><td><p><font face='Verdana' size='2'><strong>When you are through viewing these results click below to close this file.</strong></font><p></td></tr></table>";
?>[/code]

Which produced the first line being formatted properly and the ones following were still in a lump.

Another question, say I want to give the 'admin' viewing these stats the ability to delete these entries in one swipe. How would I code that? I'm assuming there'd be a form button with an action pointing back to SELF that would run the file contents/delete code. Once again, i'm complete noob so this may seem basic but it's like building the pyramids for me :)

Thanks again for your help!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.