Registration and Login Form in PHP Using OOP

This article will help you to create object oriented user registration and login forms. Use the following procedure.

Step 1

Create a database using the following query.

Create database oopregister.

Step 2

Create a table using the following query:

  1. CREATE TABLE IF NOT EXISTS `users` (  
  2.    `id` int(11) NOT NULL AUTO_INCREMENT,  
  3.    `trn_date` datetime NOT NULL,  
  4.    `namevarchar(50) NOT NULL,  
  5.    `username` varchar(50) NOT NULL,  
  6.    `email` varchar(50) NOT NULL,  
  7.    `passwordvarchar(50) NOT NULL,  
  8.    PRIMARY KEY (`id`)  
  9. );  

 

Step 3

Create a class file named class.php, a database connection is also included in this file (don’t forget to change your database username and password):
  1. <? php  
  2. define('HOST''localhost');  
  3. define('USER''root');  
  4. define('PASS''');  
  5. define('DB''oopregister');  
  6. class DB  
  7.   
  8. {  
  9.     function __construct() {  
  10.         $con = mysql_connect(HOST, USER, PASS) or die('Connection Error! '.mysql_error());  
  11.         mysql_select_db(DB, $conor die('DB Connection Error: ->'.mysql_error());  
  12.     }  
  13. }  
  14.   
  15. class User  
  16.   
  17. {  
  18.     public  
  19.   
  20.     function __construct() {  
  21.         $db = new DB;  
  22.     }  
  23.   
  24.     public  
  25.   
  26.     function register($trn_date$name$username$email$pass) {  
  27.         $pass = md5($pass);  
  28.         $checkuser = mysql_query("Select id from users where email='$email'");  
  29.         $result = mysql_num_rows($checkuser);  
  30.         if ($result == 0) {  
  31.             $register = mysql_query("Insert into users (trn_date, name, username, email, password) values ('$trn_date','$name','$username','$email','$pass')"or die(mysql_error());  
  32.             return $register;  
  33.         } else {  
  34.             return false;  
  35.         }  
  36.     }  
  37.   
  38.     public  
  39.   
  40.     function login($email$pass) {  
  41.         $pass = md5($pass);  
  42.         $check = mysql_query("Select * from users where email='$email' and password='$pass'");  
  43.         $data = mysql_fetch_array($check);  
  44.         $result = mysql_num_rows($check);  
  45.         if ($result == 1) {  
  46.             $_SESSION['login'] = true;  
  47.             $_SESSION['id'] = $data['id'];  
  48.             return true;  
  49.         } else {  
  50.             return false;  
  51.         }  
  52.     }  
  53.   
  54.     public  
  55.   
  56.     function fullname($id) {  
  57.         $result = mysql_query("Select * from users where id='$id'");  
  58.         $row = mysql_fetch_array($result);  
  59.         echo $row['name'];  
  60.     }  
  61.   
  62.     public  
  63.   
  64.     function session() {  
  65.         if (isset($_SESSION['login'])) {  
  66.             return $_SESSION['login'];  
  67.         }  
  68.     }  
  69.   
  70.     public  
  71.   
  72.     function logout() {  
  73.         $_SESSION['login'] = false;  
  74.         session_destroy();  
  75.     }  
  76. }  
  77.   
  78. ?>  
Step 4

Create a register file named register.php as in the following:
  1. <?php  
  2.    include_once 'class.php';  
  3.    $user = new User();  
  4.    if ($_SERVER["REQUEST_METHOD"] == "POST"){  
  5.       $trn_date = date("Y-m-d H:i:s");  
  6.       $register = $user->register($trn_date,$_REQUEST['name'],$_REQUEST['username'],$_REQUEST['email'],$_REQUEST['password']);  
  7.       if($register){  
  8.          echo "Registration Successful!";  
  9.       }
  10.       else
  11.       {  
  12.          echo "Entered email already exist!";  
  13.       }  
  14.    }  
  15. ?>  
  16.     <html>  
  17.   
  18.     <head>  
  19.         <title>Registration</title>  
  20.         <link rel="stylesheet" href="style.css" />  
  21.     </head>  
  22.   
  23.     <body>  
  24.         <div class="form">  
  25.             <h1>Registration</h1>  
  26.             <form action="" method="post">  
  27.                 <input type="text" name="name" placeholder="Please Enter Name" required />  
  28.                 <br />  
  29.                 <input type="text" name="username" placeholder="Please Enter Userame" required />  
  30.                 <br />  
  31.                 <input type="text" name="email" placeholder="Please Enter Email" required />  
  32.                 <br />  
  33.                 <input type="password" name="password" placeholder="Please Enter Password" required />  
  34.                 <br />  
  35.                 <input type="submit" name="submit" value="Register" />  
  36.             </form>  
  37.             <p>Alredy Registered?<a href="login.php"> Login Here</a></p>  
  38.         </div>  
  39.     </body>  
  40.   
  41.     </html>  
Step 5

Create a login file named login.php as in the following:
  1. <?php  
  2.    session_start();  
  3.    include_once 'class.php';  
  4.    $user = new User();  
  5.    if ($user->session())  
  6.    {  
  7.       header("location:index.php");  
  8.    }  
  9.   
  10.    $user = new User();  
  11.    if ($_SERVER["REQUEST_METHOD"] == "POST"){  
  12.       $login = $user->login($_REQUEST['email'],$_REQUEST['password']);  
  13.       if($login){  
  14.          header("location:index.php");  
  15.       }
  16.       else
  17.       {  
  18.          echo "Login Failed!";  
  19.       }  
  20.    }  
  21. ?>  
  22.     <html>  
  23.   
  24.     <head>  
  25.         <title>Log In</title>  
  26.         <link rel="stylesheet" href="style.css" />  
  27.     </head>  
  28.   
  29.     <body>  
  30.         <div class="form">  
  31.             <h1>Log In</h1>  
  32.             <form action="" method="post">  
  33.                 <input type="text" name="email" placeholder="Please Enter Email" required />  
  34.                 <br />  
  35.                 <input type="password" name="password" placeholder="Please Enter Password" required />  
  36.                 <br />  
  37.                 <input type="submit" name="submit" value="Login" />  
  38.             </form>  
  39.             <p>Not registered yet?<a href="register.php"> Register Here</a></p>  
  40.         </div>  
  41.     </body>  
  42.   
  43.     </html>  
Step 6

Create an index file named index.php as in the following:
  1. <?php  
  2.    session_start();  
  3.    include_once 'class.php';  
  4.    $user = new User;  
  5.    $id = $_SESSION['id'];  
  6.    if (!$user->session()){  
  7.       header("location:login.php");  
  8.    }  
  9.    if (isset($_REQUEST['q'])){  
  10.       $user->logout();  
  11.       header("location:login.php");  
  12.    }  
  13. ?>  
  14.     <html>  
  15.   
  16.     <head>  
  17.         <title>Dashboard</title>  
  18.         <link rel="stylesheet" href="style.css" />  
  19.     </head>  
  20.   
  21.     <body>  
  22.         <div class="form">  
  23.             <h1>Welcome <?php $user->fullname($id);?></h1>  
  24.             <p align="right"><a href="?q=logout">LOGOUT</a></p>  
  25.         </div>  
  26.     </body>  
  27.   
  28.     </html>  
Step 7

Create a style file named style.css as in the following:
  1. body {  
  2.     font - family: Arial, Sans - Serif;  
  3. }  
  4. a {  
  5.     color: #78b941; text-decoration:none;}  
  6. a:hover {text-decoration:underline;}  
  7. .form {width: 300px; margin: 0 auto;}  
  8. input[type= 'text'], input[type = 'password'] {  
  9.         width: 200 px;border - radius: 2 px;border: 1 px solid# CCC;padding: 10 px;color: #333; font-size: 14px; margin-top: 10px;}  
  10. input[type= 'submit'] {  
  11.         padding: 10 px 25 px 8 px;color: #fff;background - color: #78b941; text-shadow: rgba(0,0,0,0.24) 0 1px 0; font-size: 16px; box-shadow: rgba(255,255,255,0.24) 0 2px 0 0 inset,# fff 0 1 px 0 0;border: 1 px solid #6faf38; border-radius: 2px; margin-top: 10px; cursor:pointer;}  
  12. input[type= 'submit']: hover {  
  13.         background - color: #80c248;}  
Step 8

After creating all the preceding files and running the program, the following will be the output screens of the registration and login forms.

 

  1. Registration Screen.

    Registration Screen

  2. Login Screen.

    login

  3. Dashboard Screen.

    logout

If you have any kind of problem, leave it in the comments section, I will try my best to solve your issues, thanks.


Similar Articles