We will create a simple sign-in form and sign-up form using PHP MySQL.
Sign in and sign up form include following file and folder
folder:- Registration
_File:- signin.php
_File:- signup.php
_File:- home.php
First, we will create the database table
Following query used to make database table.
Database Table
CREATE TABLE IF NOT EXISTS `signupintab` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
Signup.php
Now, we will create a sign-up form. The user enters the information that stores in the database.
We will create a signup.php file that allows users to sign-up.
<!-- sign in and sign up form in php -->
<?php
session_start();
$conn=mysqli_connect("localhost","root","");
$db=mysqli_select_db($conn,"signupindb");
if(isset($_POST['submit'])){
$fname=$_POST['txtfname'];
$lname=$_POST['txtlname'];
$email=$_POST['txtemail'];
$pass=md5($_POST['txtpass']);
$query="insert into signupintab(firstname,lastname,email,password)values('$fname','$lname','$email','$pass')"or die('could not connect');
$result=mysqli_query($conn,$query);
header('location:signin.php');
}
?>
<html>
<head>
<title>Sign Up Form</title>
<style>
body{display:flex;
justify-content:center;
align-items:center;
}
.container{background-color:lightblue;
color:white;padding:35px;background-image:url(ccc.jpg)
}
input[type=submit]{background-color:red;color:white;}
</style>
</head>
<body>
<div class="container">
<h1>Sign Up</h1>
<h4><span style="color:red">*</span>Required</h4>
<form method="POST"action="">
First Name<span style="color:red">*</span><br>
<input type="text"name="txtfname"placeholder="Enter First Name"required><br>
<p>
Last Name<span style="color:red">*</span><br>
<input type="text"name="txtlname"placeholder="Enter Last Name"required><br>
<p>
Email<span style="color:red">*</span><br>
<input type="email"name="txtemail"placeholder="Enter Email Id"required><br>
<p>
Password<span style="color:red">*</span><br>
<input type="password"name="txtpass"placeholder="Enter Password"required><br>
<p>
<input type="submit"name="submit"value="Sign Up"><br>
<br>
Already account <a href="signin.php">Sign In</a>
</form>
</div>
</body>
</html>
Live Demo
signin.php
The user completes the sign-up then the user reaches the redirected login page. Here users enter email ID and password to log in.
You may also like
Then the user will click on submit button. If the user email and password do not match the database, an error occurs and the login fails.
We will create a signin.php file to log in to the user and create a link. If there is a new user, then clicking on the link redirects to the sign-up page.
<! sign in form in php -->
<?php
$conn=mysqli_connect("localhost","root","");
$db=mysqli_select_db($conn,"signupindb");
session_start();
if(isset($_POST['submit'])){
$email=$_POST['txtemail'];
$pass=md5($_POST['txtpass']);
$query="select *from signupintab where email='$email'and password='$pass'"or die('could not connect');
$result=mysqli_query($conn,$query);
$row=mysqli_fetch_row($result);
if($row>0)
{
$_SESSION['email']=$email;
echo"<script>alert('successfuly login');</script>";
header('location:home.php');
}else{
echo"<script>alert('something wrong try again');</script>";
}
}
?>
<html>
<head>
<title>sign in form</title>
<style>
body{display:flex;
justify-content:center;
align-items:center;}
.container{background:lightblue;padding:30px;color:white;background-image:url(ccc.jpg)}
input[type=submit]{background-color:red;color:white;}
</style>
</head>
<body>
<div class="container">
<h1>Sign In</h1>
<form method="POST"action="">
Email<br>
<input type="email"name="txtemail"placeholder="Enter Email Id"required><br>
<p>
Password<br>
<input type="password"name="txtpass"placeholder="Enter Password"required><br>
<p>
<input type="submit"name="submit"value="Sign In"><br>
No Account <a href="signup.php">Sign Up</a>
</form>
</div>
</body>
</html>
Live Demo
home.php/logout page
If the user has successfully login, the user will be redirected to the home page.
We will create a home.php file to redirect after signing in successfully.
Also, we will put a logout button to log out the user. After clicking the logout session, it will redirect the user back to the sign-in page.
<!-- home.php or logout.php file -->
<?php
session_start();
if(isset($_SESSION['email'])==0){
header('location:signin.php');
}
session_destroy();
?>
<a href="signin.php">Logout</a><p>
hello home page
Comments
Post a Comment