PHP Create user Signup Registration page with Login script using MySQL database

How to make user authentication form with session and password encryption using PHP.

What is User SignUp form : User sign up from are used to provide facility to website user to become a part of particular website so its user can use more resources provided by website. Sign up form requires basic information about its user with minimum requirements. For example : first name, last name, male-female, username which asks for login time, password and also email. So here is the complete step by step tutorial for PHP Create user Signup Registration page with Login script using MySQL database.

What is login form : Login form display after sign up, login form basically requires username and password and any one who has the right username and password can login into any website. It will authenticate the user( Who enters the right username and password ) and allow them to use websites resources.

android-project-download-code-button

List of all files in this project :

  1. sign-up.php
  2. login.php
  3. dashboard.php
  4. logout.php
  5. dbconfig.php
  6. style.css

Login details:

Email = [email protected]

Password = sam

PHP Create user Signup Registration page with Login script using MySQL database.

Code for sign-up.php file.

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>User Registration page</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
</head>

<body>

<table bgcolor="#f2f2f2" style="padding:50px" align="center" width="550px">
<form action="" method="post">
<tr><td align="center" colspan="2"> <h1>Registration Page</h1> </td></tr>
<tr>
<td> First Name : </td><td><input type="text" name="Fname"></td>
</tr>
<tr>
<td> Last Name : </td><td><input type="text" name="Lname"></td>
</tr>
<tr>
<td> Email : </td><td><input type="email" name="email"></td>
</tr>
<tr>
<td> Password : </td><td><input type="password" name="password"></td>
</tr>

<tr>
<td align="center" colspan="2"><input type="submit" name="submit"></td> </tr>
<td align="center" colspan="2"><a href="login.php"><button type="button">Click Here To Login</button></a></td></tr>

</form>
</table>

<?php
if(isset($_POST["submit"]))
{
 
 //Including dbconfig file.
include 'dbconfig.php';
 
$fname = $_POST["Fname"];
$lname = $_POST["Lname"];
$email = $_POST["email"];
$password = $_POST["password"];

$EncryptPassword = md5($password);

mysql_query("INSERT INTO signup (f_name,l_name,email,password) VALUES ('$fname','$lname','$email','$EncryptPassword')"); 

echo '<center> Sign Up form successfully submitted. </center>';

}

 ?>

</body>
</html>

Code for login.php file.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Login Page</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
</head>

<body>

<table bgcolor="#f2f2f2" style="padding:50px" align="center" width="550px">
<form action="" method="post">
<tr><td align="center" colspan="2"> <h1>Login Page</h1> </td></tr>
<tr>
<td> Email : </td><td><input type="email" name="email"></td>
</tr>
<tr>
<td> Password : </td><td><input type="password" name="password"></td>
</tr>

<tr>
<td align="center" colspan="2"><input type="submit" name="submit"></td></tr>
<td align="center" colspan="2"><a href="sign-up.php"><button type="button">Click Here To SignUp</button></a></td></tr>

</form>
</table>

<?php
if(isset($_POST["submit"]))
{
 
if(count($_POST)>0) 
{
 //Including dbconfig file.
include 'dbconfig.php';
 
$email = $_POST["email"];

$password = $_POST["password"];

$EncryptPassword = md5($password);

$finalResult = mysql_query("SELECT * FROM signup WHERE email='$email' and Password = '$EncryptPassword'"); 

$confirm = mysql_fetch_array($finalResult);

if(is_array($confirm)) {
 
 session_start();
 $_SESSION['sid']=session_id();
 header("location:dashboard.php");

} else {
 

echo '<center>' . "Wrong UserName or Password..." . '</center>';

}

}
}
?>

</body>
</html>

Code for dashboard.php file.

 <?php
 session_start();
 if($_SESSION['sid']==session_id())
 {
 
 echo "<a href='logout.php'>Logout</a>";
 }
 else
 {
 header("location:login.php");
 }
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Home Page</title>
</head>

<body>

<table border="2" cellpadding="15" cellspacing="2" width="400" align="center">
<tr><td align="center">
<h2>Welcome User</h2></td>
<tr><td align="center">
<a href='logout.php'>Logout</a>
</td></tr>
</tr>
</table>


</body>
</html>

Code for logout.php file.

 <?php

 echo "Logged out scuccessfully";
 
 session_start();
 session_destroy();
 setcookie(session_id(),time()-1);
 
 header("location:login.php");

?>

Code for dbconfig.php file.

<?php

 //This script is designed by Android-Examples.com
//Define your host here.
$hostname = "localhost";

//Define your database username here.
$username = "root";

//Define your database password here.
$password = "";

//Define your database name here.
$dbname = "test";

 $conn = mysql_connect($hostname, $username, $password);
 
 if (!$conn)
 
 {
 
 die('Could not connect: ' . mysql_error());
 
 }
 
 mysql_select_db($dbname, $conn);

 //This script is designed by Android-Examples.com

?>

Code for style.css file.

 @charset "utf-8";
/* CSS Document */

body{
 margin:0px;
 font-family:Baskerville, 'Palatino Linotype', Palatino, 'Century Schoolbook L', 'Times New Roman', serif;
 }

input[type=text], select {
 width: 100%;
 border-radius: 5px;
 margin: 7px 0;
 border: 1px solid #ccc;
 padding: 14px 18px; 
 display: inline-block;
 box-sizing: border-box;
}

input[type=email], select {
 width: 100%;
 border-radius: 5px;
 margin: 7px 0;
 border: 1px solid #ccc;
 padding: 14px 18px; 
 display: inline-block;
 box-sizing: border-box;
}

input[type=password], select {
 width: 100%;
 border-radius: 5px;
 margin: 7px 0;
 border: 1px solid #ccc;
 padding: 14px 18px; 
 display: inline-block;
 box-sizing: border-box;
}

input[type=submit]:hover {
 background-color: #00a7d1;
}

input[type=submit] {
 width: 100%;
 border: none;
 color: white;
 padding: 14px 20px;
 background-color: #01c9fb;
 margin: 8px 0;
 cursor: pointer;
 border-radius: 4px;
 
}

button[type=button] {
 width: 100%;
 border: none;
 color: white;
 padding: 14px 20px;
 background-color: #01c9fb;
 margin: 8px 0;
 cursor: pointer;
 border-radius: 4px;
 
}

button[type=button]:hover {
 background-color: #00a7d1;
}

Screenshots:

login-page

 

PHP Create user Signup Registration page with Login script using MySQL database

Click here to download PHP Create user Signup Registration page with Login script using MySQL database project with database SQL file included.

7 Comments

  1. why i cannot go into dashboard page after login?even i enter the right password and email?

    • Afiqah check your login.php page and set the dashboard page URL in header like i did also download my whole project and run it in your localhost it will help you.

  2. I was wondering if you had a tutorial on how to set this up? I have placed the files into my htdocs folder and run it through my localhost. However I am given the error:

    “Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\Abyss Web Server\htdocs\dbconnect.php:12 Stack trace: #0 C:\Abyss Web Server\htdocs\register.php(7): include_once() #1 {main} thrown in C:\Abyss Web Server\htdocs\dbconnect.php on line 12”

    • James did you connect the mysql database to your project and please put the all project into single folder like C:\Abyss Web Server\htdocs\project\

  3. Okay, thank you! I’ve replaced the dbconnect.php file with my own to connect the mysql database and it now works. I have also created the single folder. The register and sign in pages are actually displaying now, however there are now undeclared variable errors such as “Notice: Undefined variable: nameError in C:\Abyss Web Server\htdocs\project\register.php on line 129”.

    • James there is no page present in this project such as register.php did you change the pages name.

      • No, I don’t think I changed the page name. I downloaded the folder “login-registration-verify-reset-forms-php-mysql” and there are various php files, one of which is register.php

Leave a Reply

Your email address will not be published. Required fields are marked *