PHP Login System With
MySql Database | PHP Login System.
Hello Readers...
In this article we are
going to create a login system using HTML and PHP.
-First create a separate config.php file to create the connection with MySql
Database.
-If we create a config.php file separately then we can use this file in all PHP pages
where connection is required i.e.,we can reuse this file n number of times for
Database Connection Purpose.
-Now create login.php file,this file will be responsible to to
authenticate the users credentials and redirect then to welcome page.
-Assume our Database
name is mysql@123 and Table name is login with username and password column.
So lets create config.php.
This file is
responsible to establish the connection with MySql Database.
config.php
<?php
/* Assume we are running
MySQL with default setting (user 'root' with no password) */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'mysql@123');
/* Attempting to connect
to MySQL database */
$con = mysqli_connect(DB_SERVER,
DB_USERNAME, DB_PASSWORD, DB_NAME);
// Checking connection
if($con === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
Now create a login.php file.
Here we are going to
write PHP login logic inside login.php only.
In this file we have created a form with two text-field i.e., username and password to take input from the user's.
We have also created a submit button to submit the form after filling username and password.
When user hit the submit button after filling username and password then this login.php is responsible to authenticate the user credentials.
If user enter valid username and password then user will be redirected to welcome page otherwise a message will be displayed saying invalid login details,see below example.
login.php
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$username=$_POST['username'];
$password=$_POST['password'];
include('config.php');
$sql_query = "select
count(*) as cntUser from login where
username='".$username."' and
password='".$password."'";
$result = mysqli_query($con,$sql_query);
$row = mysqli_fetch_array($result);
$count = $row['cntUser'];
if($count > 0){
echo "
<script>
alert('Success...');
function redi(){window.location='welcome.php';}
document.write('<font color=blue>'+'please wait while
redirecting to
Welcome page...'+'</font>');
setTimeout('redi()',3000);
</script>";
}
else{
echo
"
<script>alert('Invalid Login
Details...');
</script>";
}
}
?>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<center>
<form
method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Username :<input type="text" name="username" id="username" required="required"/><br/>
Password :<input type="password" name="password" id="password" required="required"/><br/>
<input type="submit" value="Login"/>
</form>
</center>
</body>
</html>
Now create welcome.php.
If user entered valid
username and password then login page will redirect the user's to this welcome.php
welcome.php
<html>
<head>
<title>
Welcome Page
</title>
</head>
<body><br/><br/>
<center>
Welcome User to our Welcome Page.
</center>
</body>
</html>
Hope this article was
helpful.

No comments