connect_errno) { echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error; } else { debug("Successful Connection"); // debug($conn->host_info); } if($conn->select_db('ps2020')) { debug("Using ps2020"); } else { error($conn->error); } $msg = ''; if(isset($_SESSION['email']) || isset($_SESSION['username'])){ $email = $_SESSION['email']; $username = $_SESSION['username']; //Before anything we need to check wether the user account has already been acitvated or not $sql = "SELECT * FROM user_account WHERE email = '$email' OR username = '$username'"; $result = $conn->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'); } } } } //We check if user has submitted a request if($_SERVER["REQUEST_METHOD"] == "POST"){ //First we check for user email update request if(isset($_POST['update'])){ $newEmail = mysqli_real_escape_string($conn, $_POST['email']); $oldEmail = $_SESSION['email']; //Now we check if the new email and old email is the same if(!filter_var($newEmail, FILTER_VALIDATE_EMAIL)){ $msg = "Invalid Email!!"; }else if($newEmail == ""){ //Do nothing $msg = "Email cannot be empty"; }else if($newEmail == $oldEmail){ //That means user hasn't changed his/her email $msg = 'Please enter new email for verify-code. '; }else{ //This would mean that user is trying to update his current email with a new email //SO we update his records with new email $username = $_SESSION['username']; $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; } //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 .= "
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['verify'])){ $verify_code = $_POST['verifyCode']; //Now we check wether the code entered is the correct one if($verifyCode == $verify_code){ //That means user email is correct and so we set active to 1 $email = $_SESSION['email']; $sql = "UPDATE user_account SET active = '1' WHERE email = '$email'"; $result = $conn->query($sql); if($result){ //so now the verify code has been verified $_SESSION['active'] = 1; header("location: dashboard.php"); }else{ $msg = "An error has occured" . $conn->error; } }else{ $msg = "The verify code entered is incorrect"; } } } }else{ header('location:index.php'); } ?>