我的 Connect.php 正在处理注册
`
<?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();
}
}
}
`
当我提交生成的代码时,它位于数据库中,而我修改它的时间是当我将其放在验证器上时接受代码
但这一次我发送电子邮件时它不起作用(我认为它起作用,但由于我的 verify.php 没有通过电子邮件发送代码而失败(“发送电子邮件失败:电子邮件地址格式无效。”))
这是我的Verify.php,处理将发送到电子邮件的代码
`
<?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();
}
}`
您正在将
$_SESSION['email_verification_address']
,而不是 $_POST['emailAdd']
传递给您的Verify.php
所以改变
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// Retrieve the email address and verification code from the form
$emailAddress = isset($_POST['emailAdd']) ? $_POST['emailAdd'] : '';
到
if ($_SESSION['email_verification_address'] !="") {
// Retrieve the email address and verification code from the form
$emailAddress = isset($_SESSION['email_verification_address']) ? $_SESSION['email_verification_address'] : '';