To avoid the double submit problem it is necessary to split the validation page in two. The first will only validade and commit to the database without any interaction with the client browser and the second will give the client the confirmation message.
The flow direction will be: register.php page -> POST to validate.php -> Redirect/GET to confirmation.php
We will also create an index.php page just to have a reference to where to come back after confirmation. Everything inside <!-- -->HTML tags is a comment.
<!-- index.php --> <html> <body> <h3>Welcome to my site</h3> <p><a href="./register.php">Register</a></p> <p><a href="./#">Another Page</a></p> </body> </html>
We have added error messages to register.php:
<?php # register.php if (!isset($_GET['user_name'])) $user_name = ""; else $user_name = $_GET['user_name']; // We will receive error messages // from the validation page if (!isset($_GET['error'])) $error = ''; else $error = $_GET['error']; ?> <html> <body> <h3>Registration Form</h3> <?php if ($error != '') echo '<p style="color:red;">', $error, '</p>'; ?> <form method="POST" action="./validate.php"> <p>User Name: <input type="text" name="user_name" value="<?php echo $user_name; ?>"></p> <input type="submit" value="Submit the Registration Form"> </form> </body> </html>
The validate.php page sends nothing to the browser and after it finishes it redirects to confirmation.php or back to register.php if an error is found. The redirection uses the GET method, sending data in the URL.
<?php # validate.php $user_name = trim($_POST['user_name']); $error = ''; if ($user_name == '') $error = 'User name is required<br />'; // Build the query string to be attached // to the redirected URL $query_string = '?user_name=' . $user_name; // Redirection needs absolute domain and phisical dir $server_dir = $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . '/'; /* The header() function sends a HTTP message The 303 code asks the server to use GET when redirecting to another page */ header('HTTP/1.1 303 See Other'); if ($error != '') { // Back to register page $next_page = 'register.php'; // Add error message to the query string $query_string .= '&error=' . $error; // This message asks the server to redirect to another page header('Location: http://' . $server_dir . $next_page . $query_string); } // If Ok then go to confirmation else $next_page = 'confirmation.php'; /* Here is where the PHP sql data insertion code will be */ // Redirect to confirmation page header('Location: http://' . $server_dir . $next_page . $query_string); ?>
confirmation.php:
<?php # confirmation.php $user_name = $_GET['user_name']; ?> <html> <body> <h3>Congratulations <?php echo $user_name; ?>, you have been successfully registered</h3> <a href="./index.php"><h4>Back to Home</h4></a> </body> </html>
3 Responses So Far:
good.....
let do it
thanks before :D
I'm trying to learn PHP programming for our thesis. We're tasked to generate websites for certain companies. These codes are definitely useful for our project. Thank you. =)