1. 假设web入口为Index/index,在IndexController.class.php中
public function login() {
$this -> display();
}
public function doLogin() {
// 1. 接受值 2. 判断用户在数据库中是否存在
// 3. 存在,允许登录(当然还需要判断是否重复登录)4. 不存在,不允许登录
$userName = $_POST['userName'];
$password = $_POST['password'];
$code = $_POST['code'];
if (md5($code) != $_SESSION['code']) {
$this -> error('验证码不正确');
}
$user = M('User');
$where['userName'] = $userName;
$where['password'] = $password;
$result = $user -> field('id') -> where($where) -> find();
if ($result) {
$_SESSION['userName'] = $userName;
$_SESSION['userId'] = $result['id'];
$this -> success('登录成功', U('Index/index'));
} else {
$this -> error('该用户不存在');
}
}