Creating a simple PHP Login and Register Project with MySQL: A Step-by-Step Guide

In this tutorial, we’ll walk you through the process of creating a simple PHP Login and Register Project with MySQL as the database. We’ll deploy the project on localhost using XAMPP, a popular web development environment, and add some basic inline CSS styles to enhance the user interface.

Prerequisites for PHP Login and Register Project:

  1. XAMPP is installed on your local machine. You can download it from the official website: https://www.apachefriends.org/ and install the XAMPP.
  2. Basic knowledge of PHP, HTML, and MySQL.

Step 1: Setting up the PHP Login and Register Project

  1. Open the XAMPP control panel and start the Apache and MySQL services.
  2. Navigate to the htdocs folder inside your XAMPP installation directory for eg. “C:\xampp\htdocs\”.
  3. Create a new folder here for your project, let’s call it “login_project”.

Step 2: Database Setup

  1. Open your web browser and navigate to http://localhost/phpmyadmin/.
  2. Click on “Databases” in the top menu and create a new database. Name it “login_db”.
  3. Inside the “login_db” database, create two tables: “users”.
    • For the “users” table, use the following columns: userID (auto-increment), userName, userEmail, userPassword, and userRegisterDate.

Step 3: Creating the Config.php and connect database.

Inside the “login_project” folder, create a new PHP file: config.php and add the below code to it.

<?php 
$server = 'localhost'; //for server add localhost.
$user = 'root'; // default "root". If your user is something else, please add here.
$pass = ''; // default is blank. If your user has password please add here.
$db = 'login_db'; // Add database name we have created.
	
$con = new mysqli($server, $user, $pass, $db);

if ($con->connect_error) {
	die("Connection failed:". $con->connect_error);
} else {
	session_start();
}
?>

Step 4: Creating the Login and Register Pages

Inside the “login_project” folder, create two new PHP files: login.php and register.php.

Now, let’s edit “login.php” file. Copy the following code into it:

<?php 
include 'config.php';  // Include the config file.
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Login</title>
</head>
<body>
<h1>Login </h1>
<?php 
if (isset($_POST['login'])) {
	$userEmail = md5(strtolower($_POST['userEmail']));
	$userPassword = md5($_POST['userPassword']);

	$getUser = mysqli_query($con, "SELECT `userID` FROM `users` WHERE `userEmail` = '$userEmail' and `userPassword`= '$userPassword'");
	if (mysqli_num_rows($getUser)>0) {
		$row = mysqli_fetch_assoc($getUser);
		$_SESSION['userID'] = $row['userID'];
		header('Location: index.php');

		// code...
	} else {
		echo "Email or Password is wrong.";
	}
}

?>

<form method="post" action="">
	<input type="email" name="userEmail" placeholder="Email">
	<input type="password" name="userPassword" placeholder="Password">
	<input type="submit" name="login" value="Sign in">
	<p>Already have a account <a href="register.php">Register here</a></p>
</form>
</body>
</html>

Now, let’s edit “register.php” file. Copy the following code into it:

<?php 
include 'config.php';
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Register</title>
</head>
<body>
<h1>Register </h1>
<?php 
if (isset($_POST['register'])) {
	$userName = $_POST['userName'];
	$userEmail = md5(strtolower($_POST['userEmail']));
	$userPassword = md5($_POST['userPassword']);

	$getUser = mysqli_query($con, "SELECT * FROM `users` WHERE `userEmail` = '$userEmail'");

	if (mysqli_num_rows($getUser)>0) {
		echo "User already exists, please enter another email.";
		// code...
	} else {
		$insert = mysqli_query($con, "INSERT INTO `users`(`userName`, `userEmail`, `userPassword`) VALUES ('$userName','$userEmail','$userPassword')");
		if ($insert == true) {
			echo "Data added";
			// code...
		} else {
			echo "There is an error.";
		}
	}
}

?>

<form method="post" action="">
	<input type="text" name="userName" placeholder="User Name">
	<input type="email" name="userEmail" placeholder="Email">
	<input type="password" name="userPassword" placeholder="Password">
	<input type="submit" name="register" value="Sign up">
	<p>Do not have an account <a href="login.php">Login here</a></p>
</form>
</body>
</html>

Step 5: Creating the Dashboard

  1. Inside the “login_project” folder, create a new file called dashboard.php. This file will serve as the user’s dashboard after a successful login:
<?php 
include 'config.php';

if (!empty($_SESSION['userID'])) {
	$userID = $_SESSION['userID'];
} else {
	header('Location: login.php');
}

?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Dashboard</title>
</head>
<body>
	<?php 
	$getUser = mysqli_query($con, "SELECT * FROM `users` WHERE `userID` = '$userID'");
	if (mysqli_num_rows($getUser)>0) {
		$row = mysqli_fetch_assoc($getUser);
		$userName = $row['userName'];
		// code...
	} else {
		header('Location: login.php');
	}

	?>
<h1>Dashboard: <?php echo $userName; ?></h1>
<p><a href="logout.php">Logout as <?php echo $userName; ?></a></p>
</body>
</html>

Step 6: Creating the Logout Page

  1. Inside the “login_project” folder, create a new file called logout.php. This file will handle the logout functionality:
<?php
include 'config.php';

session_destroy();
header('Location: login.php');

?>

Step 7: Testing the Project

  1. Open your web browser and navigate to http://localhost/login_project/login.php.
  2. You should see the login page. Try logging in with valid credentials or click the “Register” link to create a new account.
  3. After successful login, you will be redirected to the dashboard page, where you will see a welcome message with your username.
  4. Click the “Logout” link to log out and return to the login page.

Congratulations! You’ve successfully created a PHP-based login and registration system with MySQL and deployed it on localhost using XAMPP. Remember that this is a basic example, and in a real-world scenario, you’d want to add more security measures, error handling, and possibly use a different method for storing passwords (e.g., using password_hash and password_verify). Happy coding!

Note: This tutorial assumes that you have XAMPP installed and running with default settings. If you have a different configuration, make sure to adjust the database connection details in the PHP files accordingly.

PHP Login and Register Project

25 thoughts on “Creating a simple PHP Login and Register Project with MySQL: A Step-by-Step Guide”

  1. Heya i am for the first time here. I came across this board and I find It truly useful & it helped me out much. I hope to give something back and aid others like you helped me.

  2. claygaither7436

    Great site you have got here.. It’s hard to find excellent writing like yours these days. I seriously appreciate people like you! Take care!!

  3. valentinahendric

    Hurrah, that’s what I was exploring for, what a data! existing here at this blog, thanks admin of this site.

  4. margaritarosenth

    You have made some really good points there. I checked on the net to learn more about the issue and found most individuals will go along with your views on this website.

  5. Wow, fantastic blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is great, as well as the content!

  6. This design is steller! You certainly know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Fantastic job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!

  7. lashondalowrance

    Amazing blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple tweeks would really make my blog jump out. Please let me know where you got your design. Appreciate it

  8. I’m not sure where you’re getting your information, but good topic. I needs to spend some time learning more or understanding more. Thanks for great information I was looking for this information for my mission.

  9. It’s appropriate time to make some plans for the future and it is time to be happy. I have read this post and if I could I wish to suggest you few interesting things or suggestions. Maybe you could write next articles referring to this article. I want to read more things about it!

  10. I have been browsing online more than three hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my view, if all site owners and bloggers made good content as you did, the net will be much more useful than ever before.

  11. Way cool! Some extremely valid points! I appreciate you penning this write-up and the rest of the website is very good.

  12. all the time i used to read smaller articles which as well clear their motive, and that is also happening with this post which I am reading here.

  13. madeleineshively

    This is really fascinating, You are a very skilled blogger. I’ve joined your feed and look forward to in search of more of your magnificent post. Additionally, I’ve shared your website in my social networks

  14. Great post. I used to be checking continuously this weblog and I am impressed! Extremely helpful info specially the last section 🙂 I care for such info much. I was looking for this particular information for a long time. Thank you and good luck.

  15. Hello to every one, the contents existing at this web site are truly remarkable for people experience, well, keep up the good work fellows.

  16. Hey there, You’ve done a fantastic job. I will definitely digg it and personally recommend to my friends. I’m sure they’ll be benefited from this website.

  17. Hey there would you mind letting me know which hosting company you’re using? I’ve loaded your blog in 3 completely different web browsers and I must say this blog loads a lot quicker then most. Can you recommend a good hosting provider at a fair price? Kudos, I appreciate it!

  18. paulbullock70

    A motivating discussion is worth comment. I do believe that you should write more about this subject matter, it may not be a taboo matter but generally people don’t talk about these subjects. To the next! All the best!!

Leave a Comment

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

Scroll to Top