Here we are discussing how to create simple login form in PHP and MySQL. You have to follow below basic steps for that.
Before starting, you should know how to use PHPMyAdmin and some basic knowledge of PHP.
Index
Create a database in PHPMyAdmin. Create Table using id, username, email, password fields. In my case, I created a table name ‘registration’.
Create a simple page with the name registration.php and paste the following code in it.
<?php
session_start();
// connect to the database
$db = mysqli_connect("localhost","username","password","database_name");
if(!$db){
die("connection error...".mysqli_connect_error());
}else{
echo "You are successfully connected.";
}
if(isset($_POST['username']) && isset($_POST['password'])){
$username=$_POST['username'];
$email=$_POST['email'];
$password=$_POST['password'];
$temp = mysqli_query($db,"INSERT INTO registration (username,email,password)
VALUES ('$username','$email','$password')");
if(!$temp){
echo "error";
}else{
echo "Your registration is done.";
}
}
?>
<html>
<head>
<title>Registration</title>
</head>
<body>
<div class="container">
<form class="form-signin" method="POST" name="registration">
<h2 class="form-signin-heading">Please Register</h2>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">Username</span>
<input type="text" name="username" id="username" class="form-control" placeholder="Username" required>
</div>
<label for="inputEmail" class="sr-only">Email ID</label>
<input type="email" name="email" id="email" class="form-control" placeholder="Email address" required autofocus>
<br>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="password" class="form-control" placeholder="Password" required>
<br>
<input name="submit" type="submit" value="Register" />
<a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a>
</form>
</div>
</body>
</html>
Create a simple page with name login.php and paste the following code in it
<?php
session_start();
// connect to the database
$db = mysqli_connect("localhost","username","password","database_name");
if(!$db){
die("connection error...".mysqli_connect_error());
}else{
echo "You are successfully connected.";
}
session_start();
// after form submitted insert values in to tables.
if (isset($_POST['username'])){
// removes backslashes
$username = stripslashes($_REQUEST['username']);
//escapes special characters in a string
$username = mysqli_real_escape_string($db,$username);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($db,$password);
//Checking for user already exist in the table or not
$query = "SELECT * FROM `registration` WHERE username='$username'
and password='$password'";
$result = mysqli_query($db,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if($rows==1){
$_SESSION['username'] = $username;
// Redirect user to index.php
header("Location: index.php");
}else{
echo "<div class='form'>
<h3>Username OR Password is incorrect.</h3>
<br/><a href='login.php'>Login</a></div>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
</head>
<body>
<div class="form">
<h1>Log In</h1>
<form action-xhr="#" method="post" name="login">
<input type="text" name="username" placeholder="User Name" required>
<br>
<input type="password" name="password" placeholder="Password" required>
<br>
<input name="submit" type="submit" value="Login">
</form>
<p>Not registered yet? <a href='registration.php'>Register Here</a></p>
</div>
</body>
</html> Create a simple page with name index.php and paste the following code in it
<!DOCTYPE html>
<html>
<title>Index</title>
<body>
<h1>You are logged in...</h1>
</body>
</html> There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…