BoomBox Community

Software Push - Web | IT | Coding | Tutorial

  • HOW TO
  • TUTORIALS
    • DEMO
    • HTML
    • CSS
  • JAVASCRIPT
    • JQUERY
    • Ajax
    • REACT NATIVE
    • Node.js
  • PHP
    • CODEIGNITER
    • LARAVEL
    • WORDPRESS
    • Oracle
  • PYTHON
    • HTACCESS
  • Snippets
    • Alert
  • Chat Gpt
  • Donate
sections
  • Uncategorized
  • Videos
BoomBox Community

Software Push - Web | IT | Coding | Tutorial

  1. Home
  2. HOW TO
  3. How to make login system using php opps method
2 months ago 2 months ago

HOW TO, PHP

How to make login system using php opps method


Developer
by Developer 2 months ago2 months ago
0

I can certainly help you with a basic example of a login system using object-oriented programming (OOP) in PHP. Here’s some sample code for a login class that could be used in a website:

class Login {
    private $username;
    private $password;

    public function __construct($username, $password) {
        $this->username = $username;
        $this->password = $password;
    }

    public function checkCredentials() {
        // Connect to database and check if the provided
        // username and password match a user in the system
        $db = new PDO('mysql:host=localhost;dbname=mydb', 'dbuser', 'dbpass');
        $stmt = $db->prepare('SELECT * FROM users WHERE username = ? AND password = ?');
        $stmt->execute([$this->username, $this->password]);
        return $stmt->fetch();
    }

    public function login() {
        $user = $this->checkCredentials();
        if ($user) {
            // Start a session and set a variable to indicate that the user is logged in
            session_start();
            $_SESSION['logged_in'] = true;
            $_SESSION['user'] = $user;
            return true;
        } else {
            return false;
        }
    }

    public function logout() {
        session_start();
        session_destroy();
    }
}

You can use this class by creating a new instance of the Login class and calling the login() method, passing in the username and password provided by the user. For example:

$login = new Login($_POST['username'], $_POST['password']);
if ($login->login()) {
    // Redirect to the dashboard or some other page
    header('Location: dashboard.php');
    exit;
} else {
    // Display an error message
    echo 'Invalid username or password';
}

This can be implemented in many ways, you can use database to store the data of your blog and use php to fetch and display the data in the frontend. You can also use a CMS like wordpress which is built on php and allows you to create blog easily.

Read Also:

  • Notification system using jQuery, PHP, and MySQL
  • OTP login PHP Ajax
  • Fetch Data using Oracle and PHP
  • Uploading and Downloading files on Onedrive with PHP
  • How to call a API using PHP CURL
  • How to convert an image to pdf using php

PHP oops login Example 2

Create Table tblusers

CREATE TABLE `tblusers` (
  `id` int(11) NOT NULL,
  `FullName` varchar(120) DEFAULT NULL,
  `Username` varchar(120) DEFAULT NULL,
  `UserEmail` varchar(200) DEFAULT NULL,
  `Password` varchar(250) DEFAULT NULL,
  `RegDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Create function.php file

<?php
define('DB_SERVER','localhost');
define('DB_USER','root'); 
define('DB_PASS' ,''); 
define('DB_NAME', 'userdb');
class DB_con
{
function __construct()
{
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
$this->dbh=$con;

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}


public function usernameavailblty($uname) {
$result =mysqli_query($this->dbh,"SELECT Username FROM tblusers WHERE Username='$uname'");
return $result;

}


public function registration($fname,$uname,$uemail,$pasword)
{
$ret=mysqli_query($this->dbh,"insert into tblusers(FullName,Username,UserEmail,Password) values('$fname','$uname','$uemail','$pasword')");
return $ret;
}


public function signin($uname,$pasword)
{
$result=mysqli_query($this->dbh,"select id,FullName from tblusers where Username='$uname' and Password='$pasword'");
return $result;
}

}
?>
<?php

include_once('function.php');

$userdata=new DB_con();
if(isset($_POST['submit']))
{

$fname=$_POST['fullname'];
$uname=$_POST['username'];
$uemail=$_POST['email'];
$pasword=md5($_POST['password']);

$sql=$userdata->registration($fname,$uname,$uemail,$pasword);
if($sql)
{

echo "<script>alert('Registration successfull.');</script>";
echo "<script>window.location.href='signin.php'</script>";
}
else
{

echo "<script>alert('Something went wrong. Please try again');</script>";
echo "<script>window.location.href='signin.php'</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- This file has been downloaded from Bootsnipp.com. Enjoy! -->
<title>User Registration using PHP OOPs Concept</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="assests/style.css" rel="stylesheet">
<script src="assests/jquery-1.11.1.min.js"></script>
<script src="assests/bootstrap.min.js"></script>
<script>
function checkusername(va) {
$.ajax({
type: "POST",
url: "check_availability.php",
data:'username='+va,
success: function(data){
$("#usernameavailblty").html(data);
}
});

}
</script>
</head>
<body>
<form class="form-horizontal" action='' method="POST">
<fieldset>
<div id="legend" align="center">
<legend class="">User Registration using PHP OOPs Concept</legend>
</div>

<div class="control-group">
<!-- Fullname -->
<label class="control-label" for="username">Fullname</label>
<div class="controls">
<input type="text" id="username" name="fullname" placeholder="" class="input-xlarge" required="true">
</div>
</div>


<div class="control-group">
<!-- Username -->
<label class="control-label" for="username">Username</label>
<div class="controls">
<input type="text" id="username" name="username" onblur="checkusername(this.value)" class="input-xlarge" required="true">
<span id="usernameavailblty"></span>
</div>
</div>

<div class="control-group">
<!-- E-mail -->
<label class="control-label" for="email">E-mail</label>
<div class="controls">
<input type="email" id="email" name="email" placeholder="" class="input-xlarge" required="true">
</div>
</div>

<div class="control-group">
<!-- Password-->
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" id="password" name="password" placeholder="" class="input-xlarge" required="true">
</div>
</div>

<div class="control-group">
<!-- Button -->
<div class="controls">
<button class="btn btn-success" type="submit" id="submit" name="submit">Register</button>
</div>
</div>

<div class="control-group">
<div class="controls">
Already registered <a href="#">Signin</a>
</div>
</div>

</fieldset>
</form>
<script >
</script>
</body>
</html>

check_availability.php

<?php

include_once('function.php');

$uusername=new DB_con();

$uname= $_POST["username"]; 

$sql=$uusername->usernameavailblty($uname);
$num=mysqli_num_rows($sql);
if($num > 0)
{
echo "<span style='color:red'> Username already associated with another account .</span>";
echo "<script>$('#submit').prop('disabled',true);</script>";
} else{

echo "<span style='color:green'> Unsername available for Registration .</span>";
echo "<script>$('#submit').prop('disabled',false);</script>";
}?>

signin.php

<?php

include_once('function.php');

$userdata=new DB_con();
if(isset($_POST['submit']))
{

$fname=$_POST['fullname'];
$uname=$_POST['username'];
$uemail=$_POST['email'];
$pasword=md5($_POST['password']);

$sql=$userdata->registration($fname,$uname,$uemail,$pasword);
if($sql)
{

echo "<script>alert('Registration successfull.');</script>";
echo "<script>window.location.href='signin.php'</script>";
}
else
{

echo "<script>alert('Something went wrong. Please try again');</script>";
echo "<script>window.location.href='signin.php'</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>User Registration using PHP OOPs Concept</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="assests/style.css" rel="stylesheet">
<script src="assests/jquery-1.11.1.min.js"></script>
<script src="assests/bootstrap.min.js"></script>
<script>
function checkusername(va) {
$.ajax({
type: "POST",
url: "check_availability.php",
data:'username='+va,
success: function(data){
$("#usernameavailblty").html(data);
}
});

}
</script>
</head>
<body>
<form class="form-horizontal" action='' method="POST">
<fieldset>
<div id="legend" align="center">
<legend class="">User Registration using PHP OOPs Concept</legend>
</div>

<div class="control-group">
<!-- Fullname -->
<label class="control-label" for="username">Fullname</label>
<div class="controls">
<input type="text" id="username" name="fullname" placeholder="" class="input-xlarge" required="true">
</div>
</div>

<div class="control-group">
<!-- Username -->
<label class="control-label" for="username">Username</label>
<div class="controls">
<input type="text" id="username" name="username" onblur="checkusername(this.value)" class="input-xlarge" required="true">
<span id="usernameavailblty"></span>
</div>
</div>

<div class="control-group">
<!-- E-mail -->
<label class="control-label" for="email">E-mail</label>
<div class="controls">
<input type="email" id="email" name="email" placeholder="" class="input-xlarge" required="true">
</div>
</div>

<div class="control-group">
<!-- Password-->
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" id="password" name="password" placeholder="" class="input-xlarge" required="true">
</div>
</div>

<div class="control-group">
<!-- Button -->
<div class="controls">
<button class="btn btn-success" type="submit" id="submit" name="submit">Register</button>
</div>
</div>

<div class="control-group">
<div class="controls">
Already registered <a href="#">Signin</a>
</div>
</div>

</fieldset>
</form>
<script >
</script>
</body>
</html>

welcome.php

<?php
session_start();
if(strlen($_SESSION['uid'])=="")
{
header('location:logout.php');
} else {
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>User Registraion using PHP OOPs Concept</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="assests/style.css" rel="stylesheet">
<script src="assests/jquery-1.11.1.min.js"></script>
<script src="assests/bootstrap.min.js"></script>
</head>
<body>
<form class="form-horizontal" action='' method="POST">
<fieldset>
<div id="legend">
<legend class="" align="center">Welcome Back : <?php echo $_SESSION['fname'];?></legend>
</div>

<div class="control-group" align="center">
<!-- Button -->
<div class="controls">
<a href="logout.php" class="btn btn-success" type="submit" name="signin">Logout</a>
</div>
</div>

</fieldset>
</form>
<script >
</script>
</body>
</html>
<?php } ?>

logout.php

<?php
session_start();
session_destroy();
header('location:signin.php');
?>
Post Views: 20

Post Pagination

  • Previous PostPrevious
  • Next PostNext

login system with oops, php login with oops, PHP oops login

Like it? Share with your friends!

0
Developer

Posted by Developer

0 Comments

Cancel reply

You must be logged in to post a comment.

  • Previous Post
    Build Chat Application With Socket.io Using Node Js Express
    by Developer
  • Next Post
    A ChatGPT Job Interview for a Scrum Master Position
    by Developer

More From: HOW TO

  • HOW TO, PHPlogin system with oops, php login with oops, PHP oops login

    How to make login system using php opps method

  • HOW TO, JAVASCRIPTjavascript motion control, Motion Controls In The Browser, Motion Controls In The Browser with javascript

    Motion Controls In The Browser using javascript

  • Ajax, HOW TO, HTML, JQUERY, PHPjquery notifiction, Notification system jQuery, php, php notifiction without reload page

    Notification system using jQuery, PHP, and MySQL

  • HOW TO, PYTHONMERGE TWO LISTS INTO A DICTIONARY PYTHON

    MERGE TWO LISTS INTO A DICTIONARY PYTHON

  • HOW TO, PHPotp login php ajax

    OTP login PHP Ajax

  • HOW TO, Oracle, PHPFetch Data using Oracle and PHP

    Fetch Data using Oracle and PHP

DON'T MISS

  • Uncategorized

    Python read and write csv file

  • HTAccess

    Active Gzip Compression using htaccess

  • JQUERYScroll Page Horizontally, Scroll Page Horizontally using jquery

    Scroll Page Horizontally With Mouse Wheel

  • PHPIntegrate Paytm Payment Gateway, payment gateway in php, paytm payment, paytm payment gateway in php

    How to Integrate Paytm Payment Gateway In PHP?

  • LARAVEL, PHPScaffolding in Laravel, Scaffolding in Laravel 9, Vue Js

    how Scaffolding using Vue Js Auth in Laravel 9

  • JAVASCRIPT, Node.jsChat Application, Chat Application With js express, Chat Application With node js, Chat Application With Socket.io

    Build Chat Application With Socket.io Using Node Js Express

Recent Post

  • Python read and write csv file

    2 weeks ago2 weeks ago
  • Active Gzip Compression using htaccess

    2 months ago2 months ago
  • Scroll Page Horizontally With Mouse Wheel

    2 months ago2 months ago
  • How to Integrate Paytm Payment Gateway In PHP?

    2 months ago2 months ago
  • how Scaffolding using Vue Js Auth in Laravel 9

    2 months ago2 months ago
  • Build Chat Application With Socket.io Using Node Js Express

    2 months ago2 months ago

add csv to mysql with codeigniter add Data From Excel & CSV to mysql Using Codeigniter android app android vpn app app Build a Node.js API Proxy call a API using PHP CURL change the color of command prompt Chat Application Chat Application With js express Chat Application With node js Chat Application With Socket.io ChatGPT Job Interview ChatGPT Job Interview for a Scrum Master ChatGPT Job Interview question Clear input value using Jquery convert a date number string to word convert image to pdf convert image to pdf using php css csv file into array in php date number to string detect input field value changes in angular download file from url using jquery download file using jquery Downloading files on Onedrive with PHP Fetch Data using Oracle and PHP google map google map api google map api with javascript hangman game with javascript How to add Data From Excel how to change the color of command prompt using python how to clear input value using jquery how to download file from url how to download file from url using jquery how to download file using jquery how to make game how to make game with javascript how to make hangman game with javascript how to make vpn app how to pause play video on hover how to read csv file How to Upload And save File How to Upload And save File To PostgreSQL with PHP how to upload file in php html html css http request javascript Image Illustration Filter using css Insert Data Using Oracle Insert Data Using Oracle PHP Integrate Paytm Payment Gateway javascript javascript motion control jquery jquery ajax jquery notifiction jquery validation login system with oops Motion Controls In The Browser Motion Controls In The Browser with javascript mouce hover video play Notification system jQuery otp login php ajax payment gateway in php paytm payment paytm payment gateway in php php php curl php login with oops php notifiction without reload page PHP oops login python python AI react react native read csv file into an array remove class from foreach using jquery remove class using jquery remove class with jquery remove duplicate value from array remove duplicate value from array in php remove duplicate value from array using php rotate text rotate text in css rotate text using html and css Scaffolding in Laravel Scaffolding in Laravel 9 Scroll Page Horizontally Scroll Page Horizontally using jquery Stock Prediction AI with python Uploading and Downloading files on Onedrive validate email validate email in JavaScript video play using javascript vonvert image to pdf with php vpm app with android Vue Js wordpress

  • Python read and write csv file

    Python read and write csv file

  • Active Gzip Compression using htaccess

    Active Gzip Compression using htaccess

  • Scroll Page Horizontally With Mouse Wheel

    Scroll Page Horizontally With Mouse Wheel

  • How to Integrate Paytm Payment Gateway In PHP?

    How to Integrate Paytm Payment Gateway In PHP?

  • how Scaffolding using Vue Js Auth in Laravel...

    how Scaffolding using Vue Js Auth in Laravel...

  • Build Chat Application With Socket.io Using Node Js...

    Build Chat Application With Socket.io Using Node Js...

  • How to make login system using php opps...

    How to make login system using php opps...

  • A ChatGPT Job Interview for a Scrum Master...

    A ChatGPT Job Interview for a Scrum Master...

  • How To Build a Node.js API Proxy Using...

    How To Build a Node.js API Proxy Using...

  • Motion Controls In The Browser using javascript

    Motion Controls In The Browser using javascript

  • Notification system using jQuery, PHP, and MySQL

    Notification system using jQuery, PHP, and MySQL

  • Stock Prediction AI with python

    Stock Prediction AI with python

  • python chatbot with complete source code

    python chatbot with complete source code

  • MERGE TWO LISTS INTO A DICTIONARY PYTHON

    MERGE TWO LISTS INTO A DICTIONARY PYTHON

  • OTP login PHP Ajax

    OTP login PHP Ajax

  • How to validate email in JavaScript

    How to validate email in JavaScript

  • Fetch Data using Oracle and PHP

    Fetch Data using Oracle and PHP

  • Insert Data Using Oracle PHP

    Insert Data Using Oracle PHP

  • About us
  • Advertise
  • Privacy Policy
© 2023 All Rights Reserved

log in

Become a part of our community!

Forgot password?
Don't have an account?
sign up

forgot password

Back to
log in

sign up

Back to
log in