PHP:安全的用户身份验证?

问题描述 投票:0回答:1

下面的代码检查管理员是否已登录并在网站上显示特殊编辑框。为此,将在整个脚本中使用$ show_tools。

 if (isset($user)){
        if($user->logincheck($_SESSION["loggedin"], "users", "user_password", "user_email")){
            $show_tools = true;
        }else{
            $show_tools = false;
        }
    }

以后使用$ show_tools是否安全?例如:

<?php
  if ($show_tools){
    ?>
    <h1> Hello, administrator! </h1>
  <?php
  }
?>
php security login
1个回答
0
投票

使用原始$show_tools缺乏封装。每个人都可以覆盖它,即使你是错误的,也没有提到在程序中注入代码的恶意黑客。此外,随着程序的增长,您必须将其全局化。考虑以下方法:

function show_tools($flag = null) {
    static $value = false;
    if (is_bool($flag)) {
        // you can run other checks here too
        $value = $flag;
    }
    return $value;
}

用法:

// authenticate
show_tools(true);

if (show_tools()) { // if authenticated
    // show the tools
}

// deauthenticate
show_tools(false);

函数意味着不可覆盖,因此没有人可以覆盖函数并改变你不希望被改变的东西。通过这种方法,您可以安全无虞。没有它,任何事情都可能发生:

<?php
$show_tools = true;
include("your_insecure_script.php");
// Cool! I can see special editing boxes!
?>
© www.soinside.com 2019 - 2024. All rights reserved.