Skip to content

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

Open
Akhyrokz opened this issue Sep 8, 2023 · 2 comments
Open

User Registeration mini project #2

Akhyrokz opened this issue Sep 8, 2023 · 2 comments

Comments

@Akhyrokz
Copy link

Akhyrokz commented Sep 8, 2023

passwordreset.php page is not available.

@neerweb
Copy link

neerweb commented Feb 5, 2025

passwordreset.php page is not available.

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

@neerweb
Copy link

neerweb commented Feb 5, 2025

passwordreset.zip

Download the zip file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants