Jump to content

array_unique


php-n00b

Recommended Posts

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.

<?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.

December 12, 2009, 2:55 pm: 127.0.0.1

And when it comes again

December 12, 2009, 3:00 pm: 127.0.0.1

So everythings fine, When the new IP comes though this happens.

December 12, 2009, 3:02 pm: 74.63.89.20

What happened the the last IP's entry?

 

Anyone Know?

 

Thanks Bye.

Link to comment
https://forums.phpfreaks.com/topic/184908-array_unique/
Share on other sites

Just curious. What does make2 do? You unlink it then you recreate it with an empty fopen(you open and close but dont write to them).  But you never write to it.

 

To empty and remake a file:

unlink($file);

touch($file);

chmod("$file", 0644);// if you get a permission problem then 755

 

 

So as far as I can see you put your $logdetails to the database.html file. file_put_contents does not append unless you tell it to:

file_put_contents('Database.html', implode('', array_unique(array_merge($data1,$data2))), FILE_APPEND | LOCK_EX);

 

So why not this?

 

<?php

$IP = $_SERVER['REMOTE_ADDR'];

 

// make logdetails line with a break and a new line ending

// so it is readable in a browser and as a plain file

$logdetails=  date("F j, Y, g:i a") . ': ' . '<a href=http://dnsstuff.com/tools/city.ch?ip='.$_SERVER['REMOTE_ADDR'].'>'.$_SERVER['REMOTE_ADDR'].'</a><br>\n';

 

// get the contents of database.html into an array

$data1 = file("database.html");

 

//push logdetails to the end of the array

arrary_push($data1,"$logdetails");

 

//unique the array and APPEND it to database.hrml and LOCK it while we write to it so if

//another user accesses the page with the log tickler we dont have an oops

file_put_contents('Database.html', implode('', array_unique), FILE_APPEND | LOCK_EX);

?>

 

HTH

Teamatomic

Link to comment
https://forums.phpfreaks.com/topic/184908-array_unique/#findComment-976357
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.