skip to Main Content

Can´t fix this error: "Expecting POST to Redirected to a GET – found POST".
I run this code with ngrok and XAMPP.

This is a part from following assignment: https://www.wa4e.com/assn/rps/.

Actually everything works OK, authorisation works great BUT in assignment’s autograder there is this error:
"Checking to see if the POST redirected to a GET. Expecting POST to Redirected to a GET – found POST"

HTML part

<form method="POST">
   <label for="nam">User Name</label>
   <input type="text" name="who" id="nam"><br/>
   <label for="id_1723">Password</label>
   <input type="text" name="pass" id="id_1723"><br/>
   <input type="submit" value="Log In">
   <input type="submit" name="cancel" value="Cancel">
</form>

PHP part:

// Check to see if we have some POST data, if we do process it

if ( isset($_POST['who']) && isset($_POST['pass']) ) {
    if ( strlen($_POST['who']) < 1 || strlen($_POST['pass']) < 1 ) {
        $failure = "User name and password are required";
    } else {
        $check = hash('md5', $salt.$_POST['pass']);
        if ( $check == $stored_hash ) {

            // Redirect the browser to game.php
header("Location: https://c561-212-58-119-178.ngrok-free.app/rps/game.php?name=".urlencode($_POST['who']), true, 303);
            return;
        } else {
            $failure = "Incorrect password";
        }
    }
}

I’ve expected that additional parameters to header() as "…true, 303…" would help me, but nothing had chanchged 🙁

2

Answers


  1. Looks like you need to terminate the code execution with exit() instead of return. This ensures no further code execution:

    if ( isset($_POST['who']) && isset($_POST['pass']) ) {
            if ( strlen($_POST['who']) < 1 || strlen($_POST['pass']) < 1 ) {
                $failure = "User name and password are required";
            } else {
                $check = hash('md5', $salt.$_POST['pass']);
                if ( $check == $stored_hash ) {
        
                    // Redirect the browser to game.php
        header("Location: https://c561-212-58-119-178.ngrok-free.app/rps/game.php?name=".urlencode($_POST['who']), true, 303);
                    exit();
                } else {
                    $failure = "Incorrect password";
                }
            }
        }
    
    Login or Signup to reply.
  2. Try enclose your code into below code

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // your current code
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search