为了账号安全,请及时绑定邮箱和手机立即绑定

如何屏蔽两次进入我网站的IP?

如何屏蔽两次进入我网站的IP?

慕的地10843 2023-07-18 13:49:42
我有这个代码:$accounts = fopen("accounts.txt", "r+");$give_account = fgets($accounts);$fileContents = file_get_contents("ips.txt");fclose($accounts);    if(strpos($fileContents, $ip) !== false){        echo $give_account;        }else{        echo "you have already received an account";    }我想得到他的IP(我明白了这一点),我希望你收到你的帐户,我想从列表中删除我提供的帐户,并且无法接收另一个帐户并打印其他帐户。如果你可以帮我做点什么,哪怕是一点点,我都会很感激。谢谢
查看完整描述

2 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

想象一下您的网站中没有任何类型的数据库使用,此代码应该让您快速了解如何处理您的场景。


提示:尽量不要像解析字符串一样解析文件。相反,请使用 JSON 或 XML 或某种标记语言。


<?php

if(!function_exists('get_client_ip')){

    function get_client_ip() {

        $ipaddress = '';

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

            $ipaddress = $_SERVER['HTTP_CLIENT_IP'];

        else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))

            $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];

        else if(isset($_SERVER['HTTP_X_FORWARDED']))

            $ipaddress = $_SERVER['HTTP_X_FORWARDED'];

        else if(isset($_SERVER['HTTP_FORWARDED_FOR']))

            $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];

        else if(isset($_SERVER['HTTP_FORWARDED']))

            $ipaddress = $_SERVER['HTTP_FORWARDED'];

        else if(isset($_SERVER['REMOTE_ADDR']))

            $ipaddress = $_SERVER['REMOTE_ADDR'];

        else

            $ipaddress = null;

        return $ipaddress;

    }

}


if(!function_exists('log_ip_address')){

    function log_ip_address($ip, $account_id){

        // Get the JSON object from the IP Logs file and add this IP to the object

        // $ip_logged is true if JSON object is updated. false otherwise

        $ip_logged = true;


        return $ip_logged ? true : false;

    }

}


if(!function_exists('allot_account_to_client')){

    function allot_account_to_client($account_id, $ip){

        if(empty($account_id) || empty($ip)){

            throw new Exception("Parameter missing");

        }

        // Do your magic here and allot an account to your client if he deserves

        $alloted = true;


        // When alloted, remove the current $account_id from the accounts JSON objects so that you won't allot the same account_id to anyone else


        // Log the IP address

        $log = log_ip_address($ip, $account_id);


        // return true only if ip is logged. If IP logging fails, write a mechanism to put back the account_id back to the accounts Array.

        return $log ? true : false;

    }

}


$ip = get_client_ip();

if($ip == null){

    throw new Exception("Unable to get IP. Do you even exist?");

}


// contains an JSON object of IP addresses that an account was taken from and it's details

$list_of_ips = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'].'/logs/ips.json'));


// Contains JSON Array of account_ids

$accounts = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'].'/accounts/available_accounts.json'));


if(isset($list_of_ips[$ip])){

    $taken_on = new DateTime($list_of_ips[$ip]['taken_on']);

    throw new Exception("You already got your account on ".$taken_on->format('m-d-Y H:i:sP'));

}



$available_account_id = $accounts[0];


// If he comes until here, he deserves the account

$alloted = allot_account_to_client($account_id, $ip);


if(!$alloted){

    echo "Unable to allot you an account.";

    exit(0);

}


echo "Your account id is : ".$account_id;

?>


编辑:


我不建议您像这样使用位于服务器中的文件,因为用户可以访问这些文件,并且它会损害您正在做的任何事情。请改用数据库。如果您一定要使用这些文件,那么不要存储未加密的数据,而是使用一些加密方法来加密数据,这样至少一个普通的非技术用户不会知道它是什么。


查看完整回答
反对 回复 2023-07-18
?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

您可以将访问者的 IP 写入服务器上的本地文件中。如果有人访问您的站点,请以数组形式完整读取此文件并检查array_key_exists该 IP 是否在您的列表中(与!== -1存在的情况进行比较),或者逐行读取并检查它们是否相等。

如果 IP 是两倍,那么您可以重定向另一个站点,也许类似"Upps, your here again. I only like new vistors" ,或者您只需将以下整个代码放在if 块中,这样就不会对其进行求值。


查看完整回答
反对 回复 2023-07-18
  • 2 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信