Menu

Simple Login & Registration system using PHP OOPS, MySQL and Bootstrap 4

Simple Login & Registration system using PHP OOPS, MySQL and Bootstrap 4

Registration and Login is one of the primary module in any data management system. In this tutorial, we will learn how to develop PHP Login script by using PHP Object Oriented Programming concept with MySQL and Bootstrap 4. Using password authentication method password_hash() and password_verify() algorithm.We will cover this tutorial in easy steps with live demo to develop complete Login and Registration. We will also provide to download source code of live demo.

Step 1: Create MySQL Database and Tables

// Table structure for table `user`
CREATE TABLE `user` (
`user_id` int(12) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`user_name` varchar(50) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`status` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

// Indexes for table `user`
ALTER TABLE `user`
ADD PRIMARY KEY (`user_id`),
ADD UNIQUE KEY `email` (`email`);

// AUTO_INCREMENT for table `user`
ALTER TABLE `user`
MODIFY `user_id` int(12) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
?>


Step 2: Create DBConnection Class
Create a Class file named "DBConnection.php" define constant and connect DB code.

/**
* @package Login & Registration
*
* @author TechArise Team
*
* @email info@techarise.com
*
*/

if(!isset($_SESSION))
{
session_start();
}
date_default_timezone_set('Asia/Kolkata');
$root = "http://" . $_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);
$constants['base_url'] = $root;
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'test_DB');

define('SITE_URL', $constants['base_url']);
define('HTTP_BOOTSTRAP_PATH', $constants['base_url'] . 'assets/vendor/');
define('HTTP_CSS_PATH', $constants['base_url'] . 'assets/css/');
define('HTTP_JS_PATH', $constants['base_url'] . 'assets/js/');
// windows path
//define('BASH_PATH', 'C:/xampp/htdocs/login-registration-system-using-php-oops-mysql/');
// ubuntu path
//define('BASH_PATH', '/var/www/login-registration-system-using-php-oops-mysql/');
// MAC path
define('BASH_PATH', '/Applications/XAMPP/htdocs/login-registration-system-using-php-oops-mysql/');

class DBConnection {
private $_con;
function __construct(){
$this->_con = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD,DB_DATABASE);
if ($this->_con->connect_error) die('Database error -> ' . $this->_con->connect_error);
}
// return Connection
function returnConnection() {
return $this->_con;
}
}
?>


Step 3: Create a Class file
Next create a file named User.php & include DBconnection file also:


/**
* @package Login & Registration
*
* @author TechArise Team
*
* @email info@techarise.com
*
*/
include "DBConnection.php";
class User
{
protected $db;
private $_userID;
private $_name;
private $_email;
private $_username;
private $_password;
public function setUserID($userID) {
$this->_userID = $userID;
}
public function setName($name) {
$this->_name = $name;
}
public function setEmail($email) {
$this->_email = $email;
}
public function setUsername($username) {
$this->_username = $username;
}
public function setPassword($password) {
$this->_password = $password;
}
public function __construct() {
$this->db = new DBConnection();
$this->db = $this->db->returnConnection();
}
// User registration Method
public function userRegistration() {
$password = $this->hash($this->_password);
$query = 'SELECT * FROM user WHERE user_name="'.$this->_username.'" OR email="'.$this->_username.'"';
$result = $this->db->query($query) or die($this->db->error);
$count_row = $result->num_rows;
if($count_row == 0) {
$query = 'INSERT INTO user SET user_name="'.$this->_username.'", password="'.$password.'", name="'.$this->_name.'", email="'.$this->_email.'", status="1"';
$result = $this->db->query($query) or die($this->db->error);
return true;
} else {
return false;
}
}

// User Login Method
public function doLogin() {
$query = 'SELECT user_id,password from user WHERE email="'.$this->_username.'" or user_name="'.$this->_username.'"';
$result = $this->db->query($query) or die($this->db->error);
$user_data = $result->fetch_array(MYSQLI_ASSOC);
print_r($user_data);
$count_row = $result->num_rows;
if ($count_row == 1) {
if (!empty($user_data['password']) && $this->verifyHash($this->_password, $user_data['password']) == TRUE) {
$_SESSION['login'] = TRUE;
$_SESSION['user_id'] = $user_data['user_id'];
return TRUE;
} else {
return FALSE;
}
}
}

// get User Information
public function getUserInfo() {
$query = "SELECT user_id, name, email FROM user WHERE user_id = ".$this->_userID;
$result = $this->db->query($query) or die($this->db->error);
$user_data = $result->fetch_array(MYSQLI_ASSOC);
return $user_data;
}

//get Session
public function getSession() {
if(!empty($_SESSION['login']) && $_SESSION['login']==TRUE) {
return TRUE;
} else {
return FALSE;
}
}
// logout method
public function logout() {
$_SESSION['login'] = FALSE;
unset($_SESSION);
session_destroy();
}

// password hash
public function hash($password) {
$hash = password_hash($password, PASSWORD_DEFAULT);
return $hash;
}

// password verify
public function verifyHash($password, $vpassword) {
if (password_verify($password, $vpassword)) {
return TRUE;
} else {
return FALSE;
}
}
}
?>

Step 4: Create registration file
Create a file named registration.php

function __autoload($class) {
include "include/$class.php";
}
$user = new User();

if ($user->getSession()===TRUE) {
header("location:home.php");
}
$status = '';

$errors = array();
//If our form has been submitted.
if(isset($_POST['submit'])){
extract($_POST);
//Get the values of our form fields.
$fullname = isset($fullname) ? $fullname : null;
$uemail = isset($uemail) ? $uemail : null;
$uname = isset($uname) ? $uname : null;
$password = isset($password) ? $password : null;

//Check the name and make sure that it isn't a blank/empty string.
if(strlen(trim($fullname)) === 0){
//Blank string, add error to $errors array.
$errors[] = "You must enter your fullname!";
}
if(strlen(trim($uname)) === 0){
//Blank string, add error to $errors array.
$errors[] = "You must enter your user name!";
}
if(strlen(trim($password)) === 0){
//Blank string, add error to $errors array.
$errors[] = "You must enter your password!";
}
//email address is valid.
if(!filter_var($uemail, FILTER_VALIDATE_EMAIL)) {
//$email is not a valid email. Add error to $errors array.
$errors[] = "That is not a valid email address!";
}

//If our $errors array is empty, we can assume that everything went fine.
if(empty($errors)){
//insert data into database.
$user->setName($fullname);
$user->setEmail($uemail);
$user->setUsername($uname);
$user->setPassword($password);
$register = $user->userRegistration();
if ($register) {
$status = "
Registration successful Click here to login
";
} else {
$status = "
Registration failed. Email or Username already exits please try again.
";
}
}
}

?>
include('templates/header.php');
?>


Simple Login & Registration system using PHP & MySQL








    foreach ($errors as $value) {
    echo '
  • '.$value.'
  • ' ;
    }
    ?>




include('templates/footer.php');
?>



Step 5: Create a file
Create a view file named index.php(Login File)


function __autoload($class) {
include "include/$class.php";
}
$msg = '';
$user = new User();
if (isset($_POST['submit'])) {
extract($_POST);
$user->setUsername($emailusername);
$user->setPassword($password);
$login = $user->doLogin();
if ($login) {
header("location:home.php");
} else {
$msg = 'Wrong username or password';
}
}
?>
include('templates/header.php');
?>


Simple Login & Registration system using PHP & MySQL






echo '
Wrong username or password
';
} ?>




















Register



include('templates/footer.php');
?>



Step 6: Create a file
Create a view file named home.php


function __autoload($class) {
include "include/$class.php";
}
$user = new User();
if(!empty($_SESSION['user_id'])){
$uid = $_SESSION['user_id'];
}
if ($user->getSession()===FALSE) {
header("location:index.php");
}
if (isset($_GET['q'])) {
$user->logout();
header("location:index.php");
}

$user->setUserID($uid);
$userInfo = $user->getUserInfo();
?>
include('templates/header.php');
?>


Simple Login & Registration system using PHP & MySQL







Full Name:


Email:




include('templates/footer.php');
?>

Demo  [sociallocker] Download[/sociallocker]

Ads middle content1

Ads middle content2