ThinkPHP实现登录

         首先配置好ThinkPHP,可以参考官网手册,这里使用的数据库是mongodb,记得选择合适的ThinkPHP版本,这里使用的是ThinkPHP3.0RC1_full版本,否则会连不上mongodb,本人就因为这个问题折腾了一个晚上。以下是登录的实现:

入口文件,如果未登录则转到登录页面:

      2         class IndexAction extends Action {
      3                 function index() {
      4                         if(isset($_SESSION['user'])) {
      5                                 echo 'welcome to syqx';

                                 //      $this->display();
      6                         } else {
      9                                 echo "";
     10                         }
     11                 }
     12         }
     13 ?>

业务逻辑,判断是否正确登录:

 1       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 "";
     19                                         }
     20                                 } else {
     21                                         echo "";
     22                                 }
     23                         }
     24                 }
     25         }
     26 ?>

数据库操作,查询密码:

  1       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 ?>


你可能感兴趣的:(PHP)