日期:2014-05-16 浏览次数:20598 次
<?php
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<br />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(