假设我有以下代码:
<?php
$html = '
<form id="formA">
<!-- Form for user another than admin goes here /--> ';
if ($userrole == 'admin') {
$html .= ' <!-- Form for admin goes here /--> ';
}
$html .= '
<input type="submit" value="Submit" />
</form> ';
print $html;
?>
我认为在我们的网页上实现隐藏接口是不好的,尤其是当您尝试在其上使用$_GET['']
时$userrole
,因为攻击者可能会尝试滥用 URL 上的查询字符串来查看页面上是否有任何调试模板。
我知道这对开发人员来说是一个糟糕的实现,但我不知道如何安全地做这些事情。有任何想法吗?谢谢。
更新 1
当我尝试将userrole
信息保存在 a 上_SESSION
并且在页面上具有与我上面提到的相同的检查部分时是否相同,这意味着页面上仍然有隐藏的接口?
更新 2
假设我有这两个表:
tbl用户
UserID UserName UserRole
101 Abc 1
102 Bcd 1
103 Cde 2
tbl角色
UserRole RoleName V A U D
1 Admin 1 1 1 1
2 User 1 1 0 0
注:V、A、U、D 代表VIEW
、ADD
、UPDATE
和DELETE
的授权。
此代码用于检查用户对页面上查看、添加、更新或删除内容的授权(似乎与上面提到的第一个问题不同)。
然后,假设自第一次登录以来我已经保存了会话(我省略了登录和存储会话的代码 -UserID
和UserRole
)。我下面要做的是,检查每个授权并检查使用if
条件。
$V = checkauth($_SESSION['UserRole'], 1);
$A = checkauth($_SESSION['UserRole'], 2);
$U = checkauth($_SESSION['UserRole'], 3);
$D = checkauth($_SESSION['UserRole'], 4);
if($V == 1) { //Do something here }
if($A == 1) { //Do something here }
if($U == 1) { //Do something here }
if($D == 1) { //Do something here }
function checkauth($userRole, $mode) {
$q = " SELECT V, A, U, D FROM tblRole WHERE UserRole = '" . $userRole . "' ";
//I omit the connection code since it's not important here (not the topic)
$fetch = mysqli_fetch_array($r);
switch($mode) {
case '1': return $fetch[0]; break;
case '2': return $fetch[1]; break;
case '3': return $fetch[2]; break;
case '4': return $fetch[3]; break;
}
}
代码在网页上实现是否相当安全?还是有比这更好的东西?我敢肯定有... 需要你的想法,伙计们!:)