In this tutorial we will learn how user can register and login using PHP Oops concepts and use function and Session.
File Structure for this tutorial:
- Create Database and table.
- Create Database connection file.
- Create User Registration or Sign Up Form.
- Create User Login or Sign In Form.
- Create a Students class file for function like Registration, login, email and phone number availability
- Create logout page file for logout.
See also
Step 1 : Create Database and table.
--
-- Database: `scholarship`
-- Table structure for table `students`
--
CREATE TABLE `students` (
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`phone` varchar(50) NOT NULL,
`registration_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Step 2 : Create Database Connection file using OOPs Concepts
database.php
<?php
class Database{
protected $localhost = "localhost";
protected $servername = "root";
protected $password = "";
protected $database = "scholarship";
public $con;
public function connection(){
// Create connection
$this->con = new mysqli($this->localhost, $this->servername, $this->password, $this->database);
// Check connection
if ($this->con->connect_error) {
die("Connection failed: " . $this->con->connect_error);
}
return $this->con;
}
}
Step 3: Create User Registration or Sign Up Form
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Create User Registration and Login using PHP Oops Concepts</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<?php
include_once "class/students.php";
$studentObj = new Students();
$success = "";
$error = "";
if (isset($_POST['submit'])) {
$newdata['name'] = $_POST['name'];
$newdata['email'] = $_POST['email'];
$newdata['password'] = md5($_POST['password']);
$newdata['phone'] = $_POST['phone'];
$newdata['registration_date'] = date('Y-m-d');
if (!$studentObj->isUserPhoneNumberExists($newdata['phone'])) {
if (!$studentObj->isUserExist($newdata['email'])) {
if ($studentObj->registration($newdata)) {
$success = "Your Registration Successfully Please login";
} else {
$error = "Registration failed please try again!";
}
} else {
$error = "Email already exists! Please try again.";
}
} else {
$error = "Phone No. already exists! Please try again.";
}
}
?>
<body class="hold-transition register-page">
<div class="register-box">
<div class="register-logo">
<a href=""><b>Student</b> OSM</a>
</div>
<?php
if (!empty($error)) {
echo "<div class='alert alert-danger alert-dismissible'>
<button type='button' class='close' data-dismiss='alert'>×</button>$error
</div>";
} elseif (!empty($success)) {
echo "<div class='alert alert-success alert-dismissible'>
<button type='button' class='close' data-dismiss='alert'>×</button>$success
</div>";
}
?>
<div class="card">
<div class="card-body register-card-body">
<p class="login-box-msg">Registration a new Student</p>
<form action="" method="post">
<div class="input-group mb-3">
<input type="text" name="name" class="form-control" placeholder="Full name" required="">
<div class="input-group-append">
<div class="input-group-text">
<span class="fa fa-user"></span>
</div>
</div>
</div>
<div class="input-group mb-3">
<input type="email" name="email" class="form-control" placeholder="Email" required="">
<div class="input-group-append">
<div class="input-group-text">
<span class="fa fa-envelope"></span>
</div>
</div>
</div>
<div class="input-group mb-3">
<input type="password" name="password" class="form-control" placeholder="Password" required="">
<div class="input-group-append">
<div class="input-group-text">
<span class="fa fa-lock"></span>
</div>
</div>
</div>
<div class="input-group mb-3">
<input type="text" name="phone" class="form-control" placeholder="Phone Number" required="" maxlength="10">
<div class="input-group-append">
<div class="input-group-text">
<span class="fa fa-phone"></span>
</div>
</div>
</div>
<div class="row">
<div class="col-8">
<a href="login.php" class="text-center">Already have an account?</a>
</div>
<div class="col-md-4">
<input type="submit" name="submit" class="btn btn-primary btn-block" value="Sign Up">
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Output
Step 4: Create User Login or Signup Form
login.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Create User Registration and Login using PHP Oops Concepts</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<?php
include_once "class/students.php";
if(isset($_SESSION['id'])) {
header("Location:home.php");
}
$studentObj = new Students();
$success = "";
$error = "";
if (isset($_POST['submit'])) {
$newdata['email'] = $_POST['email'];
$newdata['password'] = $_POST['password'];
if ($studentObj->login($newdata)) {
if (!isset($_SESSION['id'])) {
header("Location:student_profile.php");
} else {
header("Location:home.php");
}
}else{
$error = "Incorrect Email or Password";
}
}
?>
<body class="hold-transition login-page">
<div class="login-box">
<div class="login-logo">
<a href=""><b>Student</b> OSM</a>
</div>
<?php
if (!empty($error)){
echo "<div class='alert alert-danger alert-dismissible'>
<button type='button' class='close' data-dismiss='alert'>×</button>$error
</div>";
}
?>
<div class="card">
<div class="card-body login-card-body">
<p class="login-box-msg">Sign in to start your session</p>
<form action="" method="post">
<div class="input-group mb-3">
<input type="text" name="email" class="form-control" placeholder="Email" required="">
<div class="input-group-append">
<div class="input-group-text">
<span class="fa fa-envelope"></span>
</div>
</div>
</div>
<div class="input-group mb-3">
<input type="password" name="password" class="form-control" placeholder="Password" required="">
<div class="input-group-append">
<div class="input-group-text">
<span class="fa fa-lock"></span>
</div>
</div>
</div>
<div class="row">
<div class="col-8">
<div class="icheck-primary">
<a href="index.php" class="text-center">Don't have an account?</a>
</div>
</div>
<div class="col-4">
<input type="submit" name="submit" class="btn btn-primary btn-block" value="Sign In">
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Output
Step 5: Create a Students class file for function like Registration, login, email and phone number availability
students.php
<?php
session_start();
include_once "database.php";
class Students {
public $studentTable;
public $dbObj;
public $con;
public function __construct() {
$this->studentTable = "students";
$this->dbObj = new Database();
$this->con = $this->dbObj->connection();
}
/* student registration method */
public function registration() {
$name = $_POST['name'];
$email = $_POST['email'];
$password = md5($_POST['password']);
$phone = $_POST['phone'];
$registr_date = date('Y-m-d');
$query ="INSERT INTO $this->studentTable (name, email, password, phone, registration_date)
VALUES('$name', '$email', '$password', '$phone', '$registr_date')";
if ($this->con->query($query)) {
return true;
} else {
return false;
}
}
/* student login method */
public function login() {
$email = $_POST['email'];
$password = md5($_POST['password']);
$query = "SELECT * FROM $this->studentTable WHERE email = '$email' && password = '$password'";
$result = $this->con->query($query);
while ($student_data = $result->fetch_assoc()) {
$_SESSION['id'] = $student_data['id'];
$_SESSION['name'] = $student_data['name'];
}
if ($result->num_rows > 0) {
return true;
} else {
return false;
}
}
/* check If email is exists */
public function isUserExist($email) {
$query = "SELECT * FROM $this->studentTable WHERE email ='$email'";
$result = $this->con->query($query);
if ($result->num_rows > 0) {
return true;
} else {
return false;
}
}
/*check if phone number is exists */
public function isUserPhoneNumberExists($phone){
$query = "SELECT * FROM $this->studentTable WHERE phone = '$phone'";
$result = $this->con->query($query);
if ($result->num_rows > 0) {
return true;
}else {
return false;
}
}
}
$studentObj = new Students();
Step 6 : Create logout page file for logout.
logout.php
<?php
session_start();
session_destroy();
session_unset($_SESSION['usre_id']);
session_unset($_SESSION['name']);
header("Location:login.php");
?>
You can always support by sharing on social media or recommending my blog to your friends and colleagues.
If you have any suggestions / problems about this tutorial, please comment on the form below.😊
Post a Comment