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:
- XAMPP is installed on your local machine. You can download it from the official website: https://www.apachefriends.org/ and install the XAMPP.
- Basic knowledge of PHP, HTML, and MySQL.
Step 1: Setting up the PHP Login and Register Project
- Open the XAMPP control panel and start the Apache and MySQL services.
- Navigate to the
htdocs
folder inside your XAMPP installation directory for eg. “C:\xampp\htdocs\”. - Create a new folder here for your project, let’s call it “login_project”.
Step 2: Database Setup
- Open your web browser and navigate to
http://localhost/phpmyadmin/
. - Click on “Databases” in the top menu and create a new database. Name it “login_db”.
- 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.
- For the “users” table, use the following columns:
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
- 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
- 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
- Open your web browser and navigate to
http://localhost/login_project/login.php
. - You should see the login page. Try logging in with valid credentials or click the “Register” link to create a new account.
- After successful login, you will be redirected to the dashboard page, where you will see a welcome message with your username.
- 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.
What a stuff of un-ambiguity and preserveness of valuable know-how on the topic of unpredicted feelings.
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.
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!!
Hurrah, that’s what I was exploring for, what a data! existing here at this blog, thanks admin of this site.
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.
I have learn a few just right stuff here. Certainly price bookmarking for revisiting. I wonder how a lot effort you set to create such a fantastic informative website.
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!
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!
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
Thank you for the amazing comments.
I have added custom design elements to the website. If need any help or suggestion please let me know.
It’s an awesome post in support of all the web viewers; they will obtain advantage from it I am sure.
Thank you so much for your helping comments.
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.
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!
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.
Way cool! Some extremely valid points! I appreciate you penning this write-up and the rest of the website is very good.
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.
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
Awesome article.
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.
Hello to every one, the contents existing at this web site are truly remarkable for people experience, well, keep up the good work fellows.
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.
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!
Thank you, Here is a high speed hosting with fair price. Milesweb
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!!