是否可以在没有数据库访问权限的情况下对页面进行密码保护?我可能只有几页。但我应该能够更改密码并保存 session 等。而且我想要一种安全的方式,因为它适用于生产站点!
md5后如何存储在config.php中:
<?php
username="admin";
password="1a1dc91c907325c69271ddf0c944bc72";
?>
如果这是个好主意,有没有一种方法可以限制仅从一个脚本访问此 php 称为 check.php 或其他什么?
最佳答案
当然可以,为什么不呢?您可以在无法访问的目录(受 .htaccess 保护或位于 www 根目录之外)中使用平面文件,并将其用作数据库。
这是我创建的一个简单的登录类:
class SimpleLogin {
private $users;
private $db = './pass.txt';
function __construct() {
$data = file_get_contents($this->db);
if (!$data) {
die('Can\'t open db');
} else {
$this->users = unserialize($data);
}
}
function save() {
if (file_put_contents($this->db, serialize($this->users)) === false)
die('Couldn\'t save data');
}
function authenticate($user, $password) {
return $this->users[$user] == $this->hash($password);
}
function addUser($user, $password) {
$this->users[$user] = $this->hash($password);
$this->save();
}
function removeUser($user) {
unset($this->users[$user]);
$this->save();
}
function userExists($user) {
return array_key_exists($user, $this->users);
}
function userList() {
return array_keys($this->users);
}
// you can change the hash function and salt here
function hash($password) {
$salt = 'jafo2ijr02jfsau02!)U(jf';
return sha1($password . $salt);
}
}
注意:如果您要在实际服务器中使用它,您确实应该关闭错误报告。这可以通过调用 error_reporting() 来完成或者在 file_get_contents
和 file_put_contents
前面添加“@”(即:变成 @file_get_contents
)
关于php - 使用 php 密码保护没有数据库访问权限的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3402782/