Support Forums

Full Version: array_unique
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
Did you write this script yourself?
(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?
It is because your using
PHP Code:
fopen($ourFileName'w'

Try out this
PHP Code:
fopen($ourFileName'a'

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.
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.
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.
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);
?>
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
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($fjoin('',$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.
(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"