简体   繁体   English

如何防止通过 URL 直接访问页面

[英]How to prevent direct access page through URL

please How to write PHP code.请问如何编写PHP代码。 prevent page direct URL access but access through button.阻止页面直接 URL 访问但通过按钮访问。 let suppose I have a button with link "www.demo.com/example.html".假设我有一个带有链接“www.demo.com/example.html”的按钮。 someone click the button redirect this page "www.demo.com/example.html" but someone access through direct trough URL it show denied.有人单击按钮重定向此页面“www.demo.com/example.html”,但有人通过直接槽 URL 访问它显示被拒绝。 Someone help me collage project.有人帮我拼贴项目。

Use form for secure unwanted access.使用表单来保护不需要的访问。

//SOURCE FILE //源文件

<form action="www.demo.com/example.html" method="POST">
<input type="submit" name="SubmitButton">
</form>

//DESTINATION FILE //目标文件

if(!isset($_POST["SubmitButton"])){
header("Location: SourceFile.php");}

In source file use submit button to redirect.在源文件中使用提交按钮进行重定向。

In destination file check "SubmitButton", If user try to access page using url then he/she will redirect to same page.在目标文件中检查“SubmitButton”,如果用户尝试使用 url 访问页面,那么他/她将重定向到同一页面。

method 1:方法一:

in js, you can use local storage在js中,可以使用本地存储

on the button page:在按钮页面上:

localStorage.setItem('fromButtonRedirect', 'yes');

on the redirected page:在重定向页面上:

const getData = localStorage.getItem('fromButtonRedirect');
if (getData === 'yes') {
// allowed...
}
else {
 // access is denied...
}

localStorage.removeItem('fromButtonRedirect');

method 2:方法二:

in php, you can add a query string在php中,您可以添加查询字符串

(but you file should be a php file [www.demo.com/example.php]) (但你的文件应该是一个 php 文件 [www.demo.com/example.php])

on button开按钮

again in example.php you can check using isset再次在 example.php 中,您可以使用 isset 检查

if not set then, access is denied.如果未设置,则拒绝访问。

method 3:方法3:

you can add some identifier in .htaccess您可以在 .htaccess 中添加一些标识符

or as posted from previous answer.或从以前的答案中发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM