Home > php教程 > php手册 > body text

PHP获取IP 文件写入与行修改

WBOY
Release: 2016-06-06 20:00:16
Original
1591 people have browsed it

·做了一个投票放在微信订阅号上,通过cookie来实现禁止刷票,这个真是只能防君子不能防小人,出现了恶意刷票;所以最后改为用IP来限制; 因为是一个短暂的活动,就把IP的记录写入了文件,没有再写入表中; 我的验证规则是:同个ID 对应的同一个IP3分钟内只

·做了一个投票放在微信订阅号上,通过cookie来实现禁止刷票,这个真是只能防君子不能防小人,出现了恶意刷票;所以最后改为用IP来限制;

因为是一个短暂的活动,就把IP的记录写入了文件,没有再写入表中;

我的验证规则是:同个ID 对应的同一个IP3分钟内只能投一票;

代码写的不是很好,急着上交 ,先看着吧 ,以后再完善

function GetIP() {
    if ($_SERVER["HTTP_X_FORWARDED_FOR"])
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    else if ($_SERVER["HTTP_CLIENT_IP"])
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    else if ($_SERVER["REMOTE_ADDR"])
        $ip = $_SERVER["REMOTE_ADDR"];
    else if (getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if (getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if (getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else
        $ip = "Unknown";
    return $ip;
}
$iptxt = fopen("./data/lovewall.txt", "a+");
	$ip = GetIP();
	$ifok = $i = 1;
	while (!feof($iptxt)) { 
		$now = fgets($iptxt);//获取遍历行的内容
		if(!empty($now)){
			$now_arr = explode('#',$now);
			if($now_arr[1]==$ip&&$now_arr[2]==$_G['gp_show']."\n"){
				$ifok = 0;
				if($now_arr['0']+180>time()){
					$return = array('status'=>0,'err_msg'=>'一封情书三分钟内只限赠送一次哦');
					echo json_encode($return);
					exit;
				}else{
					$all = file_get_contents('./data/lovewall.txt');
					$new = time().'#'.$ip.'#'.$_G['gp_show']."\n";
					$update_str = str_replace($now,$new, $all);//替换 间接实现修改
					file_put_contents('./data/lovewall.txt', $update_str);
				}
			}
		}
		$i++; 
	}
	if($ifok){
		$now = time()."#".$ip."#".$_G['gp_show'];
		fwrite($iptxt,$now."\n");
	}
	fclose($iptxt);
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template