密码保护php页面的简单方法 [英] Easy way to password-protect php page

查看:82
本文介绍了密码保护php页面的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要设置密码保护的页面.我尝试过进行HTTP身份验证,但是由于某种原因,它在我的主机上不起作用.还有其他快速(简便)的方法吗?谢谢!

I have a page I want to password-protect. I've tried doing HTTP authentication, but for some reason it doesn't work on my hosting. Any other quick (and easy) way to do this? Thanks!

推荐答案

这里的密码保护功能不完全可靠,因此请不要使用它来保护信用卡号或其他非常重要的内容.

Not exactly the most robust password protection here, so please don't use this to protect credit card numbers or something very important.

只需将以下所有代码放入名为(secure.php)的文件中,更改用户并将"admin"传递给您想要的任何内容.然后,在包含include("secure.html")的那些行下,只需将其替换为您希望他们能够看到的文件名即可.

Simply drop all of the following code into a file called (secure.php), change the user and pass from "admin" to whatever you want. Then right under those lines where it says include("secure.html"), simply replace that with the filename you want them to be able to see.

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

They will access this page at [YouDomain.com/secure.php] and then the PHP script will internally include the file you want password protected so they won't know the name of that file, and can't later just access it directly bypassing the password prompt.

如果您想进一步提高保护级别,建议您将(secure.html)文件从站点的根文件夹[/public_html]中删除,并将其与该目录位于同一级别,因此它不在目录内.然后,在包含文件的PHP脚本中,只需使用("../secure.html").该(../)意味着返回目录以查找文件.这样,某人访问(secure.html)页面上的内容的唯一方法是通过(secure.php)脚本.

If you would like to add a further level of protection, I would recommend you take your (secure.html) file outside of your site's root folder [/public_html], and place it on the same level as that directory, so that it is not inside the directory. Then in the PHP script where you are including the file simply use ("../secure.html"). That (../) means go back a directory to find the file. Doing it this way, the only way someone can access the content that's on the (secure.html) page is through the (secure.php) script.

<?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>
    <?}
}
?>

这篇关于密码保护php页面的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆