PHP写入文本文件和C语言非常类似,用的函数也一致,读取文件用fopen,写入文件用fwrite,结束用fclose。要注意的是打开并写入新的文件用"w"标识,读取文件用"r"标识,在原有文件上继续写入用"a"标识
,同时windows换行符是"rn",linux换行符是"n"
PHP记录IP的函数是 $_SERVER["REMOTE_ADDR"]
PHP获取当前时间函数date("Y-m-d H:i:s", time())
PHP获取来路网址函数$_SERVER['HTTP_REFERER'] 首先要判断其是否为空值,即用isset进行判断,比如我们直接从浏览器敲入网址则不会有来路网址记录,最后代码很简单,如下:
代码如下 | 复制代码 |
$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); |
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