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');
?>
0 Comments