Home > Net >  How to prevent direct access page through URL
How to prevent direct access page through URL

Time:06-21

please How to write PHP code. prevent page direct URL access but access through button. let suppose I have a button with link "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. Someone help me collage project.

CodePudding user response:

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.

CodePudding user response:

method 1:

in js, you can use local storage

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

(but you file should be a php file [www.demo.com/example.php])

on button

again in example.php you can check using isset

if not set then, access is denied.

method 3:

you can add some identifier in .htaccess

or as posted from previous answer.

  • Related