The text database records the IP, if the IP already exists does not add, does not record in the text data, the debugging total error, the master

Source: Internet
Author: User
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 
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.