PHP Login Script

by Anu Gaba 28-Nov-17

0 5091


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.

<?php

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

?>
Login.php
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.

<?php
include ('dbconnect.php');
if(isset($_POST['']))
{
user_login($_POST);
}
?>

<html>
<head> </head>
<body>
<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>
<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>
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="Password" required>
</div>

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

<div class="login-footer">
</div>
</div>
</div>
</div>
</div>
<section>
<div class="clearfix"></div>
</body>
</html>

<?php

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']));
$result=mysqli_fetch_array($login);
if(mysqli_num_rows($login)>0)
{
$_SESSION['user']['id']=$result['id'];

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

}
else
{
$_SESSION['user']['id']="";
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:

PHP Login Script

Now Try with the correct userid/password:
PHP Login Script

After Successfully Login, you will redirect on Home Page:
PHP Login Script

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


Share:


Comments

    No Comments Yet.

Leave a Reply