Text database record IP, if the IP is not added, no record to the text data, debug total error, help master
Document One:
$now _time=time ();//Current time
$ipfile = "./images/ip.txt";//Text data storage file
$old =file ($ipfile);
$num =count ($old);
for ($i =0; $i < $num; $i + +) {
List ($txtusre, $txtzzip, $txttime) =explode ("|", $old [$i]);
if ($txtzzip = = "$REMOTE _addr") {//compares the presence of guest IP text data
Skipped without recording already exists
}else{
Not exist on the record
if (GET_MAGIC_QUOTES_GPC ()) {
if (! $fp = fopen ($ipfile, "A +")) {
Die ("Failed to create file, please check for permissions.");
}
$line = $now _time. "|". $REMOTE _addr. "|". $now _time. " \ r \ n ";
$old =file ($ipfile);
$num =count ($old);
$FP =fopen ($ipfile, "w");
Flock ($FP, 2);
if ($num <100) {//record only 100, more than delete the last 100 lines before the record
Fputs ($fp, $line);
for ($i =0; $i < $num; $i + +) {
Fputs ($fp, $old [$i]);
}}else{
Fputs ($fp, $line);
for ($i =0; $i <99; $i + +) {
Fputs ($fp, $old [$i]);
}}
Fclose ($FP);
}}}
?>
Text Data file Ip.txt:
1348308290|127.0.0.1|1348308290
1348308289|127.0.0.1|1348308289
1348308275|127.0.0.2|1348308275
When there is only one record, you can tell that the IP already exists, and continue to add the non-existent IP
When there are two records, determine that an existing IP is invalid, refresh once to record a duplicate IP
Want to effect is: When the guest's IP data file does not exist in the add a, if present, do not add
Please help to amend the master, thank you
------Solution--------------------
$REMOTE _addr The value of this variable requires your own access.
$now _time=time ();//This line can be erased.
------Solution--------------------
PHP code
$ip = $_server[' remote_addr ']; $time = time (); $ipfile = "./images/ip.txt"; $s = file_get_contents ($ipfile); if (Strstr ($s, "| $ip |")) return; exists on the end $ar = Preg_split ("/[\r\n]+/", $s); Cutting array $ar = Array_slice ($ar, 0, 99); Top 99 items $ar[] = "$time | $ip | $time"; Add New Data file_put_contents ($ipfile, join (Php_eol, $ar)); Write back file