PHP writes a text file very similar to the C language, and uses the same function. fopen is used to read the file, fwrite is used to write the file, and fclose is used to end. Note that the "w" mark is used to open and write new files, and the "r" mark is used to read files, and the "a" mark is used to continue writing existing files.
In addition, the windows line break is "rn" and the linux line break is "n"
The php ip record function is $ _ SERVER ["REMOTE_ADDR"]
PHP obtains the current time function date ("Y-m-d H: I: s", time ())
PHP functions for obtaining the Origin Site $ _ SERVER ['HTTP _ referer'] must first determine whether it is null, that is, use isset to determine, for example, if we type a URL directly from the browser, there will be no URL record. The final code is very simple, as shown below:
| The Code is as follows: |
Copy code |
|
$ Ip = $ _ SERVER ["REMOTE_ADDR"];
$ Time = date ("Y-m-d H: I: s", time ());
$ From = '';
If (isset ($ _ SERVER ['HTTP _ referer'])
{
$ From = $ _ SERVER ['HTTP _ referer'];
}
$ Myfile = 'wtndata/statistic.txt ';
$ Str = "ip = '". $ ip. "' from = '". $ from. "'Time ='". $ time. "'rn ";
$ File_pointer = fopen ($ myfile, "");
Fwrite ($ file_pointer, $ str );
Fclose ($ file_pointer ); |