All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Login Page In PHP With Database Source Code

Last Updated : Mar 11, 2024

Login Page In PHP With Database Source Code

In this tutorial we will show you the solution of login page in PHP with database source code, today we are going to understand how to create a login page in php with database source code.

In previous tutorial, we have done how to create a registration page in php with database.

Step By Step Guide On Login Page In PHP With Database Source Code :-

Here, as given below we create a page named login. Php and inside that page we use database, jQuery, some html also.

One thing to note here that we link a file that is if CSS, which we did not provide in this example, don’t worry about that its only for designing purpose. Now, let see the codes.

// login.php
<?php session_start();
    $connect=mysqli_connect("localhost","root","","index") or die("Connection Failed");
    if(!empty($_POST['button']))
    {
        $name=$_POST['username'];
        $email=$_POST['email'];
        $pass=$_POST['password'];
        if($email== "" && $pass == "" && $name=="")
        {
            echo "<script> alert('Please fill all fields.......')</script>";
        }
        else
        {
            $query= "select * from rl_form where email='$email' and pass='$pass' and user='$name'";
            $result=mysqli_query($connect, $query);
            $count= mysqli_num_rows($result);
            if($count>0)
            {
                setcookie("username",$name);
                header("location:welcome.php");
                // echo "<script> alert('Congratulations! you are logged in .......')</script>";
            }
            else
            {
                echo "<script> alert('Username/Email and Password does not match.......')</script>";
            }
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="main">
        <div class="inner">
            <div class="welcome">
                Login Form
            </div>
            <div class="login_options">
                <form action="" method="post">
                    <div class="lleft_body">
                        <label for="">
                            Enter your username here
                        </label>
                        <br>
                        <label for="">
                            Enter your email here
                        </label>
                        <br>
                        <label for="">
                            Enter your password here
                        </label>
                    </div>
                    <div class="lright_body">
                        <input type="text" name="username" id="">
                        <br><br>
                        <input type="email" name="email" id="">
                        <br><br>
                        <input type="password" name="password" id="">
                    </div>
                    <div class="login_submit">
                        <input type="submit" value="Login" id="login" name="button">
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, as we can see that we create a login page in php. First of all the database which we use in this article is index and inside that our table name is rl_form.
  6. After that in body you will see we create three inputs first for name, next for email and last for password. This all data is going to check through database.
  7. And we use some jQuery codes also here. We hope that you understand this article properly.
  8. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to create a login page in php with database source code.

I hope this tutorial on login page in PHP with database source code helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪