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:
- CREATE TABLE IF NOT EXISTS `users` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `trn_date` datetime NOT NULL,
- `name` varchar(50) NOT NULL,
- `username` varchar(50) NOT NULL,
- `email` varchar(50) NOT NULL,
- `password` varchar(50) NOT NULL,
- PRIMARY KEY (`id`)
- );
Step 3Create 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):
- <? php
- define('HOST', 'localhost');
- define('USER', 'root');
- define('PASS', '');
- define('DB', 'oopregister');
- class DB
-
- {
- function __construct() {
- $con = mysql_connect(HOST, USER, PASS) or die('Connection Error! '.mysql_error());
- mysql_select_db(DB, $con) or die('DB Connection Error: ->'.mysql_error());
- }
- }
-
- class User
-
- {
- public
-
- function __construct() {
- $db = new DB;
- }
-
- public
-
- function register($trn_date, $name, $username, $email, $pass) {
- $pass = md5($pass);
- $checkuser = mysql_query("Select id from users where email='$email'");
- $result = mysql_num_rows($checkuser);
- if ($result == 0) {
- $register = mysql_query("Insert into users (trn_date, name, username, email, password) values ('$trn_date','$name','$username','$email','$pass')") or die(mysql_error());
- return $register;
- } else {
- return false;
- }
- }
-
- public
-
- function login($email, $pass) {
- $pass = md5($pass);
- $check = mysql_query("Select * from users where email='$email' and password='$pass'");
- $data = mysql_fetch_array($check);
- $result = mysql_num_rows($check);
- if ($result == 1) {
- $_SESSION['login'] = true;
- $_SESSION['id'] = $data['id'];
- return true;
- } else {
- return false;
- }
- }
-
- public
-
- function fullname($id) {
- $result = mysql_query("Select * from users where id='$id'");
- $row = mysql_fetch_array($result);
- echo $row['name'];
- }
-
- public
-
- function session() {
- if (isset($_SESSION['login'])) {
- return $_SESSION['login'];
- }
- }
-
- public
-
- function logout() {
- $_SESSION['login'] = false;
- session_destroy();
- }
- }
-
- ?>
Step 4Create a register file named register.php as in the following:
- <?php
- include_once 'class.php';
- $user = new User();
- if ($_SERVER["REQUEST_METHOD"] == "POST"){
- $trn_date = date("Y-m-d H:i:s");
- $register = $user->register($trn_date,$_REQUEST['name'],$_REQUEST['username'],$_REQUEST['email'],$_REQUEST['password']);
- if($register){
- echo "Registration Successful!";
- }
- else
- {
- echo "Entered email already exist!";
- }
- }
- ?>
- <html>
-
- <head>
- <title>Registration</title>
- <link rel="stylesheet" href="style.css" />
- </head>
-
- <body>
- <div class="form">
- <h1>Registration</h1>
- <form action="" method="post">
- <input type="text" name="name" placeholder="Please Enter Name" required />
- <br />
- <input type="text" name="username" placeholder="Please Enter Userame" required />
- <br />
- <input type="text" name="email" placeholder="Please Enter Email" required />
- <br />
- <input type="password" name="password" placeholder="Please Enter Password" required />
- <br />
- <input type="submit" name="submit" value="Register" />
- </form>
- <p>Alredy Registered?<a href="login.php"> Login Here</a></p>
- </div>
- </body>
-
- </html>
Step 5Create a login file named login.php as in the following:
- <?php
- session_start();
- include_once 'class.php';
- $user = new User();
- if ($user->session())
- {
- header("location:index.php");
- }
-
- $user = new User();
- if ($_SERVER["REQUEST_METHOD"] == "POST"){
- $login = $user->login($_REQUEST['email'],$_REQUEST['password']);
- if($login){
- header("location:index.php");
- }
- else
- {
- echo "Login Failed!";
- }
- }
- ?>
- <html>
-
- <head>
- <title>Log In</title>
- <link rel="stylesheet" href="style.css" />
- </head>
-
- <body>
- <div class="form">
- <h1>Log In</h1>
- <form action="" method="post">
- <input type="text" name="email" placeholder="Please Enter Email" required />
- <br />
- <input type="password" name="password" placeholder="Please Enter Password" required />
- <br />
- <input type="submit" name="submit" value="Login" />
- </form>
- <p>Not registered yet?<a href="register.php"> Register Here</a></p>
- </div>
- </body>
-
- </html>
Step 6Create an index file named index.php as in the following:
- <?php
- session_start();
- include_once 'class.php';
- $user = new User;
- $id = $_SESSION['id'];
- if (!$user->session()){
- header("location:login.php");
- }
- if (isset($_REQUEST['q'])){
- $user->logout();
- header("location:login.php");
- }
- ?>
- <html>
-
- <head>
- <title>Dashboard</title>
- <link rel="stylesheet" href="style.css" />
- </head>
-
- <body>
- <div class="form">
- <h1>Welcome <?php $user->fullname($id);?></h1>
- <p align="right"><a href="?q=logout">LOGOUT</a></p>
- </div>
- </body>
-
- </html>
Step 7Create a style file named style.css as in the following:
- body {
- font - family: Arial, Sans - Serif;
- }
- a {
- color: #78b941; text-decoration:none;}
- a:hover {text-decoration:underline;}
- .form {width: 300px; margin: 0 auto;}
- input[type= 'text'], input[type = 'password'] {
- width: 200 px;border - radius: 2 px;border: 1 px solid# CCC;padding: 10 px;color: #333; font-size: 14px; margin-top: 10px;}
- input[type= 'submit'] {
- 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;}
- input[type= 'submit']: hover {
- background - color: #80c248;}
Step 8After creating all the preceding files and running the program, the following will be the output screens of the registration and login forms.
- Registration Screen.
- Login Screen.
- Dashboard Screen.
If you have any kind of problem, leave it in the comments section, I will try my best to solve your issues, thanks.