array_unique - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21) +---- Thread: array_unique (/showthread.php?tid=3555) |
array_unique - Scorpion - 12-12-2009 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 So the IP 127.0.0.1 gets logged first. Code: December 12, 2009, 2:55 pm: 127.0.0.1 Code: December 12, 2009, 3:00 pm: 127.0.0.1 Code: December 12, 2009, 3:02 pm: 74.63.89.20 Anyone Know? Thanks Bye. RE: array_unique - Socrates - 12-12-2009 Did you write this script yourself? RE: array_unique - Scorpion - 12-12-2009 (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? RE: array_unique - Gaijin - 12-12-2009 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. RE: array_unique - Scorpion - 12-12-2009 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 RE: array_unique - Gaijin - 12-12-2009 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); edit: for a new line use \n inside of double qoutes. RE: array_unique - Scorpion - 12-13-2009 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 Code: <?php RE: array_unique - Gaijin - 12-13-2009 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 RE: array_unique - Scorpion - 12-13-2009 I decided to go another way PHP Code: <?php RE: array_unique - Gaijin - 12-13-2009 (12-13-2009, 10:46 AM)Scorpion Wrote: May I suggest you a change in the above line. PHP Code: $invoegen = "$datum - $ip - $pagina \n"; |