query($sql); $verifyCode = ""; if($result){ if($result->num_rows == 1){ //the while loop will only run once while($row = $result->fetch_assoc()){ $account_status = $row['active']; $verifyCode = $row['verifyCode']; if($account_status == 1){ header('location: dashboard.php'); } } } } //Since here we have checked wether the account //has alerady become active or not //This is if user accidently comes to this page if($_SERVER["REQUEST_METHOD"] == "POST"){ //Now we need to check wether the user is trying to update email or //Else he is trying to verify the account if(isset($_POST["submitEmail"])){ //so the user is trying to update the email $newEmail = mysqli_real_escape_string($conn, $_POST['email']); $oldEmail = $_SESSION["email"]; if($newEmail == $oldEmail){ $msg = "New Email cannot be same as the old one"; }else if(!filter_var($newEmail, FILTER_VALIDATE_EMAIL)){ $msg = "Invalid Email! Please enter a valid email!"; }else{ //We fetch the user's username $username = $_SESSION['user']; //We need to make sure that user email is not already taken $sql = "SELECT * FROM user_account WHERE email = '$newEmail'"; $result = $conn->query($sql); if($result->num_rows > 0){ //this would mean that the email is already registered to another account $msg = "Email is already registered to another account!! Try Again!!"; }else{ $sql = "UPDATE user_account SET email= '$newEmail' WHERE username = '$username'"; $result = $conn->query($sql); if($result){ $msg = "You email has been Updated!"; //we also update the email of session variable to a new one $_SESSION['email'] = $newEmail; }else{ $msg = "Error updating record: " . $conn->error; } //So now we need to fetch the verifyCode for the user $sql = "SELECT * FROM user_account WHERE username = '$username'"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $verifyCode = $row["verifyCode"]; } } //Check if verifyCode is empty if($verifyCode == ""){ echo "Verify Code not Found!"; } //Now after updating the email we email the verify code again //Send it to his new email $to = $newEmail; $subject = "Account-Verification Code For GEESE"; $message = "Welcome to GEESE."; $message .= "

" . $username . "we would like to thank you for joining GEESE

"; $message .= "

Your Verfication Code is: " . $verifyCode . "

"; $header = "From:teamgeese1@gmail.com \r\n"; $header .= "Cc:teamgeese1@gmail.com \r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-type: text/html\r\n"; //This is to send the verify code to the user $retval = mail ($to,$subject,$message,$header); if( $retval == true ) { $msg = $msg . "The code has been emailed to you"; }else { $msg = $msg . "Email couldn't be sent"; } } } } if(isset($_POST["submitCode"])){ //Now we need to check if the verify Code is correct or not $userCode = mysqli_real_escape_string($conn,$_POST["code"]); $username = $_SESSION["user"]; $sql = "SELECT * FROM user_account WHERE username = '$username'"; $result = $conn->query($sql); if($result->num_rows == 1){ while($row = $result->fetch_assoc()){ $verifyCode = $row["verifyCode"]; if($verifyCode == $userCode){ $msg = "Your usercode is valid"; //Now that the usercode is valid //Now we change the active column of the user to 1 //$sql = "UPDATE user_account SET active = '1' WHERE username = '$username'"; $sql = "UPDATE user_account SET active = '1' WHERE username = '$username'"; if($conn->query($sql) === TRUE){ $msg = "Your account status has been updated!!"; $_SESSION['active'] = 1; header('location: '.dir_root.'account/dashboard.php'); }else{ $msg = "Your account settings cannot be updated!!"; } }else{ $msg = "The valid code entered is invalid!!"; } } }else{ echo "Duplicate user accounts with same value"; } } } }else{ //That is go to the sign-up/login page // header("location: index.php"); } ?> Hielo by TEMPLATED

Greenscape Environmental Experience & Learning Survey Experiments

Email Verification

Verify Account


Not a member? Register

Follow us at: