.php 和 .html 只能由 "server" 打开
.php and .html openable only by "server"
我有一个 index.php 页面,其中有一个菜单:此菜单不同取决于我是否登录。
到目前为止,我已经创建了两个文件 html:"logged-menu-html" 和 "not-logged-menu.html",然后在 index.php 中我在 php 中写了类似的东西:
"if (logged) include(logged-menu.html), else include(not-logged-menu.html)"
第一个问题(我只是好奇)..这种方式是否正确加载正确的菜单?还是一定要避免?
现在,我的真正问题是:我希望这两个文件 .html 为 "private",这意味着如果不通过 index.php 则无法加载它们!
到目前为止,我只有一个解决方案:当我通过 FileZilla 连接到我的服务器(它来自我的大学)时,我有一个文件夹 public_html,我必须在其中放置我想要在线使用的文件。
我找到的解决方案是为 public_html 创建一个同级文件夹:这样我就不能使用符号“../privatefile/*.html”(如果 index.php在 public_html) 中,但用户无法访问该文件夹。
这个解决方案可以吗?还是必须避免? :)
谢谢!
如果您使用的是 Apache - 2.4 版(因为这就是我目前使用的版本)
授权
您可以在任何人访问您的页面之前设置登录。
转到 Apache 文件夹 > bin > htpasswd - 从终端打开 htpasswd
如果这是您第一次创建密码文件:
htpasswd -c /dir username
然后输入用户密码。
否则,如果您之前创建过密码文件,只需删除 -c
标志即可。
然后,在您的 .htaccess
文件中,键入以下内容:
AuthType Basic #Type of authentication used
AuthName "This is a private area, please Log In!" #Your Login Text
AuthUserFile "C:\Apache24\Password.txt" #In which your password file is stored.
AuthGroupFile /dev/null
require valid-user
或者,如果您想在 httpd.conf
上这样做:
<Location "/protected-area">
AuthUserFile "C:\Apache24\Password.txt"
AuthName "This is a private area, please Log In!"
AuthGroupFile /dev/null
AuthType Basic
Require valid-user
</Location>
现在,如果有人访问您的页面,系统会提示他们登录。
登录
您可以设置登录页面以允许有效用户访问。
This is different from the "Auth", as in this case you will be creating files like "Login.php"
首先,将 DirectoryIndex
设置为 Login.php
,或者为登录页面设置的任何文件名。
然后,创建您的登录页面。
在您的 <form>
中,将 action
属性设置为:
<?php echo htmlspecialchars($_SERVER[PHP_SELF]); ?>
<head>
部分的php:
<?php
$err = '';
if (isset($_POST['login']) && !empty($_POST['username']) && !empty($_POST['password'])) {
if ($_POST['username'] == 'user' && $_POST['password'] == '123456') {
$_SESSION['valid'] = true;
$_SESSION['timeout'] = time();
$_SESSION['username'] = 'user';
header('Location: Welcome.php');
} else {
$err = 'Wrong username or password';
}
}
?>
在welcome.php
:
To prevent user from accessing "Welcome.php" without logging in:
<?php
if(isset($_SESSION['valid']) && $_SESSION['valid'] == true) {
echo "You are logged in!";
} else {
header("Location: Login.php");
}
?>
拒绝访问
I'm not sure about this ...
<Directory ~ "\.private">
Order allow,deny
Deny from all
</Directory>
重写规则
RewriteRule ^(.*/)?\.private/ - [F,L]
重定向
RedirectMatch 404 /\.private(/|$)
我有一个 index.php 页面,其中有一个菜单:此菜单不同取决于我是否登录。
到目前为止,我已经创建了两个文件 html:"logged-menu-html" 和 "not-logged-menu.html",然后在 index.php 中我在 php 中写了类似的东西:
"if (logged) include(logged-menu.html), else include(not-logged-menu.html)"
第一个问题(我只是好奇)..这种方式是否正确加载正确的菜单?还是一定要避免?
现在,我的真正问题是:我希望这两个文件 .html 为 "private",这意味着如果不通过 index.php 则无法加载它们!
到目前为止,我只有一个解决方案:当我通过 FileZilla 连接到我的服务器(它来自我的大学)时,我有一个文件夹 public_html,我必须在其中放置我想要在线使用的文件。
我找到的解决方案是为 public_html 创建一个同级文件夹:这样我就不能使用符号“../privatefile/*.html”(如果 index.php在 public_html) 中,但用户无法访问该文件夹。
这个解决方案可以吗?还是必须避免? :)
谢谢!
如果您使用的是 Apache - 2.4 版(因为这就是我目前使用的版本)
授权
您可以在任何人访问您的页面之前设置登录。
转到 Apache 文件夹 > bin > htpasswd - 从终端打开 htpasswd
如果这是您第一次创建密码文件:
htpasswd -c /dir username
然后输入用户密码。
否则,如果您之前创建过密码文件,只需删除 -c
标志即可。
然后,在您的 .htaccess
文件中,键入以下内容:
AuthType Basic #Type of authentication used
AuthName "This is a private area, please Log In!" #Your Login Text
AuthUserFile "C:\Apache24\Password.txt" #In which your password file is stored.
AuthGroupFile /dev/null
require valid-user
或者,如果您想在 httpd.conf
上这样做:
<Location "/protected-area">
AuthUserFile "C:\Apache24\Password.txt"
AuthName "This is a private area, please Log In!"
AuthGroupFile /dev/null
AuthType Basic
Require valid-user
</Location>
现在,如果有人访问您的页面,系统会提示他们登录。
登录
您可以设置登录页面以允许有效用户访问。
This is different from the "Auth", as in this case you will be creating files like "Login.php"
首先,将 DirectoryIndex
设置为 Login.php
,或者为登录页面设置的任何文件名。
然后,创建您的登录页面。
在您的 <form>
中,将 action
属性设置为:
<?php echo htmlspecialchars($_SERVER[PHP_SELF]); ?>
<head>
部分的php:
<?php
$err = '';
if (isset($_POST['login']) && !empty($_POST['username']) && !empty($_POST['password'])) {
if ($_POST['username'] == 'user' && $_POST['password'] == '123456') {
$_SESSION['valid'] = true;
$_SESSION['timeout'] = time();
$_SESSION['username'] = 'user';
header('Location: Welcome.php');
} else {
$err = 'Wrong username or password';
}
}
?>
在welcome.php
:
To prevent user from accessing "Welcome.php" without logging in:
<?php
if(isset($_SESSION['valid']) && $_SESSION['valid'] == true) {
echo "You are logged in!";
} else {
header("Location: Login.php");
}
?>
拒绝访问
I'm not sure about this ...
<Directory ~ "\.private">
Order allow,deny
Deny from all
</Directory>
重写规则
RewriteRule ^(.*/)?\.private/ - [F,L]
重定向
RedirectMatch 404 /\.private(/|$)