├── README.md └── user ├── DbConnect.php ├── action.php ├── img └── loader.gif ├── index.php ├── logout.php ├── registration.sql ├── reset.php ├── users.php ├── verify.php └── welcome.php /README.md: -------------------------------------------------------------------------------- 1 | # user-registration-php-mysql-ajax 2 | 3 | Follow this tutorial: https://www.youtube.com/watch?v=cXWCVvfe4ao&list=PLCakfctNSHkFXKLQKX4jv7OEyWUcTgbfE 4 | -------------------------------------------------------------------------------- /user/DbConnect.php: -------------------------------------------------------------------------------- 1 | host . '; dbname=' . $this->dbName, $this->user, $this->pass); 11 | $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 12 | return $conn; 13 | } catch( PDOException $e) { 14 | echo 'Database Error: ' . $e->getMessage(); 15 | } 16 | } 17 | } 18 | ?> -------------------------------------------------------------------------------- /user/action.php: -------------------------------------------------------------------------------- 1 | $_COOKIE['email'], 'pass'=>base64_decode($_COOKIE['pass'])]; 10 | echo json_encode($data); 11 | } 12 | } 13 | 14 | if(isset($_POST['action']) && $_POST['action'] == 'updatePass') { 15 | $users = validateUpdatePassForm(); 16 | $data = json_decode( base64_decode($users['token']), true ); 17 | $currTime = strtotime(date('d-m-Y h:i:s')); 18 | $expTime = strtotime($data['expTime']); 19 | if($currTime > $expTime) { 20 | echo json_encode( ["status" => 0, "msg" => "Token expired."] ); 21 | exit; 22 | } 23 | 24 | $objUser = new Users(); 25 | $objUser->setId($data['id']); 26 | $userData = $objUser->getUserById(); 27 | if(is_array($userData) && count($userData) > 0) { 28 | if($data['token'] == $userData['token']) { 29 | $objUser->setPass(md5($users['pass'])); 30 | if($objUser->updatePass()) { 31 | echo json_encode( ["status" => 1, "msg" => "Password Updated."] ); 32 | exit; 33 | } else { 34 | echo json_encode( ["status" => 0, "msg" => "Failed to update password."] ); 35 | exit; 36 | } 37 | } else { 38 | echo json_encode( ["status" => 0, "msg" => "Token is not valid."] ); 39 | exit; 40 | } 41 | } else { 42 | echo json_encode( ["status" => 0, "msg" => "User not found."] ); 43 | exit; 44 | } 45 | 46 | } 47 | 48 | if(isset($_POST['action']) && $_POST['action'] == 'resetPass') { 49 | $email = filter_input(INPUT_POST, 'remail', FILTER_VALIDATE_EMAIL); 50 | if(false == $email) { 51 | echo json_encode( ["status" => 0, "msg" => "Enter valid Email"] ); 52 | exit; 53 | } 54 | 55 | $objUser = new Users(); 56 | $objUser->setEmail($email); 57 | $userData = $objUser->getUserByEmail(); 58 | if(is_array($userData) && count($userData)>0) { 59 | $data['id'] = $userData['id']; 60 | $data['token'] = sha1( $userData['email'] ); 61 | $data['expTime'] = date('d-m-Y h:i:s', time() + (60*60*2)); 62 | $urlToken = base64_encode(json_encode($data)); 63 | $objUser->setId($userData['id']); 64 | $objUser->setToken($data['token']); 65 | if($objUser->updateToken()) { 66 | $url = 'http://' . $_SERVER['SERVER_NAME'] . '/user/reset.php?token=' .$urlToken; 67 | $html = '
You have requested a password reset for your user account at Localhost. You can do this by clicking the link below.:
'.$url.'

Please note this link is valid for 2 hours.
'; 68 | 69 | $mail = new PHPMailer; 70 | 71 | // $mail->SMTPDebug = 4; // Enable verbose debug output 72 | 73 | $mail->isSMTP(); // Set mailer to use SMTP 74 | $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 75 | $mail->SMTPAuth = true; // Enable SMTP authentication 76 | $mail->Username = EMAIL; // SMTP username 77 | $mail->Password = PASS; // SMTP password 78 | $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted 79 | $mail->Port = 587; // TCP port to connect to 80 | 81 | $mail->setFrom(EMAIL, 'Dsmart Tutorials'); 82 | $mail->addAddress($objUser->getEmail()); // Add a recipient 83 | 84 | $mail->addReplyTo(EMAIL); 85 | 86 | $mail->isHTML(true); // Set email format to HTML 87 | 88 | $mail->Subject = 'Reset your password'; 89 | $mail->Body = $html; 90 | 91 | if(!$mail->send()) { 92 | echo json_encode( ["status" => 0, "msg" => "Message could not be sent."] ); 93 | echo json_encode( ["status" => 0, "msg" => 'Mailer Error: ' . $mail->ErrorInfo] ); 94 | } else { 95 | echo json_encode( ["status" => 1, "msg" => "Reset password link is send to your email."] ); 96 | } 97 | } else { 98 | echo json_encode( ["status" => 0, "msg" => "Failed to set token."] ); 99 | } 100 | } else { 101 | echo json_encode( ["status" => 0, "msg" => "User is not found."] ); 102 | } 103 | 104 | } 105 | 106 | if(isset($_POST['action']) && $_POST['action'] == 'register') { 107 | $users = validateRegForm(); 108 | 109 | $objUser = new Users(); 110 | 111 | $objUser->setName($users['fname']); 112 | $objUser->setMobile($users['mobile']); 113 | $objUser->setEmail($users['uemail']); 114 | $objUser->setPass(md5($users['pass'])); 115 | $objUser->setActivated(0); 116 | $objUser->setToken(NULL); 117 | $objUser->setCreatedOn(date('Y-m-d')); 118 | 119 | $userData = $objUser->getUserByEmail(); 120 | if($userData['email'] == $users['uemail']) { 121 | echo 'Email is already registered'; 122 | exit; 123 | } 124 | if($objUser->save()) { 125 | $lastId = $objUser->conn->lastInsertId(); 126 | $token = sha1($lastId); 127 | $url = 'http://' . $_SERVER['SERVER_NAME'] . '/user/verify.php?id=' . $lastId . '&token=' .$token; 128 | $html = '
Thanks for registering with localhost. Please click this link to complete your registration:
'.$url.'
'; 129 | 130 | $mail = new PHPMailer; 131 | 132 | // $mail->SMTPDebug = 4; // Enable verbose debug output 133 | 134 | $mail->isSMTP(); // Set mailer to use SMTP 135 | $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 136 | $mail->SMTPAuth = true; // Enable SMTP authentication 137 | $mail->Username = EMAIL; // SMTP username 138 | $mail->Password = PASS; // SMTP password 139 | $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted 140 | $mail->Port = 587; // TCP port to connect to 141 | 142 | $mail->setFrom(EMAIL, 'Dsmart Tutorials'); 143 | $mail->addAddress($objUser->getEmail()); // Add a recipient 144 | 145 | $mail->addReplyTo(EMAIL); 146 | 147 | $mail->isHTML(true); // Set email format to HTML 148 | 149 | $mail->Subject = 'Confirm your email'; 150 | $mail->Body = $html; 151 | 152 | if(!$mail->send()) { 153 | echo 'Message could not be sent.'; 154 | echo 'Mailer Error: ' . $mail->ErrorInfo; 155 | } else { 156 | echo "Congratulation, Your registration done on our site. Please verify your email."; 157 | } 158 | 159 | } else { 160 | echo " Failed to save"; 161 | } 162 | } 163 | 164 | if(isset($_POST['action']) && $_POST['action'] == 'login') { 165 | $users = validateLoginForm(); 166 | $objUser = new Users(); 167 | $objUser->setEmail($users['email']); 168 | $objUser->setPass(md5($users['pwd'])); 169 | $userData = $objUser->getUserByEmail(); 170 | $rememberMe = isset($_POST['remember-me']) ? 1 : 0; 171 | if(is_array($userData) && count($userData) > 0) { 172 | if($userData['pass'] == $objUser->getPass()) { 173 | if($userData['activated'] == 1 ) { 174 | if($rememberMe == 1) { 175 | setcookie('email', $objUser->getEmail()); 176 | setcookie('pass', base64_encode($users['pwd'])); 177 | } 178 | $_SESSION['id'] = session_id(); 179 | $_SESSION['name'] = $userData['name']; 180 | echo json_encode( ["status" => 1, "msg" => "login successfull."] ); 181 | } else { 182 | echo json_encode( ["status" => 0, "msg" => "Please activate your account to login."] ); 183 | } 184 | } else { 185 | echo json_encode( ["status" => 0, "msg" => "Email or Password is wrong."] ); 186 | } 187 | } else { 188 | echo json_encode( ["status" => 0, "msg" => "Email or Password is wrong."] ); 189 | } 190 | } 191 | 192 | function validateUpdatePassForm() { 193 | $users['token'] = filter_input(INPUT_POST, 'token', FILTER_SANITIZE_STRING); 194 | if(false == $users['token']) { 195 | echo json_encode( ["status" => 0, "msg" => "Not a valid request."] ); 196 | exit; 197 | } 198 | 199 | $users['pass'] = filter_input(INPUT_POST, 'pass', FILTER_SANITIZE_STRING); 200 | if(false == $users['pass']) { 201 | echo json_encode( ["status" => 0, "msg" => "Enter valid valid pass"] ); 202 | exit; 203 | } 204 | 205 | $users['cfm_pass'] = filter_input(INPUT_POST, 'cfm_pass', FILTER_SANITIZE_STRING); 206 | if(false == $users['cfm_pass']) { 207 | echo json_encode( ["status" => 0, "msg" => "Enter valid confirm pass"] ); 208 | exit; 209 | } 210 | 211 | if($users['pass'] != $users['cfm_pass']) { 212 | echo json_encode( ["status" => 0, "msg" => "Password and confirm password not match"] ); 213 | exit; 214 | } 215 | 216 | return $users; 217 | } 218 | 219 | function validateLoginForm() { 220 | $users['email'] = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL); 221 | if(false == $users['email']) { 222 | echo json_encode( ["status" => 0, "msg" => "Enter valid Email"] ); 223 | exit; 224 | } 225 | 226 | $users['pwd'] = filter_input(INPUT_POST, 'pwd', FILTER_SANITIZE_STRING); 227 | if(false == $users['pwd']) { 228 | echo json_encode( ["status" => 0, "msg" => "Enter valid valid pass"] ); 229 | exit; 230 | } 231 | 232 | return $users; 233 | } 234 | 235 | function validateRegForm() { 236 | $users['fname'] = filter_input(INPUT_POST, 'fname', FILTER_SANITIZE_STRING); 237 | if(false == $users['fname']) { 238 | echo "Enter valid name"; 239 | exit; 240 | } 241 | 242 | $users['mobile'] = filter_input(INPUT_POST, 'mobile', FILTER_SANITIZE_NUMBER_INT); 243 | if(false == $users['mobile']) { 244 | echo "Enter valid number"; 245 | exit; 246 | } 247 | 248 | $users['uemail'] = filter_input(INPUT_POST, 'uemail', FILTER_VALIDATE_EMAIL); 249 | if(false == $users['uemail']) { 250 | echo "Enter valid Email"; 251 | exit; 252 | } 253 | 254 | $users['pass'] = filter_input(INPUT_POST, 'pass', FILTER_SANITIZE_STRING); 255 | if(false == $users['pass']) { 256 | echo "Enter valid valid pass"; 257 | exit; 258 | } 259 | $users['cfm_pass'] = filter_input(INPUT_POST, 'cfm_pass', FILTER_SANITIZE_STRING); 260 | if(false == $users['cfm_pass']) { 261 | echo "Enter valid valid confirm pass"; 262 | exit; 263 | } 264 | 265 | if($users['pass'] != $users['cfm_pass']) { 266 | echo 'Password and confirm password not match'; 267 | exit; 268 | } 269 | 270 | return $users; 271 | } 272 | ?> -------------------------------------------------------------------------------- /user/img/loader.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/durgesh-sahani/user-registration-php-mysql-ajax/b6e0a3ad34d2bec607df765bba3296ab9381d154/user/img/loader.gif -------------------------------------------------------------------------------- /user/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Complete user registration system in php and MySQL using Ajax 5 | 6 | 7 | 8 | 35 | 193 | 194 | 195 | 196 |
197 |

Sign Up, Sign In, Forgot Password in php and MySQL using Ajax

198 |

Part 8: Send reset password link with expiry time

199 |
200 |
201 |
202 | 206 |
207 |
208 | 272 | 316 |
317 |
318 |
319 |

RECOVER YOUR PASSWORD

320 | 321 |
322 |
323 |
324 |
325 |
326 |
327 | 328 |
329 | 330 |
331 |
332 |
333 | 334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 | 342 | -------------------------------------------------------------------------------- /user/logout.php: -------------------------------------------------------------------------------- 1 | Click here to login again"; 9 | } 10 | ?> -------------------------------------------------------------------------------- /user/registration.sql: -------------------------------------------------------------------------------- 1 | -- phpMyAdmin SQL Dump 2 | -- version 4.7.4 3 | -- https://www.phpmyadmin.net/ 4 | -- 5 | -- Host: 127.0.0.1 6 | -- Generation Time: Apr 29, 2018 at 07:57 PM 7 | -- Server version: 10.1.30-MariaDB 8 | -- PHP Version: 7.2.1 9 | 10 | SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 11 | SET AUTOCOMMIT = 0; 12 | START TRANSACTION; 13 | SET time_zone = "+00:00"; 14 | 15 | 16 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 17 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 18 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 19 | /*!40101 SET NAMES utf8mb4 */; 20 | 21 | -- 22 | -- Database: `registration` 23 | -- 24 | 25 | -- -------------------------------------------------------- 26 | 27 | -- 28 | -- Table structure for table `users` 29 | -- 30 | 31 | CREATE TABLE `users` ( 32 | `id` int(4) NOT NULL, 33 | `name` varchar(50) NOT NULL, 34 | `mobile` bigint(10) NOT NULL, 35 | `email` varchar(60) NOT NULL, 36 | `pass` varchar(60) NOT NULL, 37 | `activated` tinyint(2) NOT NULL DEFAULT '0', 38 | `token` varchar(100) DEFAULT NULL, 39 | `created_on` date NOT NULL 40 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 41 | 42 | -- 43 | -- Indexes for dumped tables 44 | -- 45 | 46 | -- 47 | -- Indexes for table `users` 48 | -- 49 | ALTER TABLE `users` 50 | ADD PRIMARY KEY (`id`), 51 | ADD UNIQUE KEY `email` (`email`); 52 | 53 | -- 54 | -- AUTO_INCREMENT for dumped tables 55 | -- 56 | 57 | -- 58 | -- AUTO_INCREMENT for table `users` 59 | -- 60 | ALTER TABLE `users` 61 | MODIFY `id` int(4) NOT NULL AUTO_INCREMENT; 62 | COMMIT; 63 | 64 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 65 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 66 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 67 | -------------------------------------------------------------------------------- /user/reset.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Complete user registration system in php and MySQL using Ajax 5 | 6 | 7 | 8 | 21 | 72 | 73 | 74 |
75 |

Sign Up, Sign In, Forgot Password in php and MySQL using Ajax

76 |

Part 9: Validate reset password link and update password

77 |
78 |

Update your password


79 |
80 |
81 |
82 |

83 |
84 |
85 |
86 |
87 |
88 |
89 | 90 |
91 | 92 |
93 |
94 |
95 |
96 |
97 | 98 |
99 | 100 |
101 |
102 |
103 | 104 |
105 |
106 |
107 |
108 |
109 | 110 | -------------------------------------------------------------------------------- /user/users.php: -------------------------------------------------------------------------------- 1 | id = $id; } 18 | function getId() { return $this->id; } 19 | function setName($name) { $this->name = $name; } 20 | function getName() { return $this->name; } 21 | function setMobile($mobile) { $this->mobile = $mobile; } 22 | function getMobile() { return $this->mobile; } 23 | function setEmail($email) { $this->email = $email; } 24 | function getEmail() { return $this->email; } 25 | function setPass($pass) { $this->pass = $pass; } 26 | function getPass() { return $this->pass; } 27 | function setActivated($activated) { $this->activated = $activated; } 28 | function getActivated() { return $this->activated; } 29 | function setToken($token) { $this->token = $token; } 30 | function getToken() { return $this->token; } 31 | function setCreatedOn($createdOn) { $this->createdOn = $createdOn; } 32 | function getCreatedOn() { return $this->createdOn; } 33 | 34 | 35 | function __construct() { 36 | require 'DbConnect.php'; 37 | $db = new DbConnect(); 38 | $this->conn = $db->connect(); 39 | } 40 | 41 | public function save() 42 | { 43 | $sql = "INSERT INTO `users`(`id`, `name`, `mobile`, `email`, `pass`, `activated`, `token`, `created_on`) VALUES (null,:name,:mobile,:email,:pass,:activated,:token,:cdate)"; 44 | $stmt = $this->conn->prepare($sql); 45 | $stmt->bindParam(':name', $this->name); 46 | $stmt->bindParam(':mobile', $this->mobile); 47 | $stmt->bindParam(':email', $this->email); 48 | $stmt->bindParam(':pass', $this->pass); 49 | $stmt->bindParam(':activated', $this->activated); 50 | $stmt->bindParam(':token', $this->token); 51 | $stmt->bindParam(':cdate', $this->createdOn); 52 | try { 53 | if($stmt->execute()) { 54 | return true; 55 | } else { 56 | return false; 57 | } 58 | } catch (Exception $e) { 59 | echo $e->getMessage(); 60 | } 61 | } 62 | 63 | public function getUserByEmail() { 64 | $stmt = $this->conn->prepare('SELECT * FROM users WHERE email = :email'); 65 | $stmt->bindParam(':email', $this->email); 66 | try { 67 | if($stmt->execute()) { 68 | $user = $stmt->fetch(PDO::FETCH_ASSOC); 69 | } 70 | } catch (Exception $e) { 71 | echo $e->getMessage(); 72 | } 73 | return $user; 74 | } 75 | 76 | public function getUserById() { 77 | $stmt = $this->conn->prepare('SELECT * FROM users WHERE id = :id'); 78 | $stmt->bindParam(':id', $this->id); 79 | try { 80 | if($stmt->execute()) { 81 | $user = $stmt->fetch(PDO::FETCH_ASSOC); 82 | } 83 | } catch (Exception $e) { 84 | echo $e->getMessage(); 85 | } 86 | return $user; 87 | } 88 | 89 | public function activateUserAccount() { 90 | $stmt = $this->conn->prepare('UPDATE users SET activated = 1 WHERE id = :id'); 91 | $stmt->bindParam(':id', $this->id); 92 | try { 93 | if($stmt->execute()) { 94 | return true; 95 | } else { 96 | return false; 97 | } 98 | } catch (Exception $e) { 99 | echo $e->getMessage(); 100 | } 101 | } 102 | public function updateToken() { 103 | $stmt = $this->conn->prepare('UPDATE users SET token = :token WHERE id = :id'); 104 | $stmt->bindParam(':token', $this->token); 105 | $stmt->bindParam(':id', $this->id); 106 | try { 107 | if($stmt->execute()) { 108 | return true; 109 | } else { 110 | return false; 111 | } 112 | } catch (Exception $e) { 113 | echo $e->getMessage(); 114 | } 115 | } 116 | public function updatePass() { 117 | $stmt = $this->conn->prepare('UPDATE users SET pass = :pass WHERE id = :id'); 118 | $stmt->bindParam(':pass', $this->pass); 119 | $stmt->bindParam(':id', $this->id); 120 | try { 121 | if($stmt->execute()) { 122 | return true; 123 | } else { 124 | return false; 125 | } 126 | } catch (Exception $e) { 127 | echo $e->getMessage(); 128 | } 129 | } 130 | 131 | } 132 | 133 | 134 | ?> -------------------------------------------------------------------------------- /user/verify.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Verify user account 5 | 6 | 7 | setId($id); 14 | 15 | $user = $objUser->getUserById(); 16 | if(is_array($user) && count($user)>0) { 17 | if(sha1($user['id']) == $token) { 18 | if($objUser->activateUserAccount()) { 19 | echo 'Congratulation, Your account activated. You can login now.
'; 20 | echo 'Click here to login'; 21 | } else { 22 | echo 'Some problem occurred. Please Try after some time.'; 23 | } 24 | } else { 25 | echo 'We can\'t find your detail in our database'; 26 | } 27 | } else { 28 | echo 'We can\'t find your detail in our database'; 29 | } 30 | 31 | ?> 32 | 33 | -------------------------------------------------------------------------------- /user/welcome.php: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | Complete user registration system in php and MySQL using Ajax 11 | 12 | 13 | 14 |
15 |

Welcome to localhost


16 |
17 |
18 |
Hello,
19 | 20 |
21 |
22 |
23 | 24 | --------------------------------------------------------------------------------