How to creating a registration and login form in PHP MYSQL database

First, we are creating a user account.
We will need to collect a minimal amount of information from the user.
We will collect the full name, email address and password of the user.

Furthermore, we will use the PHP MySQL database.

First, we will create a database in PHP MySQL database
we will create a database, regidb. Table structure for table `regitab`


CREATE TABLE IF NOT EXISTS `regitab` (

  `u_id` int(8) NOT NULL AUTO_INCREMENT,
  `u_fullname` varchar(50) NOT NULL,
  `u_email` varchar(50) NOT NULL,
  `u_password` varchar(50) NOT NULL,
  PRIMARY KEY (`u_id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;




Dumping data for table `regitab`
INSERT INTO `regitab` (`u_id`, `u_fullname`, `u_email`, `u_password`) VALUES

(1, 'robort', 'text123@gamil.com', '25d55ad283aa400af464c76d713c07ad');



PHP Scripts connect to the MYSQL database.
config.php
<?php 

$server='localhost';

$user='root';

$dbname='regidb';

$pass='';

$conn=mysqli_connect($server,$user,$pass,$dbname);

if(!$conn)

{

die('could not connect');

}

?>


First, we will create an index.php page.

On an index page, we will create a hyperlink to log in and Sign Up.


Index.php
<html>

<head>

<style>

body{background-image:url(img/background.png);background-repeat:no-repeat;background-position:center;background-size:cover;background-color:black;}

a{float:right;margin:10px;text-decoration:none;}

h1{background-color:red;text-align:center;}

</style>

<body>

<a href="login.php">| Login</a>

<a href="regi.php">Sign Up | </a>

<h1> Learn PHP free</h1>

</body>

</html>
Note: Replace image 


If the User did Not register, then the user redirect registration.
In registration, we will store the user's full name, email address and password in the database.

regi.php

Registration form in php

You may also like 

<?php 

error_reporting(0);

include'config.php';

$fnameE=$emailE=$passE="";

session_start();

if($_SERVER['REQUEST_METHOD']=='POST'){

$fname=$_POST['txtfname'];

$email=$_POST['txtemail'];

$pass=md5($_POST['txtpass']);


if(empty($fname) || empty($email) || empty($pass)){

if(empty($fname)){

$fnameE="Name is required";

}

if(empty($email)){

$emailE="Email is required";

}

if(empty($pass)){

$passE="Password is required";

}

}else{

$query="insert into regitab(u_fullname,u_email,u_password)values('$fname','$email','$pass')"or die('could not connect');

$result=mysqli_query($conn,$query);

header('location:login.php');

}

}

?>


<html>

<head><title>Registartion form</title>

<style>

body{align-items:center;justify-content:center;display:flex;}

.container{background-image:url(img/container.jpg);background-position:center;background-size:contain;padding:20px;}

img{margin-left:auto;margin-right:auto;display:block;

height:150px;

width:150px;

border-radius:50%;
}

input{height:30px;width:250px;}

input[type=submit]{

background-color:blue;color:white;}

</style>

</head>

<body>

<div class="container">

<h1>Registration Form</h1>

<img src="img/avatarl.png">

<h4>Required field<span style="color:red">*</span></h4>

<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">

<b>Full Name</b><span style="color:red">*<?php echo $fnameE;?></span><br>

<input type="text"name="txtfname"><br>

<p>

<b>Email Address</b><span style="color:red">*<?php echo $emailE;?></span><br>

<input type="email"name="txtemail"><br>

<p>

<b>Password</b><span style="color:red">*<?php echo $passE;?></span><br>

<input type="password"name="txtpass"><br>

<p>

<input type="submit" name="submit"value="Sign Up"><br>

Already registered <a href="login.php">Sign in </a>

</form>

</div>

</body>

</html>

Note: Replace image 

If the User has already registered, then the user direct login.
login.php
Login form In PHP
<?php

include'config.php';

$emailE=$passE="";

session_start();

if($_SERVER['REQUEST_METHOD']=='POST'){

$email=$_POST['txtemail'];

$pass=md5($_POST['txtpass']);

if(empty($email) || empty($pass)){

if(empty($email))

{

$emailE="Email is Required";

}

if(empty($pass)){

$passE="Password is Required";

}

}else{

$query="select *from regitab where u_email='$email' and u_password='$pass'";

$result=mysqli_query($conn,$query);

$row=mysqli_fetch_array($result);

if($row>0){

$_SESSION['u_email']='$email';

echo"<script>alert('login successfully');</script>";

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

}else{

echo"<script>alert('something wrong try again');</script>";

}

}

}

?>

<html>

<head><title>login form</title>

<style>

body{align-items:center;justify-content:center;display:flex;}

.container{background-image:url(img/container.jpg);background-position:center;background-size:contain;padding:20px;border:1px solid;}

img{margin-left:auto;margin-right:auto;display:block;

height:150px;

width:150px;

border-radius:50%;

border:1px solid;

}

input{height:30px;width:250px;}

input[type=submit]{

background-color:blue;color:white;}

h1{text-align:center;}

</style>

</head>
<body>

<div class="container">

<h1>Login form</h1>

<img src="img/avatarr.png">

<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">

<b>Email address</b><span style="color:red"><?php echo $emailE;?></span><br>

<input type="email"name="txtemail"><br>
<p>

<b>Password</b><span style="color:red"><?php echo $passE;?></span><br>

<input type="password" name="txtpass"><br>

<p>

<input type="submit"name="submit"value="Login"><br>

Not register <a href="regi.php">Sign up</a>

</form>

</div>

</html>
Note: Replace image 


After successful login, user redirects to the home page.

home.php

<?php 

session_start();

if(!isset($_SESSION['u_email'])){

header('location:index.php');

}

session_destroy();

?>


<html>

<head><title>Home</title>

<style>

body{background:maroon;margin:0; padding:0}

h1{text-align:center;display:flex;justify-content:center;align-items:center;}

</style>

</head>

<body>

<a href="index.php">Logout</a>

<h1>Welcome to PHP video cource</h1>

</body>

</html>


Live Demo
Registration form


Live Demo
User Login Form




Comments