Redirecting to a new page after successful login

158,046

Solution 1

First of all, move all your PHP code to the top. Without it, my code below wont work.

To do the redirect, use:

header('Location: http://www.example.com/');

Also, please consider my advice. Since it's not the first question today and all your questions are related to basics, you should consider reading some good PHP book to understand how things work.

Here you can find useful links to free books: https://stackoverflow.com/tags/php/info

Solution 2

Javascript redirection generated with php code:

 if($match > 0){
     $msg = 'Login Complete! Thanks';
     echo "<script> window.location.assign('index.php'); </script>";
 }
 else{
     $msg = 'Login Failed!<br /> Please make sure that you enter the correct  details and that you have activated your account.';
 }

Php redirection only:

<?php
    header("Location: index.php"); 
    exit;
?>
Share:
158,046
Kaos
Author by

Kaos

Updated on July 18, 2022

Comments

  • Kaos
    Kaos almost 2 years

    I'm sure this question has been asked before but I have searched thoroughly for an answer but to no avail. (The only answers I've seen involve ajax) But I'm using just javascript, PHP and HTML.

    I have a login.php page and I have already created a HTML page which is to be the landing page right after a user is successfully logged in. How do I go about this?

    The following is my code for the login page and the landing page after the login is called transfer.html:

    LOGIN.PHP

      <div id="content">
         <h3>Login to Internet Banking</h3>
         <form id="login" action="" method="post">
            <p>
              <label for="userid">UserID:</label>
              <input type="text" name="UserID" id="UserID"/>
            </p>
            <p>
              <label for="PIN">PIN:</label>
              <input type="password" name="PIN" id="PIN" />
            </p>
    
            <p>
              <input type="submit" name="btnSend" value="Login" class="submit_button" />
    
            </p>
          </form>
          <td>&nbsp;</td>
     <p>
            Not yet registered?
     <a href="registration.php">Click here to register</a>
     </p>
    
      <div id="wrap">
            <!-- start PHP code -->
            <?php
    
                mysql_connect("localhost", "root", "") or die(mysql_error()); // Connect to database server(localhost) with UserID and PIN.
                mysql_select_db("registrations") or die(mysql_error()); // Select registration database.
    
                if(isset($_POST['name']) && !empty($_POST['name']) AND isset($_POST['PIN']) && !empty($_POST['PIN'])){
                    $UserID = mysql_escape_string($_POST['name']);
                    $PIN = mysql_escape_string(md5($_POST['PIN']));
    
                    $search = mysql_query("SELECT UserID, PIN, active FROM users WHERE UserID='".$UserID."' AND PIN='".$PIN."' AND active='1'") or die(mysql_error()); 
                    $match  = mysql_num_rows($search);
    
                    if($match > 0){
                        $msg = 'Login Complete! Thanks';
                    }else{
                        $msg = 'Login Failed!<br /> Please make sure that you enter the correct details and that you have activated your account.';
                    }
                }
    
    
            ?>
            <!-- stop PHP Code -->
            <?php 
                if(isset($msg)){ // Check if $msg is not empty
                    echo '<div class="statusmsg">'.$msg.'</div>'; // Display our message and add a div around it with the class statusmsg
                } ?>
    
        </div>
            </div>
    
  • Serge Kuharev
    Serge Kuharev about 11 years
    It won't work, since headers can't be set before any output from php script.
  • Tapas Pal
    Tapas Pal about 11 years
    Put your php script under if(isset($_POST['btnSend']) && $_POST['btnSend']=='Login') this codition.
  • Serge Kuharev
    Serge Kuharev about 11 years
    Once again, there should be no output. The first line of the file, "<div id="content">" is an output already, so all headers should be set before that line.
  • Tapas Pal
    Tapas Pal about 11 years
    then use your full site url like header("Location:website.com/home.php"); Hope it'll be work.
  • Kaos
    Kaos about 11 years
    Thanks guys, all your answers were very helpful. And I'll be taking all the advice.
  • Serge Kuharev
    Serge Kuharev about 11 years
    Please read php.net/manual/ru/function.header.php to understand what is headers and how to use them. It doesn't matter what link you put inside, the issue is that you can't set any headers if you have even single character of output before. This won't work: "test<?php header(...); ?> " ;while this will: "<?php header(...); ?>test"
  • Jack
    Jack over 8 years
    your answer was a divine gift for my project. Thank you good sir!
  • Gar
    Gar over 7 years
    Is this php or javascript ? could you check the syntax please ?
  • random_user_name
    random_user_name over 7 years
    This question was answered - and accepted - three years ago. What value does your new answer bring? Additionally, the formatting is incorrect, and your code is not valid PHP.
  • anandhu
    anandhu almost 4 years
    This answer worked. Is there any security issues for using this method?