【发布时间】:2013-09-20 01:17:34
【问题描述】:
只有 1 个用户名和 1 个密码才能访问此页面,因此决定将其存储在 PHP 本身中,因为它可能比将其存储在数据库中更安全。但是,我正在尝试掌握会话并希望:
- 它不会以任何方式损害安全性。
- 当其他人尝试登录但失败时,它不会停止会话(用于登录用户)脚本的运行。例如,我不希望它删除登录用户的会话。可能是个愚蠢的问题,但我想我还是会问。
我的index.php文件,在php中看起来像这样:
session_start();
if (!isset($_SESSION['Admin']))
{
if (isset($_POST['username'], $_POST['password']) && $_POST['username'] == 'someUsername' && $_POST['password'] == 'someVeryStrongPassword')
{
$_SESSION['Admin'] = true;
session_write_close();
}
else {
// You don't have access, go back to login page.
session_destroy();
header("location:login.php");
exit();
}
}
// This will now be used for any subsequent, same page requests, such as: `index.php?action={something}`, etc. but will always use `index.php` as the main page.
// Is it now safe to continue on with code assuming that the user is logged in at this point? Should there be anything else to consider adding? Possibly into the $_SESSION[] array?
我的 login.php 文件实际上只是 HTML,看起来像这样:
<!DOCTYPE html>
<html>
<head>
<title>Administration</title>
</head>
<body>
<form name="login" method="POST" action="index.php">
<label for="user">Username: <input id="user" type="text" name="username" /></label><br />
<label for="pass">Password: <input id="pass" type="password" name="password" /></label><br />
<p><input type="submit" value="Login" /></p>
</form>
</body>
</html>
再次,我计划将$sess_id 变量附加到在index.php 文件中提交的所有表单,并对照session_id() 检查它。这足够安全吗?一直在看session_regenerate_id 和session_id。人们报告说session_regenerate_id 在多个用户登录时会导致问题(旧会话 ID 与新会话 ID)。在我的情况下使用session_regenerate_id 是否明智(因为我没有将任何数据存储到数据库中)?
感谢M Miller更新代码
【问题讨论】:
-
您应该存储密码的哈希值。
-
多个用户共享一个登录始终是 BAD BAD BAD。您如何确定哪个用户进行了更改?如何仅删除其中一个用户的访问权限?
-
@Fluffeh - 好的,谢谢,没问题,在这种情况下只有 1 个用户可以访问!谢谢
-
请注意,您调用了两次
session_start(),并且您应该在header('Location: login.php');之后调用exit;(我认为浏览器可以拒绝标题...我疯了吗? )。 -
@SolomonClosson,他们需要访问您的 Web 服务器才能读取 PHP 源代码。但我确实确认了我之前所说的,肯定添加
exit,因为浏览器可以拒绝位置重定向并加载任何 HTML/执行任何 PHP 跟随它。