.navbar.scrolled {
background-color: rgba(255, 255, 255, 0.95) !important;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
';
// 导航栏
echo '';
// 为内容添加顶部内边距
echo '
';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = $_POST['password'];
if (empty($username) || empty($email) || empty($password)) {
$error = '用户名、邮箱和密码不能为空';
} elseif (empty($_POST['agree'])) {
$error = '必须同意 APP 审核标准才能注册';
} elseif (empty($_POST['agree_privacy'])) {
$error = '必须同意隐私政策才能注册';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = '请输入有效的邮箱地址';
} else {
// 检查数据库连接是否为 PDO 对象
if (!($conn instanceof mysqli)) {
log_error('数据库连接错误: 连接不是MySQLi实例', __FILE__, __LINE__);
$error = '数据库连接错误,请检查配置';
} else {
try {
$stmt = $conn->prepare('SELECT id FROM developers WHERE username = ? OR email = ?');
$stmt->bind_param('ss', $username, $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$error = '用户名或邮箱已被注册';
} else {
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// 生成验证令牌
$verificationToken = bin2hex(random_bytes(32));
$insertStmt = $conn->prepare('INSERT INTO developers (username, email, password, verification_token) VALUES (?, ?, ?, ?)');
$insertStmt->bind_param('ssss', $username, $email, $hashedPassword, $verificationToken);
if (!$insertStmt->execute()) {
log_error('插入执行失败: ' . $insertStmt->error, __FILE__, __LINE__);
$error = '系统错误,请稍后再试';
} else {
// 生成验证链接
$verificationLink = 'http://' . $_SERVER['HTTP_HOST'] . '/developer/verify_email.php?token=' . urlencode($verificationToken);
// 加载邮件模板
$templatePath = __DIR__ . '/../mail/verification_template.php';
if (file_exists($templatePath)) {
$templateContent = file_get_contents($templatePath);
$templateContent = str_replace('{username}', htmlspecialchars($username), $templateContent);
$templateContent = str_replace('{verification_link}', $verificationLink, $templateContent);
// 调试日志测试
$testLogDir = 'c:\\web\\app2\\logs';
$testLogFile = $testLogDir . '\\test.log';
if (!is_dir($testLogDir)) {
mkdir($testLogDir, 0755, true);
}
file_put_contents($testLogFile, date('[Y-m-d H:i:s] ') . '邮件发送代码开始执行' . PHP_EOL, FILE_APPEND);
// 添加SMTP配置调试日志
log_error('SMTP配置参数 - HOST: ' . (defined('SMTP_HOST') ? SMTP_HOST : '未定义') . ', PORT: ' . (defined('SMTP_PORT') ? SMTP_PORT : '未定义') . ', USERNAME: ' . (defined('SMTP_USERNAME') ? SMTP_USERNAME : '未定义') . ', ENCRYPTION: ' . (defined('SMTP_ENCRYPTION') ? SMTP_ENCRYPTION : '未定义'), __FILE__, __LINE__);
log_error('开始执行邮件发送流程', __FILE__, __LINE__);
// 配置SMTP邮件发送
require_once '../vendor/phpmailer/phpmailer/src/PHPMailer.php';
require_once '../vendor/phpmailer/phpmailer/src/SMTP.php';
/** @var \PHPMailer\PHPMailer\PHPMailer $mail */
$mail = new \PHPMailer\PHPMailer\PHPMailer(true);
try {
$mail->isSMTP();
$mail->SMTPDebug = 4;
// 输出当前SMTP配置参数用于调试
log_error('SMTP配置参数: HOST=' . SMTP_HOST . ', PORT=' . SMTP_PORT . ', USERNAME=' . SMTP_USERNAME . ', ENCRYPTION=' . SMTP_ENCRYPTION);
// 检查openssl扩展是否启用
log_error('OpenSSL扩展状态: ' . (extension_loaded('openssl') ? '已启用' : '未启用')); // 启用详细调试
$mail->Debugoutput = function($str, $level) {
$logDir = 'c:\\web\\app2\\logs';
if (!is_dir($logDir)) {
mkdir($logDir, 0755, true);
}
file_put_contents($logDir . '\\smtp_debug.log', date('[Y-m-d H:i:s] ') . $str . PHP_EOL, FILE_APPEND);
};
$mail->Host = defined('SMTP_HOST') ? SMTP_HOST : 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = defined('SMTP_USERNAME') ? SMTP_USERNAME : ''; // Ensure SMTP_USERNAME is defined in config.php
$mail->Password = defined('SMTP_PASSWORD') ? SMTP_PASSWORD : '';
$mail->SMTPSecure = defined('SMTP_ENCRYPTION') ? SMTP_ENCRYPTION : 'tls'; // Ensure SMTP_ENCRYPTION is defined in config.php
$mail->AuthType = 'PLAIN'; // 尝试使用PLAIN认证方式
$mail->Port = defined('SMTP_PORT') ? SMTP_PORT : 587;
$mail->CharSet = 'UTF-8';
$mail->setFrom(defined('SMTP_FROM_EMAIL') ? SMTP_FROM_EMAIL : 'noreply@example.com', defined('SMTP_FROM_NAME') ? SMTP_FROM_NAME : 'App Store'); // Ensure SMTP_FROM_EMAIL is defined in config.php
$mail->addAddress($email, $username);
$mail->isHTML(true);
$mail->Subject = '邮箱验证 - ' . (defined('APP_STORE_NAME') ? APP_STORE_NAME : 'App Store');
$mail->Body = $templateContent;
$mail->send();
} catch (\PHPMailer\PHPMailer\Exception $e) {
log_error('邮件发送失败: ' . $mail->ErrorInfo, __FILE__, __LINE__);
}
} else {
log_error('验证邮件模板不存在: ' . $templatePath, __FILE__, __LINE__);
}
header('Location: login.php?register_success=1&verify_email_sent=1');
exit;
}
}
} catch (PDOException $e) {
$error = '注册时发生错误,请稍后再试';
}
}
}
}
?>
开发者注册