PHP + MySQL Login and Registration Form using XAMPP - Lara Tutorials (2024)

Simple login and registration form in PHP + MySQL + Boostrap using xampp. In this tutorial; i am going tos show you how to create a simple login and registration form in PHP and MySQL using xampp.

And as well as, you can download and use this source code of registration and login form in php and mysql with validation.

Login and Registration Form in PHP and MySQL using XAMPP

Use the following steps to create login and registration form in php and mysql using xampp; as follows:

  • Step 1 – Open the XAMPP Control Panel & Create PHP Project
  • Step 2 – Create Database and Table
  • Step 3 – Create a Database Connection File
  • Step 4 – Create a registration form and Insert data into MySQL database
  • Step 5 – Create Login Form in PHP with MySQL
  • Step 6 – Create User Profile and Fetch Data From MySQL Database
  • Step 7 – Create Logout.php file

Step 1 – Open the XAMPP Control Panel & Create PHP Project

Visit your xampp installed directory. Then open xampp control panel and start it; then visit xampp/htdocs directory. And inside this directory create php project directory.

Step 2 – Create Database and Table

Create a database name my_db and execute the below-given query in your database. The below query will create a table named users in your database with the following fields like uid, name, email, mobile:

CREATE DATABASE my_db;CREATE TABLE `users` ( `uid` bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `mobile` varchar(255) DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Step 3 – Create a Database Connection File

Create a file name db.php and update the below code into your file.

The below code is used to create a MySQL database connection in PHP. When you insert form data into MySQL database, there you will include this file:

<?php $servername='localhost'; $username='root'; $password=''; $dbname = "my_db"; $conn=mysqli_connect($servername,$username,$password,"$dbname"); if(!$conn){ die('Could not Connect MySql Server:' .mysql_error()); }?>

Step 4 – Create registration form and Insert data into MySQL database

Create registration form file; which name registration.php. And add the following code into it:

<?php require_once "db.php"; if(isset($_SESSION['user_id'])!="") { header("Location: dashboard.php"); } if (isset($_POST['signup'])) { $name = mysqli_real_escape_string($conn, $_POST['name']); $email = mysqli_real_escape_string($conn, $_POST['email']); $mobile = mysqli_real_escape_string($conn, $_POST['mobile']); $password = mysqli_real_escape_string($conn, $_POST['password']); $cpassword = mysqli_real_escape_string($conn, $_POST['cpassword']); if (!preg_match("/^[a-zA-Z ]+$/",$name)) { $name_error = "Name must contain only alphabets and space"; } if(!filter_var($email,FILTER_VALIDATE_EMAIL)) { $email_error = "Please Enter Valid Email ID"; } if(strlen($password) < 6) { $password_error = "Password must be minimum of 6 characters"; } if(strlen($mobile) < 10) { $mobile_error = "Mobile number must be minimum of 10 characters"; } if($password != $cpassword) { $cpassword_error = "Password and Confirm Password doesn't match"; } if(mysqli_query($conn, "INSERT INTO users(name, email, mobile_number ,password) VALUES('" . $name . "', '" . $email . "', '" . $mobile . "', '" . md5($password) . "')")) { header("location: login.php"); exit(); } else { echo "Error: " : "" . mysqli_error($conn); } mysqli_close($conn); }?><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Simple Registration Form in PHP with Validation | Laratutorials.com</title> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"></head><body> <div class="container"> <div class="row"> <div class="col-lg-8 col-offset-2"> <div class="page-header"> <h2>Registration Form in PHP with Validation</h2> </div> <p>Please fill all fields in the form</p> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <div class="form-group"> <label>Name</label> <input type="text" name="name" class="form-control" value="" maxlength="50" required=""> <span class="text-danger"><?php if (isset($name_error)) echo $name_error; ?></span> </div> <div class="form-group "> <label>Email</label> <input type="email" name="email" class="form-control" value="" maxlength="30" required=""> <span class="text-danger"><?php if (isset($email_error)) echo $email_error; ?></span> </div> <div class="form-group"> <label>Mobile</label> <input type="text" name="mobile" class="form-control" value="" maxlength="12" required=""> <span class="text-danger"><?php if (isset($mobile_error)) echo $mobile_error; ?></span> </div> <div class="form-group"> <label>Password</label> <input type="password" name="password" class="form-control" value="" maxlength="8" required=""> <span class="text-danger"><?php if (isset($password_error)) echo $password_error; ?></span> </div> <div class="form-group"> <label>Confirm Password</label> <input type="password" name="cpassword" class="form-control" value="" maxlength="8" required=""> <span class="text-danger"><?php if (isset($cpassword_error)) echo $cpassword_error; ?></span> </div> <input type="submit" class="btn btn-primary" name="signup" value="submit"> Already have a account?<a href="login.php" class="btn btn-default">Login</a> </form> </div> </div> </div></body></html>

Step 5 – Create Login Form In PHP with MySQL

Create login form, where you accept user email id and password and authenticate users from database. So you can create a login.php file and add the below code into your file:

<?phpsession_start();require_once "db.php";if(isset($_SESSION['user_id'])!="") { header("Location: dashboard.php");}if (isset($_POST['login'])) { $email = mysqli_real_escape_string($conn, $_POST['email']); $password = mysqli_real_escape_string($conn, $_POST['password']); if(!filter_var($email,FILTER_VALIDATE_EMAIL)) { $email_error = "Please Enter Valid Email ID"; } if(strlen($password) < 6) { $password_error = "Password must be minimum of 6 characters"; } $result = mysqli_query($conn, "SELECT * FROM users WHERE email = '" . $email. "' and pass = '" . md5($password). "'"); if(!empty($result)){ if ($row = mysqli_fetch_array($result)) { $_SESSION['user_id'] = $row['uid']; $_SESSION['user_name'] = $row['name']; $_SESSION['user_email'] = $row['email']; $_SESSION['user_mobile'] = $row['mobile']; header("Location: dashboard.php"); } }else { $error_message = "Incorrect Email or Password!!!"; }}?><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Simple Login Form in PHP with Validation | Laratutorials.com</title> <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"></head><body> <div class="container"> <div class="row"> <div class="col-lg-10"> <div class="page-header"> <h2>Login Form in PHP with Validation</h2> </div> <p>Please fill all fields in the form</p> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <div class="form-group "> <label>Email</label> <input type="email" name="email" class="form-control" value="" maxlength="30" required=""> <span class="text-danger"><?php if (isset($email_error)) echo $email_error; ?></span> </div> <div class="form-group"> <label>Password</label> <input type="password" name="password" class="form-control" value="" maxlength="8" required=""> <span class="text-danger"><?php if (isset($password_error)) echo $password_error; ?></span> </div> <input type="submit" class="btn btn-primary" name="login" value="submit"> <br> You don't have account?<a href="registration.php" class="mt-3">Click Here</a> </form> </div> </div> </div></body></html>

Step 6 – Create User Profile and Fetch data From Database

Create a new file name dashboard.php and add the below code into your file.

The below code used to show logged in user data.

<?php session_start(); if(isset($_SESSION['user_login_id']) =="") { header("Location: login.php"); }?><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>User Info Dashboard | Laratutorials.com</title> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"></head><body> <div class="container"> <div class="row"> <div class="col-lg-8"> <div class="card"> <div class="card-body"> <h5 class="card-title">Name :- <?php echo $_SESSION['user_name']?></h5> <p class="card-text">Email :- <?php echo $_SESSION['user_email']?></p> <p class="card-text">Mobile :- <?php echo $_SESSION['user_mobile']?></p> <a href="logout.php" class="btn btn-primary">Logout</a> </div> </div> </div> </div> </div></body></html>

Step 7 – Create Logout.php file

Create logout.php file and update the below code into your file.

The below code is used to destroy login your data and logout.

<?phpob_start();session_start();if(isset($_SESSION['user_login_id'])) {session_destroy();unset($_SESSION['user_id']);unset($_SESSION['user_name']);unset($_SESSION['user_email']);unset($_SESSION['user_mobile']);header("Location: login.php");} else {header("Location: login.php");}?>

Conclusion

Simple login and registration form in php with mysql database using xampp; In this tutorial, you have learned how to create a simple login, registration and logout system in PHP MySQL with validation. Also, you have learned how to authenticate and logout logged in users in PHP.

More PHP + MySQL Tutorials

Recommended:-PHP Contact us Form with MySQL Database and Validation

Recommended:-Submit Form without Page Refresh Using Ajax in PHP MySQL

Recommended:-PHP MySQL Ajax Add Edit Delete Same Page

Recommended:-PHP Ajax Live Search From MySQL Database

Recommended:-PHP MySQL Ajax Autocomplete TextBox From DB Example

Recommended:-Form validation using jQuery in PHP

Recommended:-PHP + MySQL Login Form with Session

Recommended:-PHP + MySQL Registration Form with Validation

Recommended:-PHP Ajax Form Submit with Validation Example

Recommended:-Ajax Get Data From MySQL Database in PHP

Recommended:-User Registration with Email Verification using PHP and MySQL

Recommended:-Find Nearest Location using Latitude Longitude PHP + MySQL

Recommended:-Google Places Autocomplete in PHP

Recommended:-PHP + MySQL + jQuery Signature Pad

Recommended:-Country State City Dropdown in PHP + MySQL + Ajax

Recommended:-Drag and Drop File Upload using DropzoneJS PHP + MySQL

Recommended:-

Recommended:-PHP Send Reset Password Link Email

Recommended:-Image Crop and Save into Database using PHP + jQuery + Ajax

Recommended:-Star Rating System In PHP + MySQL + Ajax

Recommended:-Category Subcategory Dropdown in PHP + MySQL + Ajax

Recommended:-PHP + Ajax Upload Multiple Image with Preview

Recommended:-Generate 6,8,10,16 digit random unique Alphanumeric string in PHP

Recommended:-Get Country, City, latitude, longitude from IP address in PHP

Recommended:-Login with Google Account Using PHP + MySQL (Source Code)

Recommended:-PHP Upload File to AWS S3 Bucket

Recommended:-Import CSV File Into MySQL Database using PHP

Recommended:-PHP + Ajax Import CSV File Into MySQL Database

If you have any questions or thoughts to share, use the comment form below to reach us.

PHP + MySQL Login and Registration Form using XAMPP - Lara Tutorials (2024)
Top Articles
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 6641

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.