object oriented programming is a coding style, mostly computer programming languages such as PHP, C#, JavaScript support oops concept.
What is Object Oriented Programming?
object-oriented programming is a paradigm you can also say it's the coding style, object-oriented programming is based on objects and classes, all programming languages like c++, c#, Java, JavaScript, and PHP support object-oriented programming paradigm, In today article we will create signup and login system using object-oriented programming PHP. follow the below steps:
classes/db.php
<?php
class db {
private $host = "localhost";
private $username = "root";
private $database = "login";
private $password = "";
protected $db;
public function __construct(){
try {
/*
* Create database connection
*/
$this->db = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->database,$this->username, $this->password);
} catch(PDOException $e){
echo "Connection Problem: ". $e->getMessage();
}
}
}
?>
classes/source.php
<?php
class source extends db {
public $Query;
/*
* Query method accept all database queries
*/
public function Query($query, $param = []){
if(empty($param)){
/*
* if we dont have the parameters
*/
$this->Query = $this->db->prepare($query);
return $this->Query->execute();
} else {
/*
* if we have some parameters
*/
$this->Query = $this->db->prepare($query);
return $this->Query->execute($param);
}
}
/*
* Count the number of rows
*/
public function CountRows(){
return $this->Query->rowCount();
}
/*
* Fetch all records from specific table
*/
public function FetchAll(){
return $this->Query->fetchAll(PDO::FETCH_OBJ);
}
/*
* fetch single row from specific table
*/
public function Single(){
return $this->Query->fetch(PDO::FETCH_OBJ);
}
}
?>
init.php
<?php
session_start();
spl_autoload_register(function($class_name){
include "classes/$class_name.php";
});
$source = new source;
?>
index.php
<?php
include "init.php";
if(isset($_SESSION['id'])){
header("location:profile.php");
}
if(isset($_POST['signup'])){
$data = [
'name' => $_POST['full_name'],
'email' => $_POST['email'],
'password' => $_POST['password'],
'confirm_password' => $_POST['confirm'],
'name_error' => '',
'email_error' => '',
'password_error' => '',
'confirm_error' => ''
];
/*
* Name validation
*/
if(empty($data['name'])){
$data['name_error'] = "Name is required";
}
/*
* Email validation
*/
if(empty($data['email'])){
$data['email_error'] = "Email is required";
} else {
if($source->Query("SELECT * FROM users WHERE email = ?", [$data['email']])){
if($source->CountRows() > 0 ){
$data['email_error'] = "Sorry email is already exist";
}
}
}
/*
* Password validations
*/
if(empty($data['password'])){
$data['password_error'] = "Password is required";
} else if(strlen($data['password']) < 5){
$data['password_error'] = "Password is too short";
}
/*
* Confirm password validations
*/
if(empty($data['confirm_password'])){
$data['confirm_error'] = "Confirm password is required";
} else if($data['password'] != $data['confirm_password']){
$data['confirm_error'] = "Confirm password is not matched";
}
/*
* Submit the form
*/
if(empty($data['name_error']) && empty($data['email_error']) && empty($data['password_error']) && empty($data['confirm_error'])){
$password = password_hash($data['password'], PASSWORD_DEFAULT);
if($source->Query("INSERT INTO users (name,email,password) VALUES (?,?,?)", [$data['name'], $data['email'], $password])){
$_SESSION['account_created'] = "Your account is successfully created";
header("location:login.php");
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Singup Form</title>
<link rel="stylesheet" href="assets/css/style.css">
<link href="https://fonts.googleapis.com/css?family=Raleway:200,300,400" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="form">
<div class="form-section">
<form action="" method="POST">
<div class="group">
<h3 class="heading">Create account</h3>
</div>
<div class="group">
<input type="text" name="full_name" class="control" placeholder="Enter Name..." value="<?php if(!empty($data['name'])): echo $data['name']; endif;?>">
<div class="error">
<?php if(!empty($data['name_error'])): ?>
<?php echo $data['name_error']; ?>
<?php endif; ?>
</div>
</div>
<div class="group">
<input type="email" name="email" class="control" placeholder="Enter Email.." value="<?php if(!empty($data['email'])): echo $data['email']; endif; ?>">
<div class="error">
<?php if(!empty($data['email_error'])): ?>
<?php echo $data['email_error']; ?>
<?php endif; ?>
</div>
</div>
<div class="group">
<input type="password" name="password" class="control" placeholder="Create Password..." value="<?php if(!empty($data['password'])): echo $data['password']; endif; ?>">
<div class="error">
<?php if(!empty($data['password_error'])): ?>
<?php echo $data['password_error']; ?>
<?php endif; ?>
</div>
</div>
<div class="group">
<input type="password" name="confirm" class="control" placeholder="Confirm Password..." value="<?php if(!empty($data['confirm_password'])): echo $data['confirm_password']; endif; ?>">
<div class="error">
<?php if(!empty($data['confirm_error'])): ?>
<?php echo $data['confirm_error']; ?>
<?php endif; ?>
</div>
</div>
<div class="group m20">
<input type="submit" name="signup" class="btn" value="Create account →">
</div>
<div class="group m20">
<a href="login.php" class="link">Already have an account ?</a>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
login.php<?php include "init.php"; ?>
<?php if(isset($_SESSION['id'])): ?>
<?php header("location:profile.php"); ?>
<?php endif; ?>
<?php
if(isset($_POST['login'])){
$data = [
'email' => $_POST['email'],
'password' => $_POST['password'],
'email_error' => '',
'password_error' => ''
];
if(empty($data['email'])){
$data['email_error'] = "Email is required";
}
if(empty($data['password'])){
$data['password_error'] = "Password is required";
}
/*
* Submit the login form
*/
if(empty($data['email_error']) && empty($data['password_error'])){
if($source->Query("SELECT * FROM users WHERE email = ?", [$data['email']])){
if($source->CountRows() > 0){
$row = $source->Single();
$id = $row->id;
$db_password = $row->password;
$name = $row->name;
if(password_verify($data['password'], $db_password)){
$_SESSION['login_success'] = "Hi ".$name . " You are successfully login";
$_SESSION['id'] = $id;
header("location:profile.php");
} else {
$data['password_error'] = "Please enter correct password";
}
} else {
$data['email_error'] = "Please enter correct email";
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Singup Form</title>
<link rel="stylesheet" href="assets/css/style.css">
<link href="https://fonts.googleapis.com/css?family=Raleway:200,300,400" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="form">
<div class="form-section">
<form action="" method="POST">
<div class="group">
<?php
if(isset($_SESSION['account_created'])):?>
<div class="success">
<?php echo $_SESSION['account_created']; ?>
</div>
<?php endif; ?>
<?php unset($_SESSION['account_created']); ?>
</div>
<div class="group">
<h3 class="heading">User Login</h3>
</div>
<div class="group">
<input type="email" name="email" class="control" placeholder="Enter Email.." value="<?php if(!empty($data['email'])): echo $data['email']; endif;?>">
<div class="error">
<?php if(!empty($data['email_error'])): ?>
<?php echo $data['email_error']; ?>
<?php endif; ?>
</div>
</div>
<div class="group">
<input type="password" name="password" class="control" placeholder="Create Password..." value="<?php if(!empty($data['password'])): echo $data['password']; endif;?>">
<div class="error">
<?php if(!empty($data['password_error'])): ?>
<?php echo $data['password_error']; ?>
<?php endif; ?>
</div>
</div>
<div class="group m20">
<input type="submit" name="login" class="btn" value="Login →">
</div>
<div class="group m20">
<a href="index.php" class="link">Create new account ?</a>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
profile.php
<?php include "init.php"; ?>
<?php if(!isset($_SESSION['id'])): ?>
<?php header("location:login.php"); ?>
<?php endif; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Profile</title>
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<div class="contain">
<?php if(isset($_SESSION['login_success'])): ?>
<div class="success">
<?php echo $_SESSION['login_success']; ?>
</div>
<?php endif; ?>
<?php unset($_SESSION['login_success']); ?>
<h2>Welcome to the dashboard</h2><hr>
<a href="logout.php">logout</a>
</div>
</body>
</html>
logout.php
<?php
session_start();
session_destroy();
header("location:login.php");
?>
style.css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Raleway', sans-serif;
}
.container {
width: 100%;
height: 100vh;
background-image: url("../img/bg.jpg");
background-size: cover;
background-repeat: no-repeat;
}
.form {
position: absolute;
top: 0;
right: 0;
width: 30%;
height: 100%;
background: #fff;
}
.form-section {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80%;
}
.group {
margin-bottom: 10px;
}
.control {
width: 100%;
padding: 12px;
border-top: 0;
border-left: 0;
border-right: 0;
border-bottom: 1px solid silver;
border-radius: 0;
transition: all 2s;
}
.control:focus {
outline: none;
border-bottom-color: blue;
}
.btn {
display: block;
width: 100%;
padding: 13px;
border-radius: 30px;
background-image: linear-gradient(to right, #08CCFD, #0379FD);
color: #fff;
border:0;
font-size: 18px;
font-weight: 200;
}
.m20 {
margin-top: 20px;
}
.link {
text-decoration: none;
color: silver;
font-weight: 300;
}
.heading {
font-size: 25px;
letter-spacing: 2px;
font-weight: 200;
}
.error {
color: red;
font-size: 14px;
}
.success {
width: 100%;
padding: 13px;
background-color: #15B309;
border-radius: 4px;
color: #fff;
}
.contain {
padding: 50px;
}
@media screen and (max-width: 765px){
.form {
width: 100%;
}
}
COMMENTS