1

I'm a little puzzled with how to implement the Post/Redirect/Get pattern in PHP. I've seen a few answers saying just add the header function after you've submitted the form; I've done that but upon validating whether the input fields are empty, it doesn't print anything although I've added the header function after it's ran the code. I simply cannot find how to integrate this, so I'm asking anyways.

<?php

require '../BACKEND/DB/DB_CONN.php';

if(isset($_POST['submit-register'])) {

    if(empty($_POST['email'])) {
        echo 'Email cannot be nothing.';
    }

    header("Location: index.php");
    die();
}

if(isset($_POST['submit-login'])) {

    if(empty($_POST['loginUser'])) {
        echo 'Field Empty.';
    }

    header("Location: index.php");
    die();

}

?>

<div class="forms">
        <form method="post" action="index.php" role="form" class="forms-inline register ">
        <h4>Register</h4>
            <input type="text" name="email" placeholder="Email Address" autocomplete="off" />
            <input type="text" name="username" placeholder="Username" autocomplete="off" />
            <input type="password" name="password" placeholder="Password" autocomplete="off" />
            <input type="submit" name="submit-register" value="Register" role="button" name="login-btn" />
        </form>
        <form method="post" action="index.php" role="form" class="forms-inline login ">
        <h4>Login</h4>
            <input type="text" name="loginUser" placeholder="Username" />
            <input type="password" name="loginPass" placeholder="Password" />
            <input type="submit" name="submit-login" value="Register" role="button" />
        </form>
    </div>

I was writing the code, but as I've found out it doesn't work, I'm asking how to fix this and how to implement the Post/Redirect/Pattern securely and I know it works.

2
  • Headers don't work properly if any information is printed to the page. Also, if the header IS working, you will never see that output because it would redirect before the page actually loads. Commented Aug 3, 2017 at 15:39
  • @GrumpyCrouton I even placed the header before the print, nothing printed. Commented Aug 3, 2017 at 15:48

2 Answers 2

2

See the submit-register POST operation validates and redirect to index.php by passing the validation message. You need to GET your message passed from header method.

In PRG Pattern when you do POST operation that has the data but when you redirect and do GET after post to maintain PRG, you have to pass your data to last destination GET url.

In your Code, see both way I have done, first one passes your message to index but second one not PRG when error occurs in validation.

//PRG..........................................
if(isset($_POST['submit-register'])) {

    if(empty($_POST['email'])) {
        echo 'Email cannot be nothing.';
        $msg = "Email cannot be nothing";
    }

    header("Location: index.php?msg=$msg");
    //Get your this message on index by $_GET //PRG

}
//Not full PRG with no passing data to destination.........................
if(isset($_POST['submit-login'])) {

    if(empty($_POST['loginUser'])) {
        echo 'Field Empty.';
    }
    else{
        header("Location: index.php");
    }

}

Note That there should not be any thing printed on page before header method. This means simply no echo before header method.

See first one is PRG and second one is also PRG but not with your data passed to destination.

header("Location: index.php?msg=$msg");
Sign up to request clarification or add additional context in comments.

8 Comments

I placed the header before printing anything but nothing got printed.
see updated code...... and comments on code, to get printed you have to pass your msg to index.php, after post and redirect , initiating new get request by header does not carry your message, you have to pass your data to the last GET (header method url) operation in your PRG pattern.
Okay, now I know it works in the URL bar, but how do I print the message in a different place - on the page?
just on your index page do <?php echo $_GET['msg']; ?> but check if exist or not , if(isset($_GET['msg'])){ echo $_GET['msg']; } , define your common message area and print in your area.
are you trying for login? or register? because above code only msg there for register. try register, and see above code, check if msg is set or not as i mentioned in my comment and print your msg on last redirected page.
|
0

Using header(path) resets the page. Therefore, any echos printed before would be discarded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.