Create a User Login System in PHP with MySQL Database

Guys, In Websites or Web Pages, Login System isn’t simple and not particularly much difficult.Today I Will Create of login System (Example) for all’s.I Will Create a Login System in PHP with MySQL Database utilizing Session  and we use xampp  and phpMyAdmin .

With My Opinion Creating of Login System, Specially First We Need a Database in light of the fact that without database you probably won’t be completely used login system or page in sites or website    pages, as in login system you do provide input data, 

For example, username , secret key, password  and so on , server or system can’t verify the your input data without database and its gets error or warning if utilize brief area since system initially check input data whose you gave from database, so its essential. 

After That Good and Simple Code is needed. You Can Also Try Advanced Code But not utilize Complex Code. In PHP Programming, Many Codes are accessible to Login System whose that keeps running on various distinctive conditions and Platforms, Ways. So I Will Create the ONLY Login System in The Easiest Way.

Example
Download
CREATE TABLE `usrlogin` (`id` INT(111) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`usrname` VARCHAR(111) NOT NULL UNIQUE ,`password` VARCHAR(111) NOT NULL , 
created_at DATETIME DEFAULT CURRENT_TIMESTAMP) ENGINE = InnoDB;
/* INSERT Just demo values */
INSERT INTO `user` (`usrname`, `password`) VALUES ('admin', 'pass123');
Example
Download
<?php
include('login.php');
?> 
<style type="text/css">
body{
font-size:14px;
background:url(images/back.jpg) no-repeat;
font-family:"Courier New", Courier, monospace;
height:100%;
width:100%;
}
.form input[type='password'],
.form text,
.form select,
.form textarea,
.form input[type='text']
{
 width:100%;
 border: 1px solid #eee;
 padding:10px;
 margin-bottom:5px;
 outline:none;
 display:block;
 resize:vertical;
}
.form input[type='submit']
{
 margin-top:5px;
 margin-bottom:5px;
}
.widget
{
 margin-bottom:10px;
 width:460px;
 height:280px;
 margin-left:400px;
 background-color:#fff;
}
.widget .widget-heading{
background:#F9F7F7;
border: 1px solid #eee;
padding:10px;
font-weight:bold;
text-align:justify;
}
.widget .widget-contents
{
padding:10px;
border-bottom: 1px solid #eee;}
.button
{
 width: 100px;
 height: 32px;
 color:#39F;
 border: medium none;
 font-size: 14px;
 margin: 24px auto;
 text-transform: uppercase;
 }
</style>
<title>Login Panel</title>
</head>
<body>
<div class="widget">
<div class="widget-heading">Login Panel</div>
<div class="widget-contents">
<form id="login" action="login.php" class="form" method="post"  enctype='multipart/form-data'/><fieldset>
<div>
<label>Username</label>
<input type="text" name="usrname" />
</div>
<div>
<label>Password>/label>
<input type="password" name="password" />
</div>
<div>  
<input type="submit" value="Login" class="button"/></div
<span><?php echo $userpass_err; ?></span>
</fieldset>
</form> 
 </div>
</div>
Example
Download
<?php
session_start();
 if(isset($_SESSION['user'])){
	header("location: welcome.php"); // Redirecting To Welcome Page
    exit;
}
// Define Variables To Store Error Message and initialize with empty values
 $username = $password = "";
 $userpass_err = '';

 // Processing form data when login form is submitted
 if($_SERVER["REQUEST_METHOD"] == "POST"){
  
   // Checking if Username & Password is not Empty 
	if(!empty($_POST['usrname']) && !empty($_POST['password'])){
	
	// Define $username and $password 
	$username = $_POST['usrname'];
    $password = $_POST['password'];
 
 	// Connecting to MySQL Database Server By Using mysqli_connect function 
    $connect = mysqli_connect("localhost","root","") or die ("could not connect");
    // Selecting Database into MySQL Server (phpMyadmin)
    mysqli_select_db($connect,"usrlogin") or die ("could not find database");
			
    //Sql Query To fetch information of registerd users and finds user match. 
	$sql = "SELECT usrname, password from usrlogin where usrname=? AND password=? LIMIT 1";
	
	// To protect MySQL injection for Security purpose 
    $result = mysqli_prepare($connect,$sql);
	mysqli_stmt_bind_param($result,"ss", $username, $password);
	mysqli_stmt_execute($result);
	mysqli_stmt_bind_result($result,$username,$password);
	mysqli_stmt_store_result($result);
	if(mysqli_stmt_fetch($result) == false)
	{
	  echo "Invalid Username or Password Check Again";
	}
	else
	{
	   // Store data in session variables  
		$_SESSION['user'] = $username;
	 // Redirect user to welcome page	
     	header("location:welcome.php");
		exit(); 
	}
		//Closing Sql Connection
        mysqli_close($connect);
		//Close statement
		mysqli_stmt_close($result);
		
	}
	else {
		$userpass_err = "Enter The Username or Password or Both";
		echo "$userpass_err";
	}
 }
?>
Example
Download
<?php
include('session.php'); 
// Check if the user is logged in, if not then redirect him to login page
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1><center>Welcome To Rajindera Comp</center></h1>
<h2>center>You Successfully Login</center></h2>
<center>
<img src="images\01.png"  align="bottom"  height="200px;" /></center>
</body>
</html>
Example
Download
<?php
// Connecting to MySQL Database Server By Using mysqli_connect function 
   $connect = mysqli_connect("localhost","root","") or die ("could not connect");
 // Selecting Database into MySQL Server (phpMyadmin)
    mysqli_select_db($connect,"usrlogin") or die ("could not find database"); 
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysqli_query($connect, "select usrname from usrlogin where usrname='$user_check'");
$row = mysqli_fetch_assoc($ses_sql);
$login_session =$row['usrname'];
 // Closing Connection
mysqli_close($connect);
?>

Leave a Reply