The text database records the IP address. if the IP address already exists, it will not be added. if the IP address does not exist, it will be recorded in the text data. The total number of debugging errors is high.

Source: Internet
Author: User
The IP address recorded in the text database. if the IP address already exists, it will not be added. if the IP address does not exist, it will be recorded in the text data. there will be a total of debugging errors. Document 1: & lt ;? Optional & quot; text data storage file & nbsp; $ oldfile ($ ipfile); & nbsp; $ num IP address recorded in the text database. if the IP address already exists, it is not added, if not, it is recorded in the text data. debugging errors always occur.
File 1:

$ 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") {// compare whether the text data of the visitor's IP address already exists
// Skip the record if the record already exists


} Else {

// Record if it does not exist
If (get_magic_quotes_gpc ()){
If (! $ Fp = fopen ($ ipfile, "a + ")){
Die ("An error occurred while creating the file. check whether you have the permission .");
}
$ 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) {// only 100 records are recorded. if it exceeds the limit, the last 100 records are deleted.
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 );
}}}
?>


Upload 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 determine that the IP address already exists and add another IP address that does not exist.

When there are two records, it is deemed that the existing IP address is invalid and record the duplicate IP address once it is refreshed.


The expected result is: when the visitor's IP address data file is not saved, add one. if it exists, do not add


Please help with the correction. thank you.


------ Solution --------------------
$ REMOTE_ADDR the value of this variable needs to be obtained by yourself.
$ Now_time = time (); // you can delete this line.
------ Solution --------------------
PHP code
$ Ip = $ _ SERVER ['remote _ ADDR ']; $ time = time (); $ ipfile = ". /images/ip.txt "; $ s = file_get_contents ($ ipfile); if (strstr ($ s," | $ ip | ") return; // if it exists, end $ ar = preg_split ("/[\ r \ n] +/", $ s); // cut it into an array $ ar = array_slice ($ ar, 0, 99); // obtain the first 99 items $ ar [] = "$ time | $ ip | $ time"; // add new data file_put_contents ($ ipfile, join (PHP_EOL, $ ar); // write back an object

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.