2 回答
![?](http://img1.sycdn.imooc.com/54584e120001811202200220-100-100.jpg)
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;
?>
编辑:
我不建议您像这样使用位于服务器中的文件,因为用户可以访问这些文件,并且它会损害您正在做的任何事情。请改用数据库。如果您一定要使用这些文件,那么不要存储未加密的数据,而是使用一些加密方法来加密数据,这样至少一个普通的非技术用户不会知道它是什么。
![?](http://img1.sycdn.imooc.com/5458632800010f8802200220-100-100.jpg)
TA贡献1824条经验 获得超8个赞
您可以将访问者的 IP 写入服务器上的本地文件中。如果有人访问您的站点,请以数组形式完整读取此文件并检查array_key_exists
该 IP 是否在您的列表中(与!== -1
存在的情况进行比较),或者逐行读取并检查它们是否相等。
如果 IP 是两倍,那么您可以重定向到另一个站点,也许类似"Upps, your here again. I only like new vistors"
,或者您只需将以下整个代码放在if 块中,这样就不会对其进行求值。
添加回答
举报