PHP OOP Login and Registration System
Hi Guys Today we are going to Learn how to Create Login and Registration System in PHP OOP..Learn PHP SESSION
![]() |
PHP OOP Login and Registration System |
Database name: codenair
Table Name: users
table structure for table 'users'
CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, PRIMARY KEY(id) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
Dumping data for table `users`
INSERT INTO `users` (`id`, `username`, `email`, `password`) VALUES (5, 'XYZ', 'xyz@gmail.com', '1122'), (6, 'abcd', 'abcd360@gmail.com', '1234'), (7, 'codenair', 'Anjankumardhali6@gmail.com', '1234'), (10, 'ANJAN', 'Anjankumardhali6@gmail.com', '1234');
Files Structure:
create 5 files called..
1. database.php
2. login.php
3. profile.php
4. registration.php
5. logout.php
Copy and Save the below codes for certain file..
1. database.php
<?php session_start(); class DBFunction{ protected $host="localhost"; protected $user="root"; protected $pass=""; protected $dbname="codenair"; public $link; private $error; public function __construct(){ $this->connectDB(); } //connecting Database public function connectDB(){ $this->link = new mysqli($this->host,$this->user,$this->pass,$this->dbname); if(!$this->link){ $this->error= "Connection Failed".$this->link->connect_error; return false; } } //Login User public function login($username,$password){ $result=mysqli_query($this->link,"SELECT*FROM users WHERE username='$username' AND password='$password'"); $row=$result->fetch_assoc(); $count=$result->num_rows; if($count==1){ $_SESSION['user']=array( 'username'=>$row['username'], 'password'=>$row['password'], 'email'=>$row['email'] ); return TRUE; }else{ return FALSE; } } //Email Check public function isEmailExist($email){ $result=mysqli_query($this->link,"SELECT*FROM users WHERE email='$email'"); $count=$result->num_rows; if($count==0){ return true; } } //UserName Check public function isUserExist($username){ $result=mysqli_query($this->link,"SELECT*FROM users WHERE username='$username'"); $count=$result->num_rows; if($count==0){ return true; } } //New User Registration public function registration($username,$email,$password){ $result=mysqli_query($this->link,"INSERT INTO users(username,email,password)VALUE('$username','$email','$password')"); if($result){ return $result; }else{ return false; } } } ?>
2. login.php
<html> <head> <title>PHP OOP Login</title> </head> <h2>PHP OOP Login</h2> <form method="POST" action=""> <table> <tr> <td>UserName:</td> <td><input type="text" name="username"/></td> </tr> <tr> <td>PassWord</td> <td><input type="text" name="password"/></td> </tr> <tr> <td></td> <td><input type="submit" name="login" value="Login Now"/></td> </tr> </table> </form> </html> <div><a href="registration.php"><b>Create New Account</b></a></div> <?php include "database.php"; //If user Already Logged if(!empty($_SESSION['user'])){ header('location:profile.php'); } $db= new DBFunction(); if(isset($_POST['login'])){ $username=mysqli_real_escape_string($db->link,$_POST['username']); $password=mysqli_real_escape_string($db->link,$_POST['password']); if($username=='' || $password==''){ echo "Fields Must Not be empty"; }else{ $result=$db->login($username,$password); if($result){ header('location:profile.php'); }else{ echo "Your UserName or PassWord Did't Match"; } } } ?>
3. profile.php
<?php session_start(); if(!empty($_SESSION['user'])){?> <?php print_r($_SESSION['user']);?> <table border="1" cellspacing="0"> <tr> <th>UserName</th> <th>PassWord</th> <th>Email</th> </tr> <tr> <td><?php echo $_SESSION['user']['username'];?></td> <td><?php echo $_SESSION['user']['password'];?></td> <td><?php echo $_SESSION['user']['email'];?></td> </tr> </table> </table> <a href="logout.php">Logout Now</a> <?php }else{ header('location:login.php'); } ?>
4. registration.php
<?php include "database.php"; //If user Already Logged if(!empty($_SESSION['user'])){ header('location:profile.php'); } $db= new DBFunction(); if(isset($_POST['login'])){ $username=mysqli_real_escape_string($db->link,$_POST['username']); $password=mysqli_real_escape_string($db->link,$_POST['password']); $email=mysqli_real_escape_string($db->link,$_POST['email']); if($username=='' || $password=='' ||$email==''){ $error= "Fields Must Not be empty"; }else{ //Initilizzing Variable $errors=array(); //Exsiting Email Check $emailcheck=$db->isEmailExist($email); if(!$emailcheck){ $emailerror='Email Aready Taken'; array_push($errors,'Email Taken'); } //Exsiting UserName Check $usercheck=$db->isUserExist($username); if(!$usercheck){ $nameerror='UserName Already Taken'; array_push($errors,'UserName Taken'); } if(count($errors)==0){ //Saving User Information into Database $register=$db->registration($username,$email,$password); if($register){ $success='Registration Successfull'; } }else{ $error= 'Please Fill The form with Correct Information'; } } } ?> <html> <head> <title>PHP OOP Registration</title> </head> <h2>PHP OOP Registration</h2> <form method="POST" action=""> <table> <tr> <td>UserName:</td> <td><input type="text" name="username"/></td> <td><?php if(isset($nameerror)){ echo $nameerror;}?></div> </td> </tr> <tr> <td>PassWord</td> <td><input type="text" name="password"/></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email"/></td> <td><?php if(isset($emailerror)){ echo $emailerror;}?></div> </td> </tr> <tr> <td></td> <td><input type="submit" name="login" value="Login Now"/></td> </tr> </table> </form> <?php if(isset($success)){echo $success;}?> <?php if(isset($error)){echo $error;}?> </html> <div><a href="login.php"><b>Login Now</b></a></div>
5. logout.php
<?php session_start(); session_destroy(); header('location:login.php'); ?>
That's it Friends how to create login and registration system in php oop..
if you like this post please share and don't forget to comment..
Thank You..
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.