gpt4 book ai didi

php - 使用 php 密码保护没有数据库访问权限的页面

转载 作者:搜寻专家 更新时间:2023-10-30 20:06:42 25 4
gpt4 key购买 nike

是否可以在没有数据库访问权限的情况下使用密码保护页面?我可能只有几页。但我应该能够更改密码并保存 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_contentsfile_put_contents 前添加“@”(即:它变成了 @file_get_contents)

使用示例:http://left4churr.com/login/

关于php - 使用 php 密码保护没有数据库访问权限的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3402782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com