Created
September 8, 2017 06:13
-
-
Save Mauryashubham/1536a03f423e6e4368fd97e32378833d to your computer and use it in GitHub Desktop.
Simple login and registration process using PHP
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 | |
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 | |
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(); | |
} | |
} | |
} | |
?> |
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] | |
**/ | |
$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] | |
**/ | |
require_once 'dbconfig.php'; | |
if(isset($_POST['login'])) | |
{ | |
$name=filter_var($_POST['uname'], FILTER_SANITIZE_STRING); | |
$pass=filter_var($_POST['upass'], FILTER_SANITIZE_STRING); | |
if($user->login($name,$pass)) | |
{ | |
echo "Login Done..!"; | |
} | |
else | |
{ | |
echo "Login Failed"; | |
} | |
/* $stmt=$DBcon->prepare("SELECT * from register WHERE name=:uname AND password=:upass"); | |
$stmt->execute(array(':uname'=>$name , ':upass'=>$pass)); | |
$data_row=$stmt->fetch(PDO::FETCH_ASSOC); | |
if($stmt->rowCount()>0) | |
{ | |
if($pass==$data_row['password']) | |
{ | |
echo "Login Done..!"; | |
} | |
} | |
else | |
{ | |
echo "Invalid Details.!!"; | |
} | |
*/ | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>PDO Login form</title> | |
</head> | |
<body> | |
<form method="post" autocomplete="off" action="<?php echo $_SERVER["PHP_SELF"];?>"> | |
<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.php"><input type="button" value="register" ></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
<?php | |
/** | |
@author : Shubham Maurya, | |
Email id : [email protected] | |
**/ | |
require_once 'dbconfig.php'; | |
if(isset($_POST['register'])) | |
{ | |
$name=filter_var($_POST['name'], FILTER_SANITIZE_STRING); | |
$password=filter_var($_POST['pass'], FILTER_SANITIZE_STRING) | |
$mobile=filter_var($_POST['mobile'], FILTER_SANITIZE_STRING) | |
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(); | |
} | |
/*$stmt=$DBcon->prepare("INSERT into register(name,password,mobile) VALUES(:name , :pass , :mobile)"); | |
if($stmt->execute(array(':name'=>$name , ':pass'=>$password , ':mobile'=>$mobile))) | |
{ | |
echo "Registration Done..!"; | |
} | |
else | |
{ | |
echo "Registration Not Done..!"; | |
} | |
*/ | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>PDO Registration form</title> | |
</head> | |
<body> | |
<form method="post" autocomplete="off" action="<?php echo $_SERVER["PHP_SELF"];?>"> | |
<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.php"><input type="button" value="Login" ></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
-- phpMyAdmin SQL Dump | |
-- version 4.6.5.2 | |
-- https://www.phpmyadmin.net/ | |
-- | |
-- Host: 127.0.0.1 | |
-- Generation Time: Sep 08, 2017 at 08:13 AM | |
-- Server version: 10.1.21-MariaDB | |
-- PHP Version: 5.6.30 | |
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; | |
SET time_zone = "+00:00"; | |
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
/*!40101 SET NAMES utf8mb4 */; | |
-- | |
-- Database: `test` | |
-- | |
-- -------------------------------------------------------- | |
-- | |
-- Table structure for table `register` | |
-- | |
CREATE TABLE `register` ( | |
`id` int(11) NOT NULL, | |
`name` varchar(255) NOT NULL, | |
`password` varchar(255) NOT NULL, | |
`mobile` bigint(20) NOT NULL | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1; | |
-- | |
-- Dumping data for table `register` | |
-- | |
INSERT INTO `register` (`id`, `name`, `password`, `mobile`) VALUES | |
(0, '0', '$2y$12$gUWmWq5gN1Nog4BCVdAb7.FNw12PB8xpt1Nl1gaAaC5/p0CAizuJm', 11), | |
(0, 'qq', '$2y$12$U1eWSL8HZrA53kPsDgWufeJ0JGJSSiemywAmV07b94I9a/uwt1AlS', 12), | |
(0, 'zz', '$2y$12$jGvR1xSvBl08K733e.LBG.MUYsrgOQy9qTWW4pYgKavEmcW.YsRMa', 11); | |
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | |
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | |
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment