How can I redirect to a page with a password, but the page cannot be accessed via URL?

Viewed 55

I'm learning web development and I'm creating a puzzle website where the home page will only have an answer input and will redirect the user to the next puzzle based on the answer. E.g. solved the first level and the answer is "solution". When typing "solution" the user will be redirected to the second level.

But it's important that the user can only access the second level page with the password, otherwise it won't allow.

I have a super basic draft that correctly redirects the user based on the answer, but I don't know how to proper restrict direct access to the next levels.

Home page input:

<form method="post" action="validation.php">
Answer: <input type="text" name="answer">
<input type="submit">
</form>

validation.php:

<?php
$input = $_POST["answer"];
if ($input == "correct answer 1") {
    header("Location: /levels/level2/level2.php"); }
if ($input == "correct answer 2") {
    header("Location: /levels/level3/level3.php"); }
if ($input == "correct answer 3") {
    header("Location: /levels/level4/level4.php"); }
?>

The order of the answers is irrelevant; if the user enters the password for level 7 without first going through the previous levels, he should be taken to level 7. I'm also not overly concerned about safety; all I want to do is play a game with some friends. I believe there is a way to do this with PHP and JS, but any solution that works is fine.

1 Answers

Ok, is very simple if you are using your post variable for validation.

For example, you can add further validation, something like this:

if (isset($_POST["answer"]) )  //*we ask if there's an answer...*//
{
    $input = $_POST["answer"];
    if ($input == "correct answer 1") {
      header("Location: /levels/level2/level2.php"); }
    if ($input == "correct answer 2") {
      header("Location: /levels/level3/level3.php"); }
    if ($input == "correct answer 3") {
      header("Location: /levels/level4/level4.php"); }

}else{

 header("Location: accessDenied-page.php"); //*if there's not an answer, we can redirect users to other page.*//

}

Im not sure if this is what you are looking for, if not, let me know.

Inside EveryLevel.php you need to re validate whith isset, like this:

<?php
if (isset($_POST["answer"]) )  //*we check if there's an answer...*//
{
 header("Location: accessDenied-page.php");  //*we redirect user somewhere else*//
}
?>

Related