-
Notifications
You must be signed in to change notification settings - Fork 33
User Registeration mini project #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Please open notepad or notepad++ or others php editor, and create file - passwordreset.php and paste the below code - prepare($sql); // prepare $query->execute([$username, $email, $password, $createdAt]); // execute return $conn->lastInsertId(); } function login_user($email, $password){ $conn = get_connection(); $sql = "SELECT * from users WHERE email=? LIMIT 1"; $query = $conn->prepare($sql); $query->execute([$email]); $results = $query->fetchAll(); if(count($results) <= 0){ throw new Exception("No users found", 401); } if(isset($results[0])){ $user = $results[0]; if (password_verify($password, $user["password"])) { startASession(); $_SESSION["username"] = $user["name"]; $_SESSION["userid"] = $user["id"]; return true; }else{ return false; } } } function getUserData($id){ $conn = get_connection(); $sql = "SELECT * from users WHERE id=? LIMIT 1"; $query = $conn->prepare($sql); $query->execute([$id]); $results = $query->fetchAll(); if(count($results) > 0){ return $results[0]; } return []; } function getUserByEmail($email){ $conn = get_connection(); $sql = "SELECT * from users WHERE email=? LIMIT 1"; $query = $conn->prepare($sql); $query->execute([$email]); $results = $query->fetchAll(); if(count($results) > 0){ return $results[0]; } return []; } function getUserByCode($code){ $conn = get_connection(); $sql = "SELECT * from users WHERE remember_token=? LIMIT 1"; $query = $conn->prepare($sql); $query->execute([$code]); $results = $query->fetchAll(); if(count($results) > 0){ return $results[0]; } return []; } function updateUserPassword($id, $password){ $password = password_hash($password, PASSWORD_DEFAULT); $conn = get_connection(); $sql = "UPDATE users SET `password`=? WHERE id=?"; $query = $conn->prepare($sql); $query->execute([$password, $id]); return true; } function saveUserVerificationCode($id, $code){ $conn = get_connection(); $sql = "UPDATE users SET `remember_token`=? WHERE id=?"; $query = $conn->prepare($sql); $query->execute([$code, $id]); return true; } function resetPasswordByCode($code, $password){ $password = password_hash($password, PASSWORD_DEFAULT); $user = getUserByCode($code); if(count($user) > 0){ $conn = get_connection(); $sql = "UPDATE users SET `password`=? WHERE id=?"; $query = $conn->prepare($sql); $query->execute([$password, $user['id']]); echo "Password Successfully Reset"; echo "Login now"; } } function isUserLoggedIn(){ startASession(); if(isset($_SESSION["username"]) && $_SESSION["userid"]){ return true; } return false; } function auth(){ if(!isUserLoggedIn()){ header("Location: login.php"); } } function getUserId(){ startASession(); if($_SESSION["userid"]){ return $_SESSION["userid"]; } return null; } function get_random_code($num=22){ $characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; $string = ''; $max = strlen($characters) - 1; for ($i = 0; $i < $num; $i++) { $string .= $characters[mt_rand(0, $max)]; } return $string; } function startASession(){ if (session_status() == PHP_SESSION_NONE) { session_start(); } } function send_register_email($msg, $email, $subject="Register user"){ send_mail($subject, $msg, $email); } function send_verification_email($code, $email, $name){ $message = 'Dear '.$name.','; $message .= 'To change your password go to the link here: '; $message .= ''.$code.''; send_mail("Verification Email", $message, $email); } function send_mail($subject, $body, $address){ $mail = new PHPMailer(true); try { //Server settings $mail->isSMTP(); $mail->Host = 'smtp.mailtrap.io'; $mail->SMTPAuth = true; $mail->Username = ''; $mail->Password = ''; $mail->Port = 2525; //Recipients $mail->setFrom("[email protected]", 'wfTutorials'); $mail->addAddress($address, $address); // Name is optional $mail->addReplyTo('[email protected]', 'wfTutorials'); // Content $mail->isHTML(true); $mail->Subject = $subject; $mail->Body = $body; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; } } @neerweb Visit my Profile - https://github.com/neerweb for any query |
Download the zip file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
passwordreset.php page is not available.
The text was updated successfully, but these errors were encountered: