时间:2021-07-01 10:21:17 帮助过:20人阅读
require ('inc/config.php');
$page_title = '注册-CNode博客';
include ('inc/header.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require (MYSQL);
$trimmed = array_map('trim', $_POST);
$errors = array();
if ($_POST['code'] != $_SESSION['code']) {
$errors[] = '验证码不正确!';
}
if(empty($trimmed['username'])) {
$errors[] = '用户名不能为空';
} else {
if (preg_match ('/^[A-Z \'.-]{5,20}$/i', $trimmed['username'])) {
$un = mysqli_real_escape_string ($conn, $trimmed['username']);
} else {
$errors[] = '您的用户名格式不对';
}
}
if(empty($trimmed['email'])) {
$errors[] = '电子邮件地址不能为空';
} else {
if (filter_var($trimmed['email'], FILTER_VALIDATE_EMAIL)) {
$e = mysqli_real_escape_string ($conn, $trimmed['email']);
} else {
$errors[] = '您的电子邮件地址格式不对';
}
}
if(!empty($trimmed['password'])) {
if (preg_match ('/^\w{4,20}$/', $trimmed['password']) ) {
if ($trimmed['password'] == $trimmed['notpassword']) {
$p = mysqli_real_escape_string ($conn, $trimmed['password']);
} else {
$errors[] = '登录密码和确认密码不一致';
}
} else {
$errors[] = '请输入有效的登录密码';
}
} else {
$errors[] = '登录密码不能为空';
}
if(empty($trimmed['notpassword'])) {
$errors[] = '确认密码不能为空';
}
if (empty($errors)) {
// 确定电子邮件是否被使用
$q = "SELECT user_id FROM users WHERE email='$e'";
$r = mysqli_query ($conn, $q) or trigger_error("Query: $q\n
MySQL Error: " . mysqli_error($conn));
if (mysqli_num_row($r) == 0) { // 如果查询验证电子邮件地址未被使用,则注册用户
$a = md5(uniqid(rand(), true)); // 创建一个激活码(创建一个长度正好为32个字符的字符串)
$q = "INSERT INTO users (username, email, password, active, registration_date) VALUES ('$un', '$e', SHA1('$p'), '$a', NOW() )";
$r = mysqli_query ($conn, $q) or trigger_error("Query: $q\n
MySQL Error: " . mysqli_error($conn));
if (mysqli_affected_rows($conn) == 1) {
$body = "感谢您的注册,激活您的帐户,请点击这里:\n\n";
$body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a";
mail($trimmed['email'], '确认注册', $body, 'From: [email protected]');
echo '感谢您的注册!一封确认邮件已经发送到您的邮箱。请在该电子邮件中单击链接,以激活您的帐户。
';
exit();
} else {
echo '用户注册失败,为此我们表示抱歉,请在注册一次
';
}
} else { // 查询验证出来的电子邮件地址已经被注册
echo '电子邮件地址已经被注册
';
}
} else {
foreach ($errors as $msg) { // Print each error.
echo " - $msg
\n";
}
}
mysqli_close($conn);
}
?>
主页 > 注册