Creating a simple user login system with PHP and MySQL database
Every website has a login and registration page. This is a simple and user-friendly and beginner registration and login form.
We will create simple starting point registration functionality using the PHP MySQL database.
In this tutorial, we will create a simple user login and registration system using the PHP MySQL database. In this tutorial, we'll create five PHP files.
Database name:-slogin
Database table name:- user
we will create the following PHP file.
config.php
index.php
registration.PHP
home.php
style.css
Create config file to connect database in PHP MYSQL.
After creating database connections, we need to create a PHP index page that the user first opens the index page. On the index page, we will create two button login and registration using CSS and HTML.
If the user already registered then, he would click on the sign-in button and the new user click on the registration form.
Create the following index file code.
index.php
Every website has a login and registration page. This is a simple and user-friendly and beginner registration and login form.
We will create simple starting point registration functionality using the PHP MySQL database.
In this tutorial, we will create a simple user login and registration system using the PHP MySQL database. In this tutorial, we'll create five PHP files.
Database name:-slogin
Database table name:- user
we will create the following PHP file.
config.php
index.php
registration.PHP
home.php
style.css
First, we will create a database file in the MySQL database.
You execute the following SQL query to access the User's table inside your MySQL database.
slogin.sql database table
CREATE TABLE IF NOT EXISTS `user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(110) NOT NULL,
`email` varchar(110) NOT NULL,
`password` varchar(110) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
User registration inserted values in a table example
1
2
| INSERT INTO `user` (`id`, `name`, `email`, `password`) VALUES
(1, 'admin', 'test@123gmail.com', '21232f297a57a5a743894a0e4a801fc3');
|
config.php
<?php
$server='localhost';
$dbname='slogin';
$user='root';
$pass='';
$conn=mysqli_connect($server,$user,$pass,$dbname);
if(!$conn){
die('dababase could not connect'.mysqli_error());
}
$result=mysqli_select_db($conn,$dbname);
?>
Create config file to connect database in PHP MYSQL.
After creating database connections, we need to create a PHP index page that the user first opens the index page. On the index page, we will create two button login and registration using CSS and HTML.
If the user already registered then, he would click on the sign-in button and the new user click on the registration form.
You may also like
We will create a session to store the user names in the session.
Create the following index file code.
index.php
<html>
<head>
<style>
body{background:lightblue}
.div a{color:white;font_size:44px}
a{font-size:25px;float:right;padding:10px;text-decoration:none;}
</style>
</head>
<body>
<div class="div">
<div><a href="login.php">sign in</a></div>
<div><a href="regitration.php">sing up</a></div>
</div>
</body>
</html>
Then, we'll create a registration page. We will get useful information and store it in the MySQL database.
In the registration form, we'll create simple information filled that users register and then login.
We will store user text box values like username, email id, and password.
We'll create, validate a password you fill minimum and maximum limit values in password fill.
If the user enters less than four characters, then an error shall be generated and a user enters the password above eight, Then error's shall be generated. If user registration without entering value, then error generated.
If the user already registered, then we will create the link through the user's clicking the link and redirect jump to login page.
Also, we will use to password encryption PHP function.
registration.php
Then, we'll create a simple login, from where users can enter their username and password. If a username and password do not, on the database, then the error message display.
If username is true and password is wrong, generate error and login fail.
If New user, then we will create a registration link to direct a user to click and go to the registration form.
Let's create a login file and place the following PHP code.
<?php
include'config.php';
if(isset($_POST['save']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$password=md5($_POST['password']);
$query="insert into user(name,email,password)values('$name','$email','$password')"or die('could not connect');
$result=mysqli_query($conn,$query);
header('location:login.php');
}
?>
<html>
<head>
<link rel="stylesheet"type="text/css"href="style.css">
</head>
<body>
<form action="regitration.php"method="POS>
<h2>Registration</h2>
<tr>
<td>Name</td><br>
<td><input type="text"name="name"placeholder="Your Name"required><br></td>
</tr>
<tr>
<td>Email</td><br>
<td><input type="email"name="email"placeholder="Your Email id"required><br></td>
</tr>
<tr>
<td>Password</td><br>
<td><input type="password"name="password"placeholder="Your Password"minlength="4"maxlength="8"required><br></td>
</tr>
<tr>
<td></td><br>
<td><input type="submit"name="save"value="Register"></td>
</tr>
already account <a href="login.php">sign in</a>
</form>
</body>
</html>
<?php
session_start();
include'config.php';
if(isset($_POST['login']))
{
$name=$_POST['name'];
$password=md5($_POST['password']);
$query="select * from user where name='$name' and password='$password'";
$result=mysqli_query($conn,$query);
$data=mysqli_fetch_row($result);
if($data>0){
$_SESSION['name']=$name;
header('location:home.php');
}else{
echo"<script>alert('try again')</script>";
}
}
?>
<html>
<head>
<link rel="stylesheet"type="text/css"href="style.css">
</head>
<form action="login.php"method="post">
<h2>Login</h2>
<tr>
<td>Name:-</td><br>
<td><input type="text"name="name"placeholder="Your Name"required><br></td>
</tr>
<td>Password</div><br>
<td><input type="password"name="password"placeholder="Your Password"maxlength="8"minlength="4"required><br></td>
</tr>
<td></td><br>
<td><input type="submit"name="login"value="login"></td>
</tr>
No account <a href="regitration.php">Regitration</a>
</form>
</body>
</html>
Then after We will create a home page. Login user redirects home page.
On the home page, we will create a logout button.
On the Home page, we will create the session destroy function even if the user click on the logout button user redirect an index.PHP page.
home.php
<?php
session_start();
if(!isset($_SESSION['name'])){
header('location:index.php');
}
session_destroy();
?>
<html>
<head>
<style>
body{background:#999;display:flex;align-items:center;justify-content:center;}
a {color:white;top:40px;
align-self: end;
margin-left: 950px;
text-decoration: none;font-size:20px;}
</style>
<body>
<ol>
<li>free cource</li>
<li>free cource download</li>
<li>free video download</li>
<li>free ebook download</li>
</ol>
<a href="home.php">Logout</a>
</body>
</html>
In the last, we create a simple CSS file to some simple registration form and login form designing.
style.css
body{align-items:center;display:flex;justify-content:center;background:#999;}
form{width:250px;border:solid white;padding:30px;background:lightblue;}
*{font-family:sants-serif;box-sizing:border-box;}
a{text-decoration:none;}
If you have any questions, you can ask me by contacting or commenting.





Comments
Post a Comment