2

是否可以在没有数据库访问权限的情况下对页面进行密码保护?我可能只有几页。但是我应该能够更改密码并保存会话等。而且我想要一种安全的方式,因为它适用于生产站点!

如何在 md5 之后存储在 config.php 中:

 <?php
 username="admin"; 
 password="1a1dc91c907325c69271ddf0c944bc72";
 ?>

如果这是一个好主意,有没有办法限制只从一个名为 check.php 的脚本或其他东西访问这个 php?

4

4 回答 4

2

当然,为什么不呢?您可以使用不可访问目录中的平面文件(受 .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或在and前面添加“@”来完成file_put_contents(即:所以它变成@file_get_contents

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

于 2010-08-04T05:16:55.213 回答
2

你应该用它.htaccess来做到这一点。您还可以通过.htaccess合理的 php 文件进行保护,例如:

Order Allow,Deny
Deny from All
于 2010-08-04T05:17:08.060 回答
1

您可以将HTTP 身份验证与 PHP 一起使用。PHP-docu 中提供了非常好的示例。

于 2010-08-04T05:16:30.073 回答
0

实际上,数据库与密码保护无关。
您可以直接在脚本中写入登录名和密码,也可以保存在数据库中。

没有必要限制对您的 php 文件的访问。通过 HTTP 调用,它将只是空白页面,仅此而已。

So, it's all right to store it that way.
Quite enough for the site that even don't use a database.

于 2010-08-04T05:27:01.020 回答