我正在制作一个目录列表器!
这是我的网站 Website
我已经在这个网站上成功实现了 php 代码。
但现在我必须用密码保护该页面!我需要帮助如何做到这一点!
如果我制作一个简单的 php/html 文件用于重定向到目录列表器,人们将看到该链接,并且可以绕过输入密码,并直接访问该目录链接!
但是我必须锁定整个页面,我该怎么办!
额外:
我想首先打开一个页面,其中有两个按钮:
- 访问目录
- FTP 登录
点击访问登录后会提示输入密码并进入目录!
最佳答案
是时候了解 $_SESSION
和 $_POST
您可以通过两种方式之一对用户进行身份验证/
$_SESSION
使用 php 在服务器上创建 cookie 和相应的用户数据信息,以确保您知道您在与谁交谈。您必须使用某种密码来使用此方法。
让一个有密码的页面转到提交页面
<form action="login.php" method="post">
<input type="password" name="pass" />
<input type="submit" />
</form>
login.php 看起来像
<?php
session_start(); //this starts a session for any user visiting the page
if (!$_POST["pass"] === "mypassword") { //if the user got there with no password
die(); //kill the page
}
//anything after here will only be shown to people who input "mypassword"
//at this point we can give the user a piece of data so they can access other
//parts of the site with
$_SESSION["auth"] = true;
您放入 session 中的任何内容都仅适用于该用户。现在,在您网站的 protected 部分,您可以
<?php
session_start();
if (!$_SESSION["auth"]) {
echo "you don't have priveleges to come here!";
die();
}
//authenticated users continue to roped off part.
$_POST
您也可以使用帖子,就像上面一样,但这样做不会记住用户,如果他们离开您的页面。 (他们必须重新登录)
if (!$_POST["pass"] === "mypassword") { //if the user got there with no password
die(); //kill the page
}
//anything after here will only be shown to people who input "mypassword"
关于javascript - 密码保护目录列表器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31367842/