防止直接访问php页面,仅在重定向后才能访问 [英] preventing direct access to a php page, only access if redirected
问题描述
我想使我的php页面只能从另一个页面重定向访问,并防止我的用户直接访问它.
I want to make my php page only accessible from another page redirect and prevent my user from accessing it directly.
我的意思是,假设我有一个名为"main.php" 的页面,另一个我想阻止直接访问的PHP文件名为"noaccess.php".
I mean, let's say I have a page called "main.php" and another PHP file that I want to prevent direct access to, called "noaccess.php".
仅当我从main.php重定向时,我才希望使noaccess.php可访问
I want to make noaccess.php accessible only if I redirect from main.php
有什么建议吗?
更新:会话是一个好主意,但是问题是我必须使用JavaScript来重定向页面,所以问题是,我可以使用ajax来设置PHP会话吗?
UPDATE: Session is a good idea, but the problem is I have to use JavaScript to redirect the page, so the question is, can I use ajax to set a PHP session?
更新2:确定,我找到了解决方案,我现在不需要阻止直接访问,因为我可以从mysql检查页面是否需要访问.
UPDATE 2: OK I found the solution, I don't need preventing direct access now, as I can check from mysql whether the page needs to be accessible or not.
推荐答案
如果每次您要重定向时,您都将一个值保存在$ _SESSION变量中,该怎么办?所以你有
What if everytime you were going to redirect you saved a value in the $_SESSION variable. So you have
//code
$_SESSION['fromMain'] = "true";
header("Location: noaccess.php");
然后在noaccess.php中放
Then in noaccess.php put
if($_SESSION['fromMain'] == "false"){
//send them back
header("Location: foo.php");
}
else{
//reset the variable
$_SESSION['fromMain'] = "false";
}
我真的不知道这是否行得通,但这就是我要尝试的方法.
I really don't know if this would work or not, but this is what I would try off the top of my head.
这篇关于防止直接访问php页面,仅在重定向后才能访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!