我有一个API,它将url释放到我的网络上的重定向页面。网址看起来是这样的:
http://mydomain.com/offers.php?country=US&offerID=5555还有一些其他的参数,但这就是其中的一部分。问题是,用户正在滥用它,并创建自己的点击与机器人。我想要创建一个密码系统,它强制用户使用API中的链接,这样就不会被滥用。
就像这样:
api.php
<?php
$random_number=mt_rand(1,100);
$url="http://mydomain.com/offers.php?country=US&offerID=5555&key=".$random_number;
echo $url;
?>offers.php
<?php
$key=$_GET['key'];
if($key<1 || $key>100)
die("Invalid key!");
?>这显然是一个非常愚蠢的解决方案,因为它只是使用一个随机的number...but,希望您能看到我想要实现的目标。
我的想法是按小时加密日期字符串,并使用静态隐藏密钥对其进行加密。这将使关键改变每小时,并将更加困难的欺诈者。唯一的问题是,如果有人在9:59从API中获得url,它将在10:00过期,这太短了。
做这件事最好的方法是什么?理想的钥匙应该是..。
你有什么建议?
发布于 2013-03-13 17:35:41
存储当前用户会话的一次使用散列如何?
发布于 2020-04-06 19:29:36
我就是这么做的。我并不推荐这是一种安全或高效的解决方案,只是简单地防止用户篡改我的urls。
public static function hashArray($arr, $timestamp)
{
$salt = "my-salt-here";
$md5 = md5(http_build_query($arr) . $salt . $timestamp);
return $md5;
}
//build the url
$timestamp = date("Y-m-d H"); //links will last for about two hours
//if you want single-use links, you can add a random number to params
$params = ['offer'=>10, 'country'=>'US'];
$params['hash']=hashArray($params, $timestamp); //build hash and add it
$url = 'http://my-domain.com/click.php?'.http_build_query($params);
//check the hash
public static function checkHash($params, $hash)
{
//allow current or previous hour
return $hash === hashArray($params, date('Y-m-d H'))
|| $hash === hashArray($params, date('Y-m-d H', time()-3600))
}
$params=$_GET;
$hash = isset($params['hash']) ? $params['hash'] : '';
unset($params['hash']);
checkHash($params, $hash);https://stackoverflow.com/questions/15391709
复制相似问题