簡體   English   中英

使用PHP密碼保護沒有數據庫訪問權限的頁面

[英]Password protect a page without db access with php

是否可以用密碼保護沒有數據庫訪問權限的頁面? 我可能只有幾頁。 但是我應該能夠更改密碼並保存會話等。我想要一種安全的方式,因為它適用於生產站點!

在md5之后如何將其存儲在config.php中:

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

如果這是一個好主意,是否有一種方法可以限制僅從一個名為check.php或類似內容的腳本訪問此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/

您應該使用.htaccess來做到這一點。 您還可以通過.htaccess保護敏感的php文件,例如:

Order Allow,Deny
Deny from All

您可以將HTTP身份驗證與PHP結合使用 PHP-docu中提供了很好的示例。

實際上,數據庫與密碼保護無關。
您可以直接在腳本中寫入登錄名和密碼,也可以保留在數據庫中。

無需限制對您的php文件的訪問。 通過HTTP調用時,它將只是空白頁,僅此而已。

因此,可以通過這種方式存儲它。
對於甚至不使用數據庫的站點來說,已經足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM