PHP HTML Login Script | PHP Scripts | Login Script Code

PHP Login Script


Let's get into detail how to create PHP login more functionally. The following tutorial will provide you each and every step guidance on how to create a secure PHP Login Script and its prerequisite.

Before creating a PHP Login Script, there is a need for a database connection to store all the user's information.

Following is the code for connecting database file with a login form. dbconnect.php is the file with a database connection which needs to be included in a login page header.


global $db;
$GLOBALS['db'] = mysqli_connect('localhost', 'root', '', 'students');


Following is the code for fetching values of the login page through post method. Here, login.php is the file with fetching values such as email id, password, and login functions.

include ('dbconnect.php');

<head> </head>
<section class="bg-grey">
<div class="main">
<div class="container">
<div class="col-md-12">
<div class="form-content-box">
<div class="login-header">
<h3 class="text-center"> Login </h3>
<div class="details">
<form action="" method="post">
<div class="form-group">
<input type="email" name="email" placeholder="Enter email" autocomplete="off" required
class="form-control" pattern="[a-z0-9._%+-]+@[a-z.-]+\.[a-z]{2,3}$">
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="Password" required>

<div class="form-group">
<button type="submit" name="login" class="btn btn-submit">login</button>

<div class="login-footer">
<div class="clearfix"></div>


function user_login($data){
$email = $data['email'];
$password = md5($data['password']);
$login_qry = "select * from users where email='$email' AND password='$password'";
$login=mysqli_query($GLOBALS['db'],$login_qry) or die ("mysql error2".mysqli_error($GLOBALS['db']));

echo '<script type="text/javascript">window.location.href="home.php";</script>';

echo '<script type="text/javascript">alert("Please Enter Correct Username and Password");</script>';

Output of the above code will be as below:
When we enter wrong userid/password:

Now try with the correct userid/password:

After Successfully Login, you will redirect on Home Page:

For any query or technical help, we always available for support.

Best WordPress Hosting


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price

Leave a Reply

    Waiting for your comments