用户注册的代码至少包含三个部分:
A.注册表单;
B.客户端javascript验证代码;
C.服务器端验证和表单信息入库代码。

因此需要写一个注册模块的控制器类:RegisterController.php:
代码如下:
<?php
include_once 'GoodstypeController.php';
class RegisterController extends GoodstypeController {

//这个方法是加载注册表单页的。
function registerAction() {
//获取大类及各类最新商品的信息
self::$smarty->caching = 2;
if(!self::$smarty->is_cached('reg/regist.tpl')) {
$this -> getTypeInfo(new GoodsType());
self::$smarty->cache_lifetime = 3600*24*10;
}
self::$smarty -> display('reg/regist.tpl');
}

//该方法是ajax验证用户名是否已经被注册的。
function checkusernameAction() {
$username = trim($_POST['name']);
$model_user = new User();
$count = $model_user->selectCount("username = '$username'");
if($count>0){
echo 'false';
} else {
echo 'true';
}
}

//该方法是ajax验证验证码是否正确的。
function checkrandcodeAction() {
if($_POST['code'] != $_SESSION['checkcode']){
echo "false";
} else {
echo "true";
}
}

/*
该方法是实现注册信息入数据库的。
服务器端验证的流程依次为:
判断是否跨域提交、验证码是否正确、必填信息是否为空、注册信息入库、注册成功后赋值session,并进入到会员中心。
*/
function regokAction() {
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$model_user = new User();

$checkcode = $_POST['checkcode'];
if(!$checkcode || $checkcode != $_SESSION['checkcode']){
echo "<script>alert('验证码输入错误!');history.back();</script>";die;
}
unset($_SESSION['checkcode']);

include_once HELPER_DIR . 'CommonFunction.class.php';
$membername = CommonFunction::filter_html(trim($_POST['membername']));
$password = CommonFunction::filter_html(trim($_POST['password0']));
$pwdquestion = CommonFunction::filter_html(trim($_POST['pwdquestion']));
$pwdanswer = CommonFunction::filter_html(trim($_POST['pwdanswer']));
$email = CommonFunction::filter_html(trim($_POST['email']));

if(!$membername || !$password || !$email || !$pwdquestion || !$pwdanswer) {
echo "<script>alert('注册信息填写不完全,请重新操作!');history.back();</script>";die;
}

$count = $model_user->selectCount("username = '$membername'");
if($count>0) {
echo "<script>alert('该用户名已经存在,请重新注册!');history.back();</script>";die;
}

$data['username'] = $membername;
$data['password'] = $password;
$data['pwdquestion'] = $pwdquestion;
$data['pwdanswer'] = $pwdanswer;
$data['email'] = $email;
$data['realname'] = CommonFunction::filter_html(trim($_POST['realname']));
$data['sex'] = $_POST['sex'];
$data['address'] = CommonFunction::filter_html(trim($_POST['address']));
$data['qq'] = CommonFunction::filter_html(trim($_POST['qq']));
$data['tel'] = CommonFunction::filter_html(trim($_POST['tel']));
$data['regip'] = CommonFunction::get_clientip();

if($model_user->insert($data)) {
$_SESSION['username'] = $membername;
$_SESSION['password'] = md5($password);
echo "<script>alert('注册成功!');window.location.href='/';</script>";die;
} else {
echo "<script>alert('注册失败!'); history.back();</script>";
}
}
}

}
?>