Introduction

WAMP stands for Windows, Apache, MySQL, and anyone of PHP, Perl, or Python. WAMP server is a local webserver for running the scripting language (PHP, Perl) and this is open source. XAMPP stands for Cross-Platform (X), Apache (A), Maria DB (M), PHP (P), and Perl (P). It is developed by Apache Friends. It includes Apache and MySQL, FileZilla, Mercury, Tomcat, and Maria DB.
Here, I am going to show how to create a login form validation, using PHP and XAMPP.
Requirements
Steps given below are required to be followed
Follow the steps to create a login form validation, using PHP and XAMPP. I have included the source code, which is given below.
Step 1
Open the XAMPP Control Panel. Start Apache and MySQL Server.
PHP
Step 2
Open your browser and then type http:// localhost or http://127.0.0.1. After you see the XAMPP community page, make sure, if your Server is running or not.
PHP
Step 3
Open XAMPP Control Panel, followed by clicking MySQL admin. Now, create the database for the login validation form.
PHP
Step 4
To create the new database, click New.
PHP
Step 5
Put the database name, which you want. Here, the database name is user-registration, followed by clicking create.
PHP
Step 6
Go to the SQL query tab, copy and paste the query given below. Click go. Afterward, your table (my table name: person) is created successfully.
SQL query
  1. CREATE TABLE IF NOT EXISTS `login` (
  2. `username` varchar(200) NOT NULL,
  3. `password` varchar(200) NOT NULL,
  4. PRIMARY KEY (`username`)
  5. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
PHP
Step 7
Put your Webpage files into the destination folder. Default folder is c:/xampp/htdocs.
PHP
Step 8
Open the bracket(IDE) and create the login.php file. Copy the code given below and paste into login.php
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Login</title>
  5. <style>
  6. body{
  7. margin-top: 100px;
  8. margin-bottom: 100px;
  9. margin-right: 150px;
  10. margin-left: 80px;
  11. background-color: azure ;
  12. color: palevioletred;
  13. font-family: verdana;
  14. font-size: 100%
  15. }
  16. h1 {
  17. color: indigo;
  18. font-family: verdana;
  19. font-size: 100%;
  20. }
  21. h3 {
  22. color: indigo;
  23. font-family: verdana;
  24. font-size: 100%;
  25. } </style>
  26. </head>
  27. <body>
  28. <center><h1>CREATE REGISTRATION AND LOGIN FORM USING PHP AND MYSQL</h1></center>
  29. <p><a href="register.php">Register</a> | <a href="login.php">Login</a></p>
  30. <h3>Login Form</h3>
  31. <form action="" method="POST">
  32. Username: <input type="text" name="user"><br />
  33. Password: <input type="password" name="pass"><br />
  34. <input type="submit" value="Login" name="submit" />
  35. </form>
  36. <?php
  37. if(isset($_POST["submit"])){
  38. if(!empty($_POST['user']) && !empty($_POST['pass'])) {
  39. $user=$_POST['user'];
  40. $pass=$_POST['pass'];
  41. $con=mysql_connect('localhost','root','') or die(mysql_error());
  42. mysql_select_db('user_registration') or die("cannot select DB");
  43. $query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'");
  44. $numrows=mysql_num_rows($query);
  45. if($numrows!=0)
  46. {
  47. while($row=mysql_fetch_assoc($query))
  48. {
  49. $dbusername=$row['username'];
  50. $dbpassword=$row['password'];
  51. }
  52. if($user == $dbusername && $pass == $dbpassword)
  53. {
  54. session_start();
  55. $_SESSION['sess_user']=$user;
  56. /* Redirect browser */
  57. header("Location: member.php");
  58. }
  59. } else {
  60. echo "Invalid username or password!";
  61. }
  62. } else {
  63. echo "All fields are required!";
  64. }
  65. }
  66. ?>
  67. </body>
  68. </html>
PHP
Step 9
Create the register.php file. Copy the code given below and paste it into register.php.
register.php
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Register</title>
  5. <style>
  6. body{
  7. margin-top: 100px;
  8. margin-bottom: 100px;
  9. margin-right: 150px;
  10. margin-left: 80px;
  11. background-color: azure ;
  12. color: palevioletred;
  13. font-family: verdana;
  14. font-size: 100%
  15. }
  16. h1 {
  17. color: indigo;
  18. font-family: verdana;
  19. font-size: 100%;
  20. }
  21. h2 {
  22. color: indigo;
  23. font-family: verdana;
  24. font-size: 100%;
  25. }</style>
  26. </head>
  27. <body>
  28. <center><h1>CREATE REGISTRATION AND LOGIN FORM USING PHP AND MYSQL</h1></center>
  29. <p><a href="register.php">Register</a> | <a href="login.php">Login</a></p>
  30. <center><h2>Registration Form</h2></center>
  31. <form action="" method="POST">
  32. <legend>
  33. <fieldset>
  34. Username: <input type="text" name="user"><br />
  35. Password: <input type="password" name="pass"><br />
  36. <input type="submit" value="Register" name="submit" />
  37. </fieldset>
  38. </legend>
  39. </form>
  40. <?php
  41. if(isset($_POST["submit"])){
  42. if(!empty($_POST['user']) && !empty($_POST['pass'])) {
  43. $user=$_POST['user'];
  44. $pass=$_POST['pass'];
  45. $con=mysql_connect('localhost','root','') or die(mysql_error());
  46. mysql_select_db('user_registration') or die("cannot select DB");
  47. $query=mysql_query("SELECT * FROM login WHERE username='".$user."'");
  48. $numrows=mysql_num_rows($query);
  49. if($numrows==0)
  50. {
  51. $sql="INSERT INTO login(username,password) VALUES('$user','$pass')";
  52. $result=mysql_query($sql);
  53. if($result){
  54. echo "Account Successfully Created";
  55. } else {
  56. echo "Failure!";
  57. }
  58. } else {
  59. echo "That username already exists! Please try again with another.";
  60. }
  61. } else {
  62. echo "All fields are required!";
  63. }
  64. }
  65. ?>
  66. </body>
  67. </html>
PHP
Register your username and password. Do not use the already used usernames.
PHP
Step 10
Create the logout.php file. Copy the code given below and paste it into logout.php.
logout.php
  1. <?php
  2. session_start();
  3. unset($_SESSION['sess_user']);
  4. session_destroy();
  5. header("location:login.php");
  6. ?>
Step 13
Create the member.php file. Copy the code given below and paste it into member.php.
member.php
  1. <?php
  2. session_start();
  3. if(!isset($_SESSION["sess_user"])){
  4. header("location:login.php");
  5. } else {
  6. ?>
  7. <!doctype html>
  8. <html>
  9. <head>
  10. <title>Welcome</title>
  11. <style>
  12. body{
  13. margin-top: 100px;
  14. margin-bottom: 100px;
  15. margin-right: 150px;
  16. margin-left: 80px;
  17. background-color: azure ;
  18. color: palevioletred;
  19. font-family: verdana;
  20. font-size: 100%
  21. }
  22. h2 {
  23. color: indigo;
  24. font-family: verdana;
  25. font-size: 100%;
  26. }
  27. h1 {
  28. color: indigo;
  29. font-family: verdana;
  30. font-size: 100%;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <center><h1>CREATE REGISTRATION AND LOGIN FORM USING PHP AND MYSQL</h1></center>
  36. <h2>Welcome, <?=$_SESSION['sess_user'];?>! <a href="logout.php">Logout</a></h2>
  37. <p>
  38. WE DO IT. SUCCESSFULLY CREATED REGISTRATION AND LOGIN FORM USING PHP AND MYSQL
  39. </p>
  40. </body>
  41. </html>
  42. <?php
  43. }
  44. ?>
PHP
Now, you need to click the logout.
Output
Now, check the database to check, whether our data was stored or not.
PHP
Here, we have successfully created a login form validation. Using PHP and WAMP/XAMPP, and we created and executed it. I will continue my next article to give you in-depth knowledge about XAMPP, FileZilla, and GitHub lessons.
Reference article
https://www.c-sharpcorner.com/article/install-and-configure-xampp-server-with-data-insert-into-database2/
Source code
https://github.com/GaneshanNT/Registration-and-login-validation-form