繁体   English   中英

编码URL $ _GET变量

[英]Encoding URL $_GET variables

我正在传递$ _GET []; 链接中的变量,但希望对网址中的值进行加密。 我通常会使用会话,但是由于我没有使用表单,而只是使用URL,因此我有什么选择。 我确信有某种方法可以利用此方法,因此我正在尝试使其尽可能安全。

下面是我的代码:

$cost = "152.00";
<a href="registration.php?class=Spanish&age=4-6&time=3pm&cost=<?echo $cost; ?>">

和registration.php看起来像

<?
$class = $_GET[class];
$age = $_GET[age];
$time = $_GET[time];
$cost = $_GET[cost];
?>

如果我可以使用类似md5的值来编码值,然后在registration.php上对其进行解码,那将是不错的选择,或者是否可以使用<a>传递$ _session变量也可以。

您要链接到的页面是否与您从中链接的网站位于同一网站上?

如果是这样,为什么不将它们放在$_SESSION呢? 这样,它们就存储在服务器上,用户永远不会想到要对其进行操作。

另一个选择实际上不是加密数据, 而是使用HMAC对其进行签名 如果您使用的是现代版本的PHP,则可以使用hash_hmac 在链接创建方面:

$validate = serialize(array( $class, $age, $time, $cost ));
$hmac = hash_hmac('sha1', $validate, 'secret key goes here');
$link = 'foo.php?...&validate=' . $hmac;

...而另一方面:

$validate = serialize(array( $_GET['class'], $_GET['age'], $_GET['time'], $_GET['cost'] ));
$hmac = hash_hmac('sha1', $validate, 'secret key goes here');
if($hmac != $_GET['validate']) {
    echo "No hacking!";
    exit;
}

您实际上应该对选项#3使用此技术,即: 使用POST ,即表单。 这将阻止临时用户更改数据。

选项#4最好: 将所有敏感数据存储在服务器端,以开头 ,并通过用户看到的唯一标识符对其进行引用。 这样一来,数据就不会出现在用户手中,这意味着您无需担心用户会对其进行篡改。

您可以考虑创建一个由您不想操纵的变量组成的“哈希键”:

(下面进行了编辑以使内容更清楚)

在第一页(例如class.php)中,生成链接,如下所示:

$paramStr = "class=Spanish&age=4-6&time=3pm&cost=$cost";
$globalSecretKey = "Some secret key string that you don't reveal to the user";
$publicKey = md5($globalSecretKey . $paramStr);
$link = "registration.php?$paramStr&hash=$publicKey";

然后在registration.php上,确保哈希值与提交的变量匹配:

$submittedParamStr = "class=" . $_GET['class'] . "&age=" . $_GET['age'] . "&time=" . $_GET['time'] . "&cost=" . $_GET['cost'];
$globalSecretKey = "Some secret key string that you don't reveal to the user";
$submittedDataKey = md5($globalSecretKey . $submittedParamStr);

// checking if submitted key matches submittedDataKey
if($_GET['hash'] != $submittedDataKey) die("Problem!");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM