开发者

how to remove Duplicate IP in PHP

I wrote the code that save the ip of client 开发者_如何学JAVAinto logs.txt but i want to remove the additional ip from the logs.txt . what am i going to do ?

$at = $_SERVER['REMOTE_ADDR'];
$log = fopen("logs.txt", "a"); 
fwrite($log, $at ."\n"); 
fclose($log);   

Thanks in advance .


try following command

sort file | uniq > file.new


I would go with Salman's way because it works both in Windows or Linux boxes.

However, consider using a database such as SQLite that saves all the data in a single file. So, you will be able to query your data in a more flexible way.


Method #1

$at = $_SERVER['REMOTE_ADDR'];
$log = file_get_contents("logs.txt");
$log = trim($log); // removes leading/trailing blank lines
$log = explode("\n", $log);
$log[] = $at;
$log = array_unique($log);
$log = implode("\n", $log);
file_put_contents("logs.txt", $log);

Method #2

$at = $_SERVER['REMOTE_ADDR'];
$log = file_get_contents("logs.txt");
$temp = explode("\n", $log);
if(in_array($at, $temp) == false) {
    file_put_contents("logs.txt", $log . $at . "\n");
}


Using linux:

sort logs.txt | uniq

Btw usually the ip will be logged in the webservers access log.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜