How to make a login page using PHP connecting it with the database? – Read Details

Note: you should have preinstalled the Xampp Server in your PC or You can use your hosting account also.

  • Firstly you need to make a file naming index.php
  • We need to make a login page for the following code is needed:
<html>
    <head>
        <title>Login Page - Shopeepoint</title>
    </head>
    <body>
        <form method="post" action="">
            <label>Username</label>
            <input type="text" name="username" placeholder="enter your username" required><br><br>
            <label>Password</label>
            <input type="password" name="password" placeholder="enter your password" required><br><br>
            <button type="submit" name="login">Login</button>
            <button type="reset">Clear</button>
        </form>
    </body>
</html>
  • Above code will look like this, when you will run it in your browser:
  • Now we need to code for connecting the form to data base:
<?php

    $host='localhost';
    $user='root';
    $password='';
    $db_name='blog';

    $con=mysqli_connect($host, $user, $password) or die(mysqli_error());
    mysqli_select_db($con,$db_name);

    session_start();

    if(isset($_POST['login']))
    {
        
    	$username=$_REQUEST['username'];
	   
	    $password=$_REQUEST['password'];
	
	    $sql = "SELECT * FROM tbl_login WHERE username='$username' AND password='$password'";
	
	    $res = mysqli_query($con,$sql);
	
	    $query=mysqli_fetch_array($res);
	    $id=$query['username'];
	    if($res)
	    {
		    $_SESSION['username']=$id;
		    echo "<script>
		    alert('Welcome ');
		    </script>";
	    }
	    else
	    {
		    echo "<script>
		    alert('Invalid Credentials');
		    </script>";
	    }
    }

?>

Note: Database credentials can be different in different server.

  • Now we need to make a database naming mentioned above in the code. For that, you need to login into your PHPMyAdmin.
  • Enter the database naming mentioned above in the code. For example ‘blog’.
  • Create a table naming mentioned above in the code. For example ‘tbl_login’.
  • After entering the name. You need to make the fields of the table like id, username & password.
  • After making the fields you need to insert data. By clicking on the ‘Insert’ option
  • After inserting the data you can check it by clicking on the ‘Browse’ option
  • Now you need to open that index.php file in your browser. In my case, I will enter localhost/blog/index.php, or if your open through your online hosting server then you need to type like this www.domainname.com/directory name/index.php.
  • Now click on Login Button.
  • We have not given any other page referencing if our username and password get matched. If you want to give then add the below code after the alert message printing the Welcome message.

window.location.href=’dashboard.php’;

1 thought on How to make a login page using PHP connecting it with the database? – Read Details

  • Saksham GoyalDecember 23, 2020 at 6:06 pm

    Awesomely presented tutorial . Very Helpful

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *