密码保护php页面的简单方法

2022-08-30 10:08:34

我有一个想要密码保护的页面。我尝试过进行HTTP身份验证,但由于某种原因,它在我的主机上不起作用。还有其他快速(和简单)的方法可以做到这一点吗?谢谢!


答案 1

这里不完全是最强大的密码保护,所以请不要用它来保护信用卡号或非常重要的东西。

只需将以下所有代码拖放到一个名为(secure.php)的文件中,更改用户并从“admin”传递到您想要的任何内容。然后,就在它说包含(“安全.html”)的行下方,只需将其替换为您希望他们能够看到的文件名即可。

他们将在[YouDomain.com/secure.php]访问此页面,然后PHP脚本将在内部包含您想要密码保护的文件,因此他们不知道该文件的名称,并且以后无法绕过密码提示直接访问它。

如果您想添加进一步的保护级别,我建议您将(安全.html)文件带到站点的根文件夹[/public_html]之外,并将其放在与该目录相同的级别上,以便它不在目录中。然后在包含该文件的PHP脚本中,只需使用(“../secure.html”)。那 (../) 表示返回目录以查找文件。这样,某人访问(安全.html)页面上的内容的唯一方法是通过(安全.php)脚本。

<?php
$user = $_POST['user'];
$pass = $_POST['pass'];

if($user == "admin"
&& $pass == "admin")
{
        include("secure.html");
}
else
{
    if(isset($_POST))
    {?>

            <form method="POST" action="secure.php">
            User <input type="text" name="user"></input><br/>
            Pass <input type="password" name="pass"></input><br/>
            <input type="submit" name="submit" value="Go"></input>
            </form>
    <?}
}
?>

答案 2

这有点晚了,但我想回复,以防其他人来到这个页面,发现最高的回复有点偏差。我对系统进行了一点改进。请注意,它仍然不是非常安全,但这是一个改进。

首先准备您的密码盐文件:

hash_generate.php:

 <?php

 $user = "Username"; // please replace with your user
 $pass = "Password"; // please replace with your passwd
 // two ; was missing

 $useroptions = ['cost' => 8,];
 $userhash    = password_hash($user, PASSWORD_BCRYPT, $useroptions);
 $pwoptions   = ['cost' => 8,];
 $passhash    = password_hash($pass, PASSWORD_BCRYPT, $pwoptions);

 echo $userhash;
 echo "<br />";
 echo $passhash;

 ?>

获取输出,并将它们分别放在两个文本文件中:user.txt和pass.txt。其他人建议将这些文本文件放在public_html上方,这是一个好主意,但我只是使用了.htaccess并将它们存储在一个名为“stuff”的文件夹中。$userhash$passhash

.htaccess

 deny from all

现在没有人可以窥视哈希。接下来是您的索引.php:

索引.php:

<?php
$user = ""; //prevent the "no index" error from $_POST
$pass = "";
if (isset($_POST['user'])) { // check for them and set them so
    $user = $_POST['user'];
}
if (isset($_POST['pass'])) { // so that they don't return errors
    $pass = $_POST['pass'];
}    

$useroptions = ['cost' => 8,]; // all up to you
$pwoptions   = ['cost' => 8,]; // all up to you
$userhash    = password_hash($user, PASSWORD_BCRYPT, $useroptions); // hash entered user
$passhash    = password_hash($pass, PASSWORD_BCRYPT, $pwoptions);  // hash entered pw
$hasheduser  = file_get_contents("stuff/user.txt"); // this is our stored user
$hashedpass  = file_get_contents("stuff/pass.txt"); // and our stored password


if ((password_verify($user, $hasheduser)) && (password_verify($pass,$hashedpass))) {

    // the password verify is how we actually login here
    // the $userhash and $passhash are the hashed user-entered credentials
    // password verify now compares our stored user and pw with entered user and pw

    include "pass-protected.php";

} else { 
    // if it was invalid it'll just display the form, if there was never a $_POST
    // then it'll also display the form. that's why I set $user to "" instead of a $_POST
    // this is the right place for comments, not inside html
    ?>  
    <form method="POST" action="index.php">
    User <input type="text" name="user"></input><br/>
    Pass <input type="password" name="pass"></input><br/>
    <input type="submit" name="submit" value="Go"></input>
    </form>
    <?php 
} 

推荐