设置防止直接访问htaccess的网址密码 [英] make an url password protected from direct accessing htaccess
问题描述
我是.htaccess
的新手.我有两个分别名为page1.php
和page2.php
的文件,而page2.php
在另一个文件夹中,并且page1.php
具有指向它的链接.当有人想要直接打开page2.php
时,我想为page2.php
设置密码保护URL.
但是,当有人想从page1.php
页面转到该页面时,它应该是打开的.
I am new to .htaccess
. I have two files named page1.php
and page2.php
,while page2.php
is in another folder and page1.php
has a link to it.I want to make page2.php
password protected when someone wants to open page2.php
directly hitting the URL.
But it should be open when someone wants to go to that page from page1.php
page.
有人可以帮我完成这项工作吗?
Can anyone help me out to get this done?
注意:我已经在.htaccess中添加了以下内容.但是,无论有人直接打开此页面还是从page1.php
转到该页面,此页面均受密码保护.我想仅在直接转到该页面时将其设置为受密码保护.
Note:I have already added the following thing to my .htaccess . But here this page is password protected in both ways no matter someone open it directly or go to that page from page1.php
. I want to make it password protected only when directly want to go to that page.
AuthUserFile C:/wamp/www/htaccess_pwd/main/.htpasswd.txt
AuthType Basic
AuthName "You need to be authorized!!"
Require valid-user
<Files "page2.php">
Require valid-user
</Files>
推荐答案
解决方案1:
如果您正在处理.php文件,我认为这是最简单的用php编写代码的方法. page1.php:
If you are dealing with .php files, i think its the easiest to code this in php. page1.php:
<?php
session_start();
$_SESSION["log"]="ok";
?>
page2.php:
page2.php:
<?php
session_start();
if(isset($_SESSION["log"])){
?>
//your site
<?php }else{ ?>
You are not allowed to enter!!!
<?php } ?>
解决方案2:
将密码包含在page1.php的链接中(在某些浏览器中会导致错误)
Include the password into the link on page1.php (will cause errors on some browsers)
<a href="user:password@http://yourserver/page2.php">Log In</a>
这篇关于设置防止直接访问htaccess的网址密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!