I have my Connect.php which is handling Registration
`
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
function generateVerificationCode() {
return substr(md5(uniqid(mt_rand(), true)), 0, 6); // Generates a 6-character alphanumeric verification code
}
if (isset($_POST["submitSignUp"])) {
// Check if captcha response is provided
if (empty($_POST['g-recaptcha-response'])) {
$_SESSION['error_message5'] = "re-CAPTCHA verification failed. Please try again";
header("Location: login.php"); // Redirect back to the signup page
exit();
}
// Verify captcha response
$captchaResponse = $_POST['g-recaptcha-response'];
$secretKey = '6LcqbYkpAAAAAIgIDMA849mdEbkYifGcw0Tk1_Ww'; // Replace with your secret key
$captchaVerify = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secretKey}&response={$captchaResponse}");
$captchaVerify = json_decode($captchaVerify);
$emailAddress = $_POST['emailAdd'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$password = $_POST['password'];
$confirmPassword = $_POST['confirmPassword'];
$phoneNum = $_POST['phoneNum'];
$registeredAt = date('Y-m-d');
// Validate that the password and confirm password match
if ($password !== $confirmPassword) {
$_SESSION['error_message4'] = "Password does not match. Please try again";
header("Location: login.php"); // Redirect back to the signup page
exit();
}
// Hash the password
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
// Establish database connection
$conn = new mysqli('localhost', 'root', '', 'database');
if ($conn->connect_error) {
die('Connection Failed: ' . $conn->connect_error);
} else {
// Check if the email address already exists in the database
$checkQuery = $conn->prepare("SELECT COUNT(*) FROM registration WHERE emailAdd = ?");
$checkQuery->bind_param("s", $emailAddress);
$checkQuery->execute();
$checkResult = $checkQuery->get_result();
$count = $checkResult->fetch_assoc()['COUNT(*)'];
if ($count > 0) {
$_SESSION['error_message3'] = "Email address already exists.";
header("Location: login.php"); // Redirect back to the signup page
exit();
}
// Ensure $registeredAt is not null
if ($registeredAt === null) {
$registeredAt = date('Y-m-d');
}
// Generate verification code
$verificationCode = generateVerificationCode();
// Store verification code in the database
$verificationQuery = $conn->prepare("INSERT INTO email_verification (email, verification_code) VALUES (?, ?)");
$verificationQuery->bind_param("ss", $emailAddress, $verificationCode);
$verificationQuery->execute();
$verificationQuery->close();
// Insert user data into the registration table
$stmt = $conn->prepare("INSERT INTO registration (emailAdd, firstname, lastname, password, phonenum, registeredat) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $emailAddress, $firstName, $lastName, $passwordHash, $phoneNum, $registeredAt);
if ($stmt->execute()) {
// After successful registration, redirect and display success message
$_SESSION['email_verification_address'] = $emailAddress;
$_SESSION['success_message'] = "Code has been sent to your email.";
header("Location: emailformverifyer.php");
exit();
} else {
$_SESSION['error_message'] = "Registration failed. Please try again.";
header("Location: login.php"); // Redirect back to the signup page
exit();
}
}
}
`
When I submit the code generated it is in the database and the time i modified it was accepting the code when i put it on my verifyer
but this time with my sending email it is not working (i think it is working but it fails because of my verify.php not sending code through email ("Failed to send email: Invalid email address format."))
And this is my Verify.php handling the code that will be sent to the email
`
<?php
session_start();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once __DIR__ . '/vendor/autoload.php';
// Create an instance of PHPMailer
$mail = new PHPMailer(true);
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// Retrieve the email address and verification code from the form
$emailAddress = isset($_POST['emailAdd']) ? $_POST['emailAdd'] : '';
$verificationCode = isset($_POST['verification_code']) ? $_POST['verification_code'] : '';
// Validate the email address format
if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
$_SESSION['email_sent_error'] = 'Failed to send email: Invalid email address format.';
header("Location: login.php");
exit();
}
try {
// Compose the email
$mail->isSMTP();
$mail->Host = 'localhost'; // Your SMTP host
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Your SMTP username
$mail->Password = 'secret'; // Your SMTP password
$mail->SMTPSecure = 'tls';
$mail->Port = 25; // Your SMTP port
$mail->SMTPDebug = SMTP::DEBUG_CONNECTION;
$mail->setFrom('[email protected]', 'Gemma Balnodo');
$mail->addAddress($emailAddress); // Add the recipient email address
$mail->Subject = 'Email Verification';
$mail->Body = 'Here is your verification code: ' . $verificationCode . '. Click <ahref="emailformverifyer.php">here</a> to verify.';
// Send the email
if ($mail->send()) {
// Email sent successfully
$_SESSION['email_sent_success'] = true;
header("Location: verification_form.php");
exit();
} else {
// Failed to send email
$_SESSION['email_sent_error'] = 'Failed to send email: ' . $mail->ErrorInfo;
header("Location: login.php");
exit();
}
} catch (Exception $e) {
$_SESSION['email_sent_error'] = 'Failed to send email: ' . $e->getMessage();
header("Location: login.php");
exit();
}
}`
(1) You need to properly pass variables to your Verify.php. For your case (based on your code), you are passing variables by SESSION instead of by POST, so
Please add the following line in
Connect.phpafter the line$_SESSION['email_verification_address'] = $emailAddress;After that, you need to update your Verify.php to use session variables
So change
to
(2) It is quite unusual to use port 25 for tls encryption, please double check whether it is correct in your server environment (either change the port number or do not use encryption). Also, make sure your system can send out email by SMTP
(3) Last but not least, I think your PHP script sending out email should be having the name
emailformverifyer.phpif you have using the lineheader("Location: emailformverifyer.php");