Skip to content

Instantly share code, notes, and snippets.

@Mauryashubham
Created March 8, 2017 08:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mauryashubham/1e26b783529735fc2b79ec4890f1f4c5 to your computer and use it in GitHub Desktop.
Save Mauryashubham/1e26b783529735fc2b79ec4890f1f4c5 to your computer and use it in GitHub Desktop.
Simple Login/Registration Process in PHP using PDO
<?php
/**
*
*/
class USER
{
private $db;
//Constructor
function __construct($DBcon)
{
$this->db=$DBcon;
}
//Login
public function login($name,$pass)
{
try {
$stmt=$this->db->prepare(“SELECT * from register WHERE name=:uname”);
$stmt->execute(array(‘:uname’=>$name));
$data_row=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount()>0)
{
if(password_verify($pass,$data_row[‘password’]))
{
return true;
}
}
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
//Signup
public function signup($name,$password,$mobile)
{
try {
$new_password = password_hash($password, PASSWORD_BCRYPT, array(‘cost’=>12));
$stmt=$this->db->prepare(“INSERT into register(name,password,mobile) VALUES(:name , :pass , :mobile)”);
if($stmt->execute(array(‘:name’=>$name , ‘:pass’=>$new_password, ‘:mobile’=>$mobile)))
{
return $stmt;
}
} catch (PDOException $e)
{
echo $e->getMessage();
}
}
//Logout
public function logout()
{
session_destroy();
unset($_SESSION[‘name’]);
return true;
}
//Logged in
public function is_loggedin()
{
if(isset($_SESSION[‘name’]))
{
return true;
}
}
//Redirect
public function redirect($url)
{
header(“Location: $url”);
}
}
?>
//CODING
1.Make a file in notepad and save it as index.php and paste the below code.
<?php
/**
@author : Shubham Maurya,
Email id : maurya.shubham5@gmail.com
**/
session_start();
require_once ‘dbconfig.php’;
$check=new USER($DBcon);
//Is Logged in
if($check->is_loggedin())
{
$check->redirect(‘login/profile.php’);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
Welcome,<br>
<a href=”login/index.php”><p>Login</p></a>
<a href=”register/index.php”><p>Register</p></a>
</body>
</html>
2.Make a file in notepad and save it as dbconfig.php and paste the below code.
<?php
/**
@author : Shubham Maurya,
Email id : maurya.shubham5@gmail.com
**/
$DB_host = “localhost”;
$DB_user = “root”;
$DB_pass = “”;
$DB_name = “test”;
try
{
$DBcon = new PDO(“mysql:host={$DB_host};dbname={$DB_name}”,$DB_user,$DB_pass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// echo “Done..”;
}
catch(PDOException $e)
{
echo “ERROR : “.$e->getMessage();
}
include_once ‘class.User.php’;
$user=new USER($DBcon);
?>
<?php
/**
@author : Shubham Maurya,
Email id : maurya.shubham5@gmail.com
**/
session_start();
require_once ‘dbconfig.php’;
$check=new USER($DBcon);
//Is Logged in
if($check->is_loggedin())
{
$check->redirect(‘login/profile.php’);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
Welcome,<br>
<a href=”login/index.php”><p>Login</p></a>
<a href=”register/index.php”><p>Register</p></a>
</body>
</html>
//LOGIN PART
1.Make a folder and give the name “login”
2.Now , make a file inside login folder and save it as index.php and paste the below code.
<?php
/**
@author : Shubham Maurya,
Email id : maurya.shubham5@gmail.com
**/
session_start();
require_once ‘../dbconfig.php’;
//Is Logged in
$reg=new USER($DBcon);
if($reg->is_loggedin())
{
$reg->redirect(‘profile.php’);
}
if(isset($_POST[‘login’]))
{
$name=$_POST[‘uname’];
$pass=$_POST[‘upass’];
if($user->login($name,$pass))
{
//echo “Login Done..!”;
$_SESSION[‘name’]=$name;
header(“location:profile.php”);
}
else
{
echo “Login Failed”;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PDO Login form</title>
</head>
<body>
<form method=”post”>
<input type=”text” name=”uname” placeholder=”Your UserName” required=””>
<input type=”text” name=”upass” placeholder=”Your Password” required=””>
<input type=”submit” name=”login” value=”login”>
</form>
<a href=”../register/index.php”><input type=”button” value=”register” ></a>
</body>
</html>
3.Now , make a file again inside login folder and save it as profile.php and paste the below code.
<?php
session_start();
require_once ‘../dbconfig.php’;
$name=$_SESSION[‘name’];
if(!$_SESSION[‘name’])
{
header(“location:index.php”);
}
if(isset($_POST[‘logout’]))
{
if($user->logout())
{
header(“location:index.php”);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Profile</title>
</head>
<body>
<h1>WELCOME , <?php echo $name; ?></h1>
<form method=”POST”>
<input type=”submit” name=”logout” value=”logout”>
</form>
</body>
</html>
//REGISTRATION PART
1.Make a folder and give the name “register”
2.Now , make a file inside register folder and save it as index.php and paste the below code.
<?php
/**
@author : Shubham Maurya,
Email id : maurya.shubham5@gmail.com
**/
session_start();
require_once ‘../dbconfig.php’;
$reg=new USER($DBcon);
//Is Logged in
if($reg->is_loggedin())
{
$reg->redirect(‘../login/profile.php’);
}
if(isset($_POST[‘register’]))
{
$name=$_POST[‘name’];
$password=$_POST[‘pass’];
$mobile=$_POST[‘mobile’];
try
{
$stmt=$DBcon->prepare(“SELECT name FROM register WHERE name=:name”);
$stmt->execute(array(‘:name’=>$name)); //PLACEHOLDER ‘ : ‘
$data_f=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount()>0)
{
echo “Name Already Taken”;
}
else
{
if($user->signup($name,$password,$mobile))
{
if(true)
echo “Registration Done..!”;
}
else
{
echo “Registration fAILED”;
}
}
}
catch (PDOException $e)
{
echo $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PDO Registration form</title>
</head>
<body>
<form method=”post”>
<input type=”text” name=”name” placeholder=”Your Name” required=””>
<input type=”password” name=”pass” placeholder=”Your password” required=””>
<input type=”number” name=”mobile” placeholder=”Your mobile” required=””>
<input type=”submit” name=”register” value=”Register”>
</form>
<a href=”../login/index.php”><input type=”button” value=”Login” ></a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment