我有一个家族史网站,叫它"my_family.com“。主文件index.php有一些介绍性说明和一个html表单,其中包含网站的密码(所有家庭成员都使用一个密码)。如果运行"my_family.com“并插入正确的密码并单击"Submit”按钮,文件中的php代码将带您进入几个html文件中的第一个--称为"first.html“,这将提供一个指向进一步html文件的链接。所有这些文件都包含家谱、信件副本、照片、提醒、讣告等,不应向非家庭成员开放。
问题是,如果入侵者碰巧学习了文件名"first.html“并将"my_family.com/first.html”放入他/她的浏览器中,入侵者将立即被带到first.html,完全跳过密码过程。从那里,入侵者可以访问整个网站。
我怎样才能防止这种情况发生呢?
我的网络主机GoDaddy的一位技术人员教我如何在.htaccess文件中插入密码保护器。通过此更改,网站的第一个操作是,在一个无标记的对话框中,请求用户名和密码。但我根本不想使用用户名,我想使用我的自定义形式的密码。
任何帮助都将不胜感激。
发布于 2020-09-19 18:54:38
我知道这并不完全是你想要的,但我认为你的GoDaddy技术员找到了最好、最简单的解决方案。我要做的是将所有安全文件放在一个单独的目录中,并从不安全的index.php文件链接到该目录。如果您只想让用户记住密码,您可以包括一个通知,告诉您的用户要使用什么用户名。从本质上说,你会有这样一个网站:
例如,在https://my_family.com/index.php中:
Login Here with the username "Guest"然后,在https://my_family.com/secure/中,您将得到GoDaddy技术描述的.htaccess文件,如下所示:
Authtype Basic
AuthName "WHATEVER_YOU_WANT_HERE"
AuthUserFile /path/to/password/file
Require valid-user因此,未经身份验证的用户可以查看和访问您的index.php页面,但是他们需要正确的密码才能访问您的敏感内容。当然,他们会有你正在谈论的丑陋的登录表单,但它将是快速、容易和安全的。
也就是说,如果您真的希望拥有一个自定义表单,那么它将增加大量的复杂性。但是有很多方法你可以采取。这里有一个:
你可以这样做:
Invalid password";
}
?>
Password:
Current Directory: $directory";
if ($files) {
echo '';
foreach ($files as $file) {
if (is_dir($secureDirectory . '/' . $file)) {
printf('%s', self::getBaseUrl() . '?directory=' . urlencode($directory . $file), htmlentities($file));
} else {
printf('%s', self::getBaseUrl(), urlencode($file), urlencode($directory), htmlentities($file));
}
}
echo '';
} else {
echo 'No files';
}
}
/**
* Outputs the contents of a file
*
* @param $directory
* @param $file
*/
public static function readFile($directory, $file)
{
// As above, regularize forward slashes and remove any periods in the path which could otherwise be a security risk
$fullPath = preg_replace('#[/]+#', '/', '/' . self::SECURE_PATH . '/' . $directory . '/') . str_replace('/', '', $file);
if (is_file($fullPath)) {
readfile($fullPath);
} else {
echo "Not a file ";
echo $fullPath;
}
}
/**
* Gets the base URL for the current web page
*
* @return string
*/
public static function getBaseUrl()
{
return 'https://' . $_SERVER[HTTP_HOST] . strtok($_SERVER["REQUEST_URI"], '?');
}
/**
* Builds a secure path for the given directory and file. Ensures that the directory or file is located within the
* secure path so that malicious users cannot access files they should have access to
*
* @param $directory
* @param null $file
* @return bool|string
* @throws Exception
*/
public static function getSecurePath($directory, $file = null)
{
// First let's regularize the forward slashes in the directory
$directory = preg_replace('#/+#', '/', '/' . self::SECURE_PATH . '/' . $directory . '/');
// Next get rid of all forward slashes in the file
$file = str_replace('/', '', $file);
$result = realpath($directory . $file);
// Make sure that the directory/file falls within the secure directory
if (strpos($result, realpath(self::SECURE_PATH)) !== 0) {
throw new Exception("Invalid path");
} else {
return $result;
}
}
}
MySecurePage::run();请注意,上面的脚本只是一个例子。它不是很有用;它还没有经过彻底的测试;它很可能会带来安全漏洞!
尽管有上面的例子,我还是建议你考虑一下第一种选择,即使这并不是你想要的。它不仅更容易实现,而且可能更安全。例如,在我所提供的脚本中,我基本上将明文密码硬编码到文件中,这是一个安全实践中的大 no-no。(我想您可以做一些类似于散列密码的操作,以使其更加安全,但即使这样也远远不够理想!)另一个例子是,如果您不小心的话,也很容易意外地允许经过身份验证的用户访问您甚至不希望他们能够访问的文件。
我想你需要决定的问题是,你愿意花多少时间和精力去拥有一个更漂亮的登录表单。
https://webmasters.stackexchange.com/questions/131488
复制相似问题