-1
<!DOCTYPE html>

<html lang = "en">

    <meta name = "viewport" content = "width = device-width, initial-scale = 1.0">

    <meta charset = "UTF-8">
        
    <title>
        PHP Project
    </title>

    <p align = "centre">

        <form action = "LoginPage.php" method = "POST">

            <b>

                <i>
                    Username: <input required = "required" type = "text" name = "Username" placeholder = "Enter Username"> 
                </i>

            </b>

            <br>

            <br>

            <b>

                <i>
                    Password: <input required = "required" type = "password" name = "Password" placeholder = "Enter Password">
                </i>

            </b>

            <br>

            <br>

            <input type = "submit" name = "Login" value = "Login">

        </form>

    </p>

    <?php

        include "DataBase.php";
        
        error_reporting(0);
        $password = $_POST [ "Password" ];
        $username = $_POST [ "Username" ];

        echo"<b>";

            if ($username == Null || $password == Null)

                {

                    echo "________________________________________________Please enter your 
                    Username and Password to Log In into the system.________________________________________________";

                }           
            
            else if ($username != "CWIT" || $password != "Admin@194006")                    
        
                {

                    echo "________________________________________________Invalid Credentials! 
                    Please enter the correct Username and Password.________________________________________________";

                }

        echo"</b>";

    ?>   

</html>

If the username = CWIT and password = Admin@194006 I want my application to go to HomePage.php How do I do that?

Following is the code in the file I have included DataBase.php

<!DOCTYPE html>

<html>

  <?php

    echo "<b>";

      error_reporting(0);

      $servername = "localhost";
      $username = "root";
      $password = "";
      $database = "attendance";

      $connect = mysqli_connect($servername, $username, $password, $database);

      if (!$connect)

        {

          die ("___________________________________Failed to establish a connection: ".mysqli_connect_error().
          ".___________________________________");

        }

      else

        {

          echo "___________________________________________The details will be recorded 
          as a connection to the database has been established.___________________________________________<br> <br>";

        }

      echo "</b>";

  ?>

</html>

I tried adding code in the if else block to link the loginpage to the homepage if the username and password were correct but that didn't work.

vee
  • 4,506
  • 5
  • 44
  • 81
  • 1
    Does this answer your question? [PHP header(Location: ...): Force URL change in address bar](https://stackoverflow.com/questions/7467330/php-headerlocation-force-url-change-in-address-bar) ... simply add a location header after a successful validation. N.B. Your database code doesn't seem to be relevant here. – Markus AO Dec 24 '21 at 13:51
  • Please don't use all **bold** text. Are you sure that is your code in **DataBase.php** file? The connection file in PHP should contains only PHP with connection code without any HTML. You should enable and display all errors on all level to let PHP tells you what is incorrect and should be improve. Disable errors while coding is bad practice. – vee Dec 24 '21 at 14:15
  • Move the code that is processing part to the top before any output to browser. This can prevent many errors such as _header already sent_ and then the output part is at bottom. OR Use output buffer to prevent that but I don't recommend it. – vee Dec 24 '21 at 14:18

1 Answers1

0

If you already stored the username and password in database. you need to check the username and password in table using mysql script.

Otherwise you can check the username and password by if condition using php script as per below.

  if ($username == "CWIT" && $password == "Admin@194006")
{
 header("location:HomePage.php");

}
Sazal Ahmed
  • 97
  • 1
  • 10