加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

通过get,php在url和security中传输数据

发布时间:2020-12-13 21:48:53 所属栏目:PHP教程 来源:网络整理
导读:这是第一个问题,我需要你的帮助. 我使用php中的头位置方法将表单数据从第一页传输到第二页. 在第二页,我使用get接受数据. 现在这里是第二页的网址,在数据发送后(即提交表格) http://mydomain.com/site1/form1_conf.php?id=123 当用户在第二页时,第二页上的数
这是第一个问题,我需要你的帮助.

我使用php中的头位置方法将表单数据从第一页传输到第二页.

在第二页,我使用get接受数据.

现在这里是第二页的网址,在数据发送后(即提交表格)

http://mydomain.com/site1/form1_conf.php?id=123

当用户在第二页时,第二页上的数据将根据mysql数据库中的id号显示.

现在的问题是,当用户在第二页上并且他改变了数字(例如123,比如78)时,数据库中显示id = 78的数据,这是不好的.

我怎么能阻止它?

请注意:我不能使用帖子,也不能使用会话.

EDITE:

第一页上的PHP代码,转移到第二页:

// after all validations are okay
$insert = //insert into database
$result = mysql_query($insert);
if($result)
{
echo("<br>Input data is succeed");
$lastInsertedId =  mysql_insert_id();
header('Location:form1_conf.php?id='.$lastInsertedId); //THIS IS THE IMPORTANT LINE
}
else
{
$message = "The data cannot be inserted.";
$message .= "<br />" . mysql_error();
}

解决方法

您的问题不在于URL:高级用户更改cookie或POST变量与为常规用户编辑GET变量一样简单.您需要某种方式将请求“签名”为有效.

最简单的方法是使用“预共享密钥”,您可以使用单向哈希来验证请求.

重定向:

$newURL = '/newpage?id='.$id.'&hash='.sha1('mypresharedkey'.$id);
header('HTTP/1.1 303 See other');
header('Location: '.$newURL);
die;

另一页:

$idToShow = $_GET['id'];
$hash = sha1('mypresharedkey'.$id);
if($hash != $_GET['hash'])
  die("Tssss,don't play with the address bar!");
else
  RenderThePage();

这可确保最终用户只能访问提交允许的页面.

对于您的特定代码:

...all prior code
$lastInsertedId = mysql_insert_id();
$timestamp = time();
header('Location:form1_conf.php?'.http_build_query([
      'id' => $lastInsertedId,'time' => $timestamp,'hash' => sha1('some-generated-key'.$timestamp.$lastInsertedId)
]);

在另一页中,如果你想要包括一个定时炸弹(否则只是将其注释掉):

$id = $_GET['id'];
$time = $_GET['time'];
if($_GET['hash'] != sha1('some-generated-key'.$time.$id))
  die('URL was tampered with');
if(time() - $time > 300)
  die('URL was only valid for 5 minutes');

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读