Home  >  Article  >  Backend Development  >  PHP code to write text file txt_PHP tutorial

PHP code to write text file txt_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:10:141118browse

Writing text files in PHP is very similar to the C language, and the functions used are also the same. fopen is used to read files, fwrite is used to write files, and fclose is used to end. It should be noted that opening and writing a new file is marked with "w", reading a file is marked with "r", continuing to write on the original file is marked with "a"

, and the windows newline character is "rn" , the Linux newline character is "n"
The function for PHP to record IP is $_SERVER["REMOTE_ADDR"]

PHP gets the current time function date("Y-m-d H:i:s", time())

PHP function to get the source URL $_SERVER['HTTP_REFERER'] must first determine whether it is a null value, that is, use isset to judge. For example, if we type the URL directly from the browser, there will be no source URL. Record, the final code is very simple, as follows:

The code is as follows
 代码如下 复制代码

$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,"a");
fwrite($file_pointer,$str);
fclose($file_pointer);

Copy code
$ip = $_SERVER["REMOTE_ADDR"];
$time = date("Y-m-d H:i:s", time());$from = '';

if(isset($_SERVER['HTTP_REFERER']))

{}$ myfile = 'wtndata/statistic.txt';$str = "ip='".$ip."' from='".$from."' time='".$time."'rn"; $file_pointer = fopen($myfile,"a");fwrite($file_pointer,$str);fclose($file_pointer);
http://www.bkjia.com/PHPjc/444738.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444738.htmlTechArticlePHP writing text files is very similar to C language, and the functions used are also the same. Use fopen to read files and write Use fwrite to enter the file and fclose to end it. Note that to open and write new files use "...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn