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'
1 2 3 4 5 6 7 | 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`
1 2 3 4 5 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <?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
1 2 3 4 5 | <?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.