<?php
/**
* Validator for Register
*/
final class RegisterValidator {
private function __construct() {
}
/**
* Validate the given username
* @param $username
* @return array array of {@link Error} s
*/
public static function validate($username
$errors = array();
$username = trim($username);
$password = trim($password);
if (!$username) {
$errors[] = new Error(
} elseif (strlen($username)<
$errors[] = new Error(
} elseif (strlen($username)>
$errors[] = new Error(
} elseif (!preg_match(
$errors[] = new Error(
} elseif (!preg_match(
$errors[] = new Error(
} elseif (!$password) {
$errors[] = new Error(
} elseif (strlen($password)<
$errors[] = new Error(
} elseif (strlen($password)>
$errors[] = new Error(
} elseif (!preg_match(
$errors[] = new Error(
} elseif ($password != trim($repeat_password)) {
$errors[] = new Error(
} elseif (!Utils::isValidEmail($email)) {
$errors[] = new Error(
} else {
// check whether user exists or not
$dao = new UserDao();
$user = $dao
if ($user) {
$errors[] = new Error(
}
$user = null;
// check whether email being used or not
$user = $dao
if ($user) {
$errors[] = new Error(
}
}
return $errors;
}
}
?>
$username = null;
$password = null;
$repeat_password = null;
$email = null;
$msg = "";
if (isset($_POST[
&& isset($_POST[
$username = addslashes(trim(stripslashes($_POST [
$password = addslashes(trim(stripslashes($_POST [
$repeat_password = addslashes(trim(stripslashes($_POST [
$email = addslashes(trim(stripslashes($_POST [
// validate
$errors = RegisterValidator::validate($username
// validate
if (empty($errors)) {
// save
$dao = new UserDao();
$user = new User();
$user
$last_login_ip = Utils::getIpAddress();
$user
$user
$salt = substr(sha
$hash_password = sha
$user
$user
$user = $dao
if ($user) {
UserLogin::setUserInfo($user);
Flash::addFlash(
}
else {
Flash::addFlash(
}
Utils::redirect(
}
foreach ($errors as $e) {
$msg
}
<?php
/**
* Validation error
*/
final class Error {
private $source;
private $message;
/**
* Create new error
* @param mixed $source source of the error
* @param string $message error message
*/
function __construct($source
$this
$this
}
/**
* Get source of the error
* @return mixed source of the error
*/
public function getSource() {
return $this
}
/**
* Get error message
* @return string error message
*/
public function getMessage() {
return $this
}
}
?>
From:http://tw.wingwit.com/Article/program/PHP/201311/21003.html