prepare("SELECT id FROM users WHERE username = ? OR email = ?"); $stmt->bind_param("ss", $username, $email); $stmt->execute(); $result = $stmt->get_result(); if($result->num_rows > 0) { $_SESSION['error'] = "Username or email already exists."; header("Location: register.php"); exit; } $stmt->close(); // Insert the new user record. (Assuming columns firstName and lastName exist.) $hashed_password = password_hash($password, PASSWORD_DEFAULT); $stmt = $db->prepare("INSERT INTO users (username, password, email, firstName, lastName, img, emailVerified) VALUES (?, ?, ?, ?, ?, '', 0)"); $stmt->bind_param("sssss", $username, $hashed_password, $email, $first_name, $last_name); if(!$stmt->execute()){ $_SESSION['error'] = "Registration failed. Please try again."; header("Location: register.php"); exit; } $user_id = $stmt->insert_id; $stmt->close(); // Log the user in $_SESSION['user'] = [ 'id' => $user_id, 'username' => $username, 'email' => $email ]; // Trigger email verification: generate a verification code valid for 15 minutes $verification_code = bin2hex(random_bytes(16)); $expires_at = date("Y-m-d H:i:s", strtotime("+15 minutes")); // Insert record with purpose 'email_verification' $stmt = $db->prepare("REPLACE INTO email_verifications (user_id, email, verification_code, expires_at, purpose) VALUES (?, ?, ?, ?, 'email_verification')"); $stmt->bind_param("isss", $user_id, $email, $verification_code, $expires_at); $stmt->execute(); $stmt->close(); // Send verification email via AWS SES using config settings $sesClient = new SesClient([ 'version' => 'latest', 'region' => $config['aws']['ses']['region'], 'credentials' => [ 'key' => $config['aws']['ses']['access_key'], 'secret' => $config['aws']['ses']['secret_key'] ] ]); $sender_email = $config['aws']['ses']['sender_email']; $recipient_email = $email; $subject = "Verify Your Email Address"; $verification_link = $config['app']['url'] . "/verify_email.php?code={$verification_code}"; $body_text = "Thank you for registering at " . $config['app']['name'] . ".\n\n"; $body_text .= "Please verify your email address by clicking the link below or by entering the verification code in your profile:\n\n"; $body_text .= "{$verification_link}\n\nYour verification code is: {$verification_code}\nThis code will expire in 15 minutes."; try { $result = $sesClient->sendEmail([ 'Destination' => [ 'ToAddresses' => [$recipient_email], ], 'ReplyToAddresses' => [$sender_email], 'Source' => $sender_email, 'Message' => [ 'Body' => [ 'Text' => [ 'Charset' => 'UTF-8', 'Data' => $body_text, ], ], 'Subject' => [ 'Charset' => 'UTF-8', 'Data' => $subject, ], ], ]); $_SESSION['success'] = "Registration successful! A verification email has been sent to your email address."; } catch (AwsException $e) { $_SESSION['error'] = "Registration successful, but failed to send verification email: " . $e->getAwsErrorMessage(); } header("Location: profile.php"); exit; } require_once 'includes/header.php'; ?>
' . htmlspecialchars($_SESSION['error']) . '
'; unset($_SESSION['error']); } ?>

Register

Already have an account? Login