Question

I coded a registration form and after every input has been validated , it sends some data to the database and redirects to the login page.

I want to display a message on the login page tho like : Registration Successful - Login Here

if (!validated() {
    // post error messages

} else {
    echo '
       <div class="container">
        <div class="page-header">
            <h3>Registration Complete</h3>
        </div>
       </div>';

    header( 'Location: ../login.php' );
}

Login file :

Header - Navbar

<div class="container">
   <div class="page-header">
       <h3>Login To Members Corner</h3>
   </div>

   <form>
       // login form
   </form>

Footer
Was it helpful?

Solution

You can't redirect to a file after output has been shown. You have two alternate solutions here:

  1. Refresh the page after a timeout while showing the message in the meantime, using a <meta> tag:

    <meta http-equiv="refresh" content="5; url=../login.php">
    
  2. Output the message after redirection:

    <?php 
    header('Location: ../login.php?registered=true');
    

    And in login.php:

    <?php
    if (@$_GET['registered'] == 'true')
        echo 'You have registered successfully.'
    

OTHER TIPS

You do a header("Location") to redirect the page. That is possible but than you want to add a paremeter where you can detect if the registration was successful. Something like:

header( 'Location: ../login.php?action=success' );

And than in you file do this:

if( $_GET['action'] == 'success' ) {
    echo "thanks for you registration, log in here";

}

Because if you redirect, the current request vars will not be available in the redirected page. So you need to pass them on to the next.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top