make an url password protected from direct accessing htaccess

时光怂恿深爱的人放手 提交于 2019-12-24 07:46:08

问题


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?

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:


Solution1:

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:

<?php
session_start();
if(isset($_SESSION["log"])){
?>
//your site
<?php }else{ ?>
You are not allowed to enter!!!
<?php } ?>

Solution2:

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>


来源:https://stackoverflow.com/questions/38632183/make-an-url-password-protected-from-direct-accessing-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!