Jump to content


Photo

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


  • Please log in to reply
9 replies to this topic

#1 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 11 July 2006 - 03:02 AM

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!

#2 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 11 July 2006 - 03:05 AM

Why are you using fopen AND file_get_contents?  Also, it sounds more like you are needing file() to place into an array and loop through the content.
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#3 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 11 July 2006 - 03:12 AM

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!

#4 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 11:07 AM

Ok, just tell us the format of the file and it should be easy enough.
If each line is like this:
214.168.245.222 2005/07/06

Then what you might do is:
$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/>');
}

It all depends on the line format.

<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 July 2006 - 02:59 PM

If you just want to read and dump the contents of the file to the screen, the following code will work fine:
<?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>';
?>

Ken
 

#6 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 11 July 2006 - 05:22 PM

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:

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



We're really close! Any ideas? :)

#7 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 11 July 2006 - 05:26 PM

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!

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 July 2006 - 05:28 PM

Please post the code for each attempt. I have a feeling that you used our code snippets verbatim without modifying them for your needs.

Ken

#9 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 11 July 2006 - 05:49 PM

Here's what I wound up with:

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


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

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

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!



#10 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 11 July 2006 - 07:02 PM

I figured out the way to delete the file contents using the 'ftruncate' method.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users