GPT答: |
7i24.cn
|
<?php
//因IP地址获取函数为php5.2.0版本以上支持,故此处使用php5.2.4,并且载入文件时直接使用if判断
if (version_compare(PHP_VERSION, '5.2.4', '<')) {
die('需要PHP 5.2.4版本以上,当前版本为'.PHP_VERSION);
}
//获取访问IP
$ip = getenv('REMOTE_ADDR') ? getenv('REMOTE_ADDR') : getenv('HTTP_CLIENT_IP');
//初始化访问次数
$count = 0;
//判断文件是否存在,若存在,读取文件
if (file_exists('data.txt')) {
$arr = file_get_contents('data.txt');
//字符串拆分为 数字+$ip形式,利用explode函数进行数组类型转化,并判断当前ip是否被访问过
$countarr = explode('$',$arr);
if ($countarr[1] == $ip){
//$countarr[0] 为访问次数,转化为int类型
$count = (int)$countarr[0];
}
}
//文件存在,若IP未被记录,直接加上未记录IP
if ($count == 0){
$con = "1$ip";
file_put_contents('data.txt',$con);
}
//若IP未被记录,将文件内容准备为空,无$ip,加上记录IP
else {
$con = ($count+1)."$ip";
file_put_contents('data.txt',$con);
}
//显示访问次数
echo "访问次数:".$count++;
?>
|