Created
March 8, 2017 08:28
-
-
Save Mauryashubham/1e26b783529735fc2b79ec4890f1f4c5 to your computer and use it in GitHub Desktop.
Simple Login/Registration Process in PHP using PDO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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”); | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//CODING | |
1.Make a file in notepad and save it as index.php and paste the below code. | |
<?php | |
/** | |
@author : Shubham Maurya, | |
Email id : [email protected] | |
**/ | |
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 : [email protected] | |
**/ | |
$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); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
@author : Shubham Maurya, | |
Email id : [email protected] | |
**/ | |
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 : [email protected] | |
**/ | |
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 : [email protected] | |
**/ | |
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