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

标签 php database authentication password-protection

是否可以在没有数据库访问权限的情况下对页面进行密码保护?我可能只有几页。但我应该能够更改密码并保存 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/

相关文章:

php - 如何在学校门户中表示三个学期的学年

php - 尝试匹配两个数组中的值,如果存在匹配值则输出 true

php - 在 php 中连接到 MySQL 数据库时遇到问题

c# - 远程数据库良好实践

database - 在 postgres 中创建表空间时权限被拒绝

php - 将数组内容放入表中

php - Domain 目前无法在 Laravel 项目上处理此请求

ios - 在 Parse (Swift) 中接受登录参数

mongodb - 无法使用 --db 创建备份 mongodump。身份验证失败

php - Laravel - 从特定的身份验证保护中获取用户