Posts: 34
Threads: 17
Joined: Oct 2009
Reputation:
0
What's Up?
OK, So New to PHP, And have a quick and hopefully easy question.
Basically when the same user comes to me PHP ip logger script their IP gets logged, But if they come twice it makes a whole new line, So i set about figuring out how to clean up multiple entry's. Heres my source.
Code: <?php
$logfile= 'make1.html';
$IP = $_SERVER['REMOTE_ADDR'];
$logdetails= date("F j, Y, g:i a") . ': ' . '<a href=http://dnsstuff.com/tools/city.ch?ip='.$_SERVER['REMOTE_ADDR'].'>'.$_SERVER['REMOTE_ADDR'].'</a>';
$fp = fopen($logfile, "a");
fwrite($fp, $logdetails);
fwrite($fp, "<br>");
fclose($fp);
$data1 = file("make1.html");
$data2 = file("make2.html");
file_put_contents('Database.html', implode('', array_unique(array_merge($data1,$data2))));
unlink('make1.html');
unlink('make2.html');
$ourFileName = "make1.html";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
$ourFileName = "make2.html";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
?>
Now that works fine, When a the same person comes it just updates the time, But, When a new IP comes it clears the last IP and puta the new one their. Example.
So the IP 127.0.0.1 gets logged first. Code: December 12, 2009, 2:55 pm: 127.0.0.1
And when it comes again
Code: December 12, 2009, 3:00 pm: 127.0.0.1
So every things fine, When the new IP comes though this happens.
Code: December 12, 2009, 3:02 pm: 74.63.89.20
What happened the the last IP's entry?
Anyone Know?
Thanks Bye.
Posts: 264
Threads: 48
Joined: Oct 2009
Reputation:
1
Did you write this script yourself?
Posts: 34
Threads: 17
Joined: Oct 2009
Reputation:
0
(12-12-2009, 01:56 PM)Socrates Wrote: Did you write this script yourself?
Can't you tell? That's why it doesn't work, Joke joke, Anyway part of it yes. You dont happen to know why it isn't working do you?
Posts: 1,351
Threads: 111
Joined: Oct 2009
Reputation:
40
12-12-2009, 02:14 PM
(This post was last modified: 12-12-2009, 02:17 PM by Gaijin.)
It is because your using
Try out this
What you doing wrong is, you open a file for writing only "w", using the letter a == "append" will add content to the open handle.
Posts: 34
Threads: 17
Joined: Oct 2009
Reputation:
0
Quote:What you doing wrong is, you open a file for writing only "w", using the letter a == "append" will add content to the open handle.
So it should be.
PHP Code: <?php $logfile= 'make2.html'; $IP = $_SERVER['REMOTE_ADDR']; $logdetails= date("F j, Y, g:i a") . ': ' . '<a href=http://dnsstuff.com/tools/city.ch?ip='.$_SERVER['REMOTE_ADDR'].'>'.$_SERVER['REMOTE_ADDR'].'</a>'; $fp = fopen($logfile, "a"); fwrite($fp, $logdetails); fwrite($fp, "<br>"); fclose($fp); $data1 = file("make1.html"); $data2 = file("make2.html"); file_put_contents('Database.html', implode('', array_unique(array_merge($data1,$data2)))); unlink('make1.html'); unlink('make2.html'); $ourFileName = "make1.html"; $ourFileHandle = fopen($ourFileName, 'a') or die("can't open file"); fclose($ourFileHandle); $ourFileName = "make2.html"; $ourFileHandle = fopen($ourFileName, 'a') or die("can't open file"); fclose($ourFileHandle); ?>
If so then its still having the same problem.
Posts: 1,351
Threads: 111
Joined: Oct 2009
Reputation:
40
12-12-2009, 02:33 PM
(This post was last modified: 12-12-2009, 02:34 PM by Gaijin.)
It doesn't work because you're creating a new file db.htm make 1 & 2 every time an user visits your site.
You use file_put_contents wrong, try it this way.
PHP Code: file_put_contents('Database.html', implode('', array_unique(array_merge($data1,$data2))), FILE_APPEND);
http://www.php.net/manual/en/function.fi...ntents.php
edit: for a new line use \n inside of double qoutes.
Posts: 34
Threads: 17
Joined: Oct 2009
Reputation:
0
12-13-2009, 07:58 AM
(This post was last modified: 12-13-2009, 08:06 AM by Scorpion.)
I said it worked before I actually fully tested it lol, Anyway It now appends the two new IP's to the one file (Database.html) But it no longer overwrites if its the same IP.
Code: December 13, 2009, 9:59 am 192.145.643.222
Code: December 13, 2009, 10:00 am 192.145.643.222
Urg!
Code: <?php
$logfile= 'make1.html';
$IP = $_SERVER['REMOTE_ADDR'];
$logdetails= date("F j, Y, g:i a") . ': ' . '<a href=http://dnsstuff.com/tools/city.ch?ip='.$_SERVER['REMOTE_ADDR'].'>'.$_SERVER['REMOTE_ADDR'].'</a>';
$fp = fopen($logfile, "a");
fwrite($fp, $logdetails);
fwrite($fp, "<br>");
fclose($fp);
$data1 = file("make1.html");
$data2 = file("make2.html");
file_put_contents('Database.html', implode('', array_unique(array_merge($data1,$data2))), FILE_APPEND);
unlink('make1.html');
unlink('make2.html');
$ourFileName = "make1.html";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
$ourFileName = "make2.html";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
?>
Posts: 1,351
Threads: 111
Joined: Oct 2009
Reputation:
40
The code you firstly posted never did overwrited the same IP, but was writing a new log.
What you now need to do is to read the lines to an array first and then use preg_match(_all) to check if there is a same IP and then replace that.
http://www.php.net/manual/en/function.preg-match.php
Posts: 34
Threads: 17
Joined: Oct 2009
Reputation:
0
I decided to go another way
PHP Code: <?php $ip = $_SERVER['REMOTE_ADDR']; $pagina = $_SERVER['REQUEST_URI']; $datum = date("d-m-y / h:a"); $invoegen = $datum . " - " . $ip . " - " . $pagina . " "; $fopen = fopen("Database.html", "a"); fwrite($fopen, $invoegen); fclose($fopen); $filename = 'Database.html'; $text = array_unique(file($filename)); $f = @fopen($filename,'w+'); if ($f) { fputs($f, join('',$text)); fclose($f); } ?>
I had to sacrifice the time of the IP visit but I'm not too bothered about that. Thanks for your help.
Posts: 1,351
Threads: 111
Joined: Oct 2009
Reputation:
40
(12-13-2009, 10:46 AM)Scorpion Wrote: PHP Code: $invoegen = $datum . " - " . $ip . " - " . $pagina . " ";
May I suggest you a change in the above line.
PHP Code: $invoegen = "$datum - $ip - $pagina \n";
|