Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am making a password proteced webpage using two PHP files named password.php and incog.php.
Here is password.php:
<html>
  <title>Enter the password</title>
  <head>
  <style>
p{
  color:red;
}
  </style>
  </head>
  <body>
    <p>Please enter the pass word or key to visit this site.</p>
  <form action="incog.php" method="post">
Passoword: Do not hack!<input type="password" name = "pass">
<input type="submit" value="Go to site">
    
  </form>
  </body>
</html>


And here is incog.php:
<?php
if ($_POST['pass'] == 'password')
header("Location: hello.html"); 
else
echo "Go away"

?>



The problem is that anyone can get into the "secret" page by guessing the url.

What I have tried:

I have tried to add this code to the secret webpage:

<!DOCTYPE html>
<html>
    <script>
    
function thisFunction() {
  window.location.href = "password.php";
}
</script>
    <body onload="thisFunction()">
        <h1>Hello you have reached the secret page</h1>
    </body>
</html>


But obviously that is not going to work because it will keep taking you back to the same page over and over again.
A solution would be appreciated; I am new to PHP and Javascript.
Posted
Updated 11-Jul-20 12:59pm
v3

1 solution

Use sessions to check if a page was loaded or not, if the user did not enter a password then session is false else they did, they can go to the next page -

Read more on using sessions here -

Sessions, cookies and user validation in PHP
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900