Create A Login Form Validation Using PHP And WAMP / XAMPP

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
  • XAMPP/WAMP Server.
  • Brackets (IDE).
  • Little HTML, CSS, and PHP knowledge.
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.   `passwordvarchar(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.               
  8.     margin-top: 100px;  
  9.     margin-bottom: 100px;  
  10.     margin-right: 150px;  
  11.     margin-left: 80px;  
  12.     background-color: azure ;  
  13.     color: palevioletred;  
  14.     font-family: verdana;  
  15.     font-size: 100%  
  16.       
  17.         }  
  18.             h1 {  
  19.     color: indigo;  
  20.     font-family: verdana;  
  21.     font-size: 100%;  
  22. }  
  23.         h3 {  
  24.     color: indigo;  
  25.     font-family: verdana;  
  26.     font-size: 100%;  
  27. } </style>  
  28. </head>  
  29. <body>  
  30.      <center><h1>CREATE REGISTRATION AND LOGIN FORM USING PHP AND MYSQL</h1></center>  
  31.    <p><a href="register.php">Register</a> | <a href="login.php">Login</a></p>  
  32. <h3>Login Form</h3>  
  33. <form action="" method="POST">  
  34. Username: <input type="text" name="user"><br />  
  35. Password: <input type="password" name="pass"><br />   
  36. <input type="submit" value="Login" name="submit" />  
  37. </form>  
  38. <?php  
  39. if(isset($_POST["submit"])){  
  40.   
  41. if(!empty($_POST['user']) && !empty($_POST['pass'])) {  
  42.     $user=$_POST['user'];  
  43.     $pass=$_POST['pass'];  
  44.   
  45.     $con=mysql_connect('localhost','root','') or die(mysql_error());  
  46.     mysql_select_db('user_registration') or die("cannot select DB");  
  47.   
  48.     $query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'");  
  49.     $numrows=mysql_num_rows($query);  
  50.     if($numrows!=0)  
  51.     {  
  52.     while($row=mysql_fetch_assoc($query))  
  53.     {  
  54.     $dbusername=$row['username'];  
  55.     $dbpassword=$row['password'];  
  56.     }  
  57.   
  58.     if($user == $dbusername && $pass == $dbpassword)  
  59.     {  
  60.     session_start();  
  61.     $_SESSION['sess_user']=$user;  
  62.   
  63.     /* Redirect browser */  
  64.     header("Location: member.php");  
  65.     }  
  66.     } else {  
  67.     echo "Invalid username or password!";  
  68.     }  
  69.   
  70. else {  
  71.     echo "All fields are required!";  
  72. }  
  73. }  
  74. ?>  
  75. </body>  
  76. </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.         }  
  17.             h1 {  
  18.     color: indigo;  
  19.     font-family: verdana;  
  20.     font-size: 100%;  
  21. }  
  22.          h2 {  
  23.     color: indigo;  
  24.     font-family: verdana;  
  25.     font-size: 100%;  
  26. }</style>  
  27. </head>  
  28. <body>  
  29.      
  30.     <center><h1>CREATE REGISTRATION AND LOGIN FORM USING PHP AND MYSQL</h1></center>  
  31.    <p><a href="register.php">Register</a> | <a href="login.php">Login</a></p>  
  32.     <center><h2>Registration Form</h2></center>  
  33. <form action="" method="POST">  
  34.     <legend>  
  35.     <fieldset>  
  36.           
  37. Username: <input type="text" name="user"><br />  
  38. Password: <input type="password" name="pass"><br />   
  39. <input type="submit" value="Register" name="submit" />  
  40.               
  41.         </fieldset>  
  42.         </legend>  
  43. </form>  
  44. <?php  
  45. if(isset($_POST["submit"])){  
  46. if(!empty($_POST['user']) && !empty($_POST['pass'])) {  
  47.     $user=$_POST['user'];  
  48.     $pass=$_POST['pass'];  
  49.     $con=mysql_connect('localhost','root','') or die(mysql_error());  
  50.     mysql_select_db('user_registration') or die("cannot select DB");  
  51.   
  52.     $query=mysql_query("SELECT * FROM login WHERE username='".$user."'");  
  53.     $numrows=mysql_num_rows($query);  
  54.     if($numrows==0)  
  55.     {  
  56.     $sql="INSERT INTO login(username,password) VALUES('$user','$pass')";  
  57.   
  58.     $result=mysql_query($sql);  
  59.         if($result){  
  60.     echo "Account Successfully Created";  
  61.     } else {  
  62.     echo "Failure!";  
  63.     }  
  64.   
  65.     } else {  
  66.     echo "That username already exists! Please try again with another.";  
  67.     }  
  68.   
  69. else {  
  70.     echo "All fields are required!";  
  71. }  
  72. }  
  73. ?>  
  74. </body>  
  75. </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.               
  14.     margin-top: 100px;  
  15.     margin-bottom: 100px;  
  16.     margin-right: 150px;  
  17.     margin-left: 80px;  
  18.     background-color: azure ;  
  19.     color: palevioletred;  
  20.     font-family: verdana;  
  21.     font-size: 100%  
  22.       
  23.         }  
  24.             h2 {  
  25.     color: indigo;  
  26.     font-family: verdana;  
  27.     font-size: 100%;  
  28. }  
  29.         h1 {  
  30.     color: indigo;  
  31.     font-family: verdana;  
  32.     font-size: 100%;  
  33. }  
  34.               
  35.           
  36.     </style>  
  37. </head>  
  38. <body>  
  39.     <center><h1>CREATE REGISTRATION AND LOGIN FORM USING PHP AND MYSQL</h1></center>  
  40.       
  41. <h2>Welcome, <?=$_SESSION['sess_user'];?>! <a href="logout.php">Logout</a></h2>  
  42. <p>  
  43. WE DO IT. SUCCESSFULLY CREATED REGISTRATION AND LOGIN FORM USING PHP AND MYSQL  
  44. </p>  
  45. </body>  
  46. </html>  
  47. <?php  
  48. }  
  49. ?>  
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. 
 
 


Similar Articles