首先配置好ThinkPHP,可以参考官网手册,这里使用的数据库是mongodb,记得选择合适的ThinkPHP版本,这里使用的是ThinkPHP3.0RC1_full版本,否则会连不上mongodb,本人就因为这个问题折腾了一个晚上。以下是登录的实现:
入口文件,如果未登录则转到登录页面:
<?php
2 class IndexAction extends Action {
3 function index() {
4 if(isset($_SESSION['user'])) {
5 echo 'welcome to syqx';
// $this->display();
6 } else {
9 echo "<script>window.location.href='./User/login/';</script>";
10 }
11 }
12 }
13 ?>
业务逻辑,判断是否正确登录:
1 <?php
2
3 class UserAction extends Action {
4 function login() {
5 if(!isset($_POST['name'])) {
6 $this->display();
7 } else {
8 $name = $_POST['name'];
9 $password = $_POST['password'];
10 if($name && $password) {
11 $User = new UserModel('User');
13 $result = $User->login($name);
14 if($result["password"] == $password) {
15 $_SESSION['name'] = $name;
16 echo "welcome to syqx";
17 } else {
18 echo "<script language='javascript'> alert('Wrong Password! Pleas
e Retry'); history.back(-1);</script>";
19 }
20 } else {
21 echo "<script language='javascript'> alert('Empty Name or Password! Pleas
e Retry'); history.back(-1);</script>";
22 }
23 }
24 }
25 }
26 ?>
数据库操作,查询密码:
1 <?php
2 class UserModel extends MongoModel {
3 function login($name) {
4 $condition['name'] = $name;
5 $res = $this->field('password')->where($condition)->find();
6 return $res;
7 }
8
9 }
10 ?>