IP Logger

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

IP Logger

Post by Dale »

I've made an IP Logger:

Heres the page that logs it:
http://dframe.t35.com/iplogger/test.php

Then click the ADMIN SECTION link, then click VIEW IPS in the menu. IT will display them with the date aswell. Now what i wanna know is how would i get it to display the numbers to the left of it?

[EXAMPLE]

1 November 2nd 2003 123.456.7.8
2 November 2nd 2003 123.456.7.8
3 November 2nd 2003 123.456.7.8
4 November 3rd 2003 123.456.7.8
5 November 3rd 2003 123.456.7.8
6 November 3rd 2003 123.456.7.8
7 November 4th 2003 123.456.7.8

[/EXAMPLE]
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The code you're using for display is...?

Mac
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Woops forgot, sorry :)

Code thats loggin and sending to logs.txt

Code: Select all

<? 

$iplog="ips.txt";
$TestLog=fopen($iplog,"r");
fclose($TestLog);
$Date=date("F j, Y");
$IP=$HTTP_SERVER_VARS["REMOTE_ADDR"];
$Logging="<tr valign="top">
<td width="50%"><font face="Verdana" size="1">$Date</font></td>
<td width="50%"><font face="Verdana" size="1">$IP</font></td>
</tr>";
$Post = fopen("$iplog","a");
        fputs($Post,$Logging);
        fclose($Post);
?>
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Give it a try:

Code: Select all

<?php
$iplog="ips.txt";
$TestLog=fopen($iplog,"r");
fclose($TestLog);
$nextLine = count(file($iplog)) + 1;
$Date=date("F j, Y");
$IP=$HTTP_SERVER_VARS["REMOTE_ADDR"];
$Logging="<tr valign="top"><td width="10%"><font face="Verdana" size="1">$nextLine</font></td><td width="40%"><font face="Verdana" size="1">$Date</font></td><td width="50%"><font face="Verdana" size="1">$IP</font></td></tr>\n";
$Post = fopen("$iplog","a");
fputs($Post,$Logging);
fclose($Post);
?>
See more info on [php_man]file[/php_man] and count

Tis may cause your server to speed dow due to the possibility of the ips.txt file becomes very very large. So keep cleaning it from time to time... Or implement a counter in a separate file to keep track of the access number.

Hope that helps. Tchau,
Scorphus.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

I'll also probably add a delete button which deletes all the content in the txt file.

(How would i do that?)
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

You could write a dellogs.php:

Code: Select all

<?php
/* This script creates a backup copy of ips.txt and creates a new empty ips.txt
 * It deletes a backup file ips.txt.bak if it exists
 * It copy the current ips.txt to a new ips.txt.bak
 * And then creates a new ips.txt
 */

$iplog="ips.txt";

// Checks if ips.txt.bak exists and attempts to delete it

if (file_exists($iplog.'.bak') && !unlink($iplog.'.bak')) {
	print ("failed to delete $iplog.bak...<br>\n");
}

// Attempts to copy ips.txt to ips.txt.bak
if (!copy($iplog, $iplog.'.bak')) {
	print ("failed to copy $iplog to $iplog.bak...<br>\n");
}

// Attempts to create a new ips.txt
$TestLog = @fopen($iplog, "w");
if (!$TestLog) {
	print ("failed to create new $iplog...<br>\n");
}
fclose($TestLog);
?>
and make your delete button (<a href="dellogs.php"><img src="button.jpg"></a>) to do the job.

Hope that helps your understanding!

Cheers,
Scorphus.
Post Reply