Q: PHP how do I maintain my variables on a redirect to myself so that when I pass through I don't get a message when I hit back I learned this back in college a few years ago, and now I actually have to do something like this for work. I'm sifting through my old homework assignments and man I wish I was neater.
I'm creating a registration page.
User submits POST to self -> php validates on the same page
if it's good
I direct to a thankYou.php page and clear any variables.
if it's no good, I redirect to myself and populate the form with my bad answers.
Do i need to start a session and store all my variables in a session or something?
I omitted some of the code. to make it quicker to read
<?php
    //connect to database.....

    //Extracting the data
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
     $pageValid = true;
     $fName         = $_POST['fName'];
     $lName         = $_POST['lName'];
    };

        //validate $fname $lname etc $pageValid = true if it's all good

        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        if ($pageValid  == true){
            //insert into sql
        header('Location: thankyou.php');
        exit;

        } else  {  
        //if page is not valid redirect come back here
        header('Location: register.php');
        exit;
    };
} //<!--End of ($_SERVER['REQUEST_METHOD'] == 'POST')


?>
<!DOCTYPE html>
<html lang="en">
<head>header...</head>

<body>

<div id="form"  class="col-md-12">
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">

<table class="table"><tr>
<td width="200"><label for="firstName">First Name:</label></td>
<td>
<input name="fName" type="text" id="register" 
value="<?php 

//$fName I want to add the value of $fName here after it gets redirected

?>" size="25" maxlength="50" /> *
 <?php print $fNameError;?>          
</td>
</tr>
</table>

</body>
</html>


A: fName can be populated with $_REQUEST['fName']
You could always retrieve the value of every post inputs using $_POST (or $_GET for forms with the GET method)
In both cases, you can retrieve your input values accessing the array $_REQUEST. Find here the documentation
Using a session is a really really bad idea: it would cause you tons of headache when your user will start accessing your web app from multiple tabs. It is also a problem since it will require you to clear the session after having processed the form, or unexpected results may happen the next time the user will use the form (like, for example, input fields automatically and unexplainably filled with no user input).
Update
Storing forms inputs in the session is discouraged for at least two reasons:


*

*sessions are shared between all the pages concurrently opened by the same user. Imagine you open the form in a tab and you submit it with some errors; the web app will re-open the form, filling the forms with the data it has in session. Now, open a second tab with the same form: the session is still the same, so the form will be filled with the data in the first form. Submit it with some errors: you will have changed the data for both the forms

*$_REQUEST items are populated during a POST, and they are automatically cleaned up the next request; sessions are not, they are persisted for the whole session. This means that your code will need to clear them up explicitely, or you will risk to find form inputs with the old values even without a form submit

A: Yes, storing data in $_SESSION variable is a good idea.
e.g.$_SESSION["lname"] = $_POST["lname"];. Obvioulsy you need to start a session, check for input validity, etc....

A: Basically you check for the existence of a POST variable:
<?php
if( isset( $_POST['fName'] ) )
{
  // the form has been submitted, do something

}
?>

You don't have to use session variables if your form data is displayed on the page that receives the POST data.
Edit
If you want to populate some $_SESSION variables then you could stuff all the POST data into a session array
<?php
if( isset( $_POST['fName'] ) )
{
  $_SESSION['posted'] = $_POST;
}
?>

Or, if you validate POST data and want to populate the session with only valid input:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  $_SESSION['formData'] = array();
  $_SESSION['formData']['pageValid'] = true;
  $_SESSION['formData']['fName'] = $_POST['fName'];
  $_SESSION['formData']['lName'] = $_POST['lName'];
};
?>


A: You don't need to redirect back to the form on an error... The form is built within the same script so just let it render the rest of the script. You only need a redirect on valid registration data.
Basically, get rid of the "else" portion of you PHP.
