zoukankan      html  css  js  c++  java
  • yii2 登录

      1 <?php
      2 
      3 namespace appaddonsfoodstoresmodels;
      4 use yiidbActiveRecord;
      5 use Yii;
      6 class User extends ActiveRecord
      7 {
      8     public $username;
      9     public $password;
     10     public function rules()
     11     {
     12         return [
     13             // username and password are both required
     14             [['username', 'password','email'], 'required','message'=>'不能为空'],
     15             //[['username'], 'unique','message'=>'{attribute}已经被占用了'],
     16             ['password', 'validatePass'],
     17         ];
     18     }
     19 
     20     public function validatePass()
     21     {
     22         if (!$this->hasErrors()) {
     23             $data = self::find()->where('username = :username and password = :password', [":username" => $this->username, ":password" => md5($this->password)])->one();
     24             if (is_null($data)) {
     25                 $this->addError("adminpass", "用户名或者密码错误");
     26             }
     27         }
     28 
     29     }
     30 
     31     public function login($data)
     32     {
     33         //$this->scenario = "login";
     34         if ($this->load($data) && $this->validate()) {
     35             //做点有意义的事
     36             //$lifetime = $this->rememberMe ? 24*3600 : 0;
     37             $lifetime = 3600*24;
     38             $session = Yii::$app->session;
     39             session_set_cookie_params($lifetime);
     40             $session['admin'] = [
     41                 'username' => $this->username,
     42                 'isLogin' => 1,
     43             ];
     44            // ip2long(Yii::$app->request->userIP)
     45             //$userIP = Yii::$app->request->userIP;
     46             $userIP ='180.96.11.189';
     47             $data = $this->post($userIP);
     48             $this->updateAll(['logintime' => time(), 'loginip' => $ip], 'username = :username', [':username' => $this->username]);
     49             return (bool)$session['admin']['isLogin'];
     50         }
     51         return false;
     52     }
     53 
     54         /*
     55     *根据新浪IP查询接口获取IP所在地
     56     */
     57     function getIPLoc_sina($queryIP){
     58         $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$queryIP;
     59         $ch = curl_init($url);
     60         //curl_setopt($ch,CURLOPT_ENCODING ,'utf8');
     61         curl_setopt($ch, CURLOPT_TIMEOUT, 10);
     62         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
     63         $location = curl_exec($ch);
     64         $location = json_decode($location);
     65         curl_close($ch);
     66 
     67         $loc = "";
     68         if($location===FALSE) return "";
     69         if($location < 0) return "";
     70         if (empty($location->desc)) {
     71             $loc = $location->province.'-'.$location->city.'-'.$location->district.'-'.$location->isp;
     72         }else{
     73             $loc = $location->desc;
     74         }
     75         return $loc;
     76     }
     77 
     78     public function post($ip,$https=true,$method='get',$data=null)
     79     {
     80         $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip;
     81         //1.初始化url
     82         $ch = curl_init($url);
     83         //2.设置相关的参数
     84         //字符串不直接输出,进行一个变量的存储
     85         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     86         //判断是否为https请求
     87         if($https === true){
     88           curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     89           curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     90         }
     91         //判断是否为post请求
     92         if($method == 'post'){
     93           curl_setopt($ch, CURLOPT_POST, true);
     94           curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
     95         }
     96         //3.发送请求
     97         $str = curl_exec($ch);
     98         //4.关闭连接
     99         curl_close($ch);
    100         //返回请求到的结果
    101         $location = json_decode($str);
    102         return $location->country;
    103   }
    104 
    105 }
     1 <?php
     2 
     3 namespace appaddonsfoodstorescontrollers;
     4 
     5 use yiiwebController;
     6 use appaddonsfoodstoresmodelsUser;
     7 use Yii;
     8 /**
     9  * Default controller for the `stores` module
    10  */
    11 class DefaultController extends Controller
    12 {
    13     /**
    14      * Renders the index view for the module
    15      * @return string
    16      */
    17     public $layout = false;
    18     public function actionIndex()
    19     {
    20         $this->layout = "layout2";
    21         $model = new User();
    22         if (Yii::$app->request->isPost) {
    23             $post = Yii::$app->request->post();
    24             if ($model->login($post)) {
    25                 $this->redirect(['default/test']);
    26                 Yii::$app->end();
    27             }
    28             //var_dump($_POST);die;
    29              //var_dump($this->username);
    30         }
    31     //$a = User::findOne(1);
    32        //var_dump($a);
    33         return $this->render('index',[
    34                 'model' => $model
    35             ]);
    36     }
    37 
    38     public function actionTest()
    39     {
    40         echo 'test';
    41     }
    42 }
     1 <?php
     2 
     3 /* @var $this yiiwebView */
     4 /* @var $form yiiootstrapActiveForm */
     5 /* @var $model commonmodelsLoginForm */
     6 
     7 use yiihelpersHtml;
     8 use yiiootstrapActiveForm;
     9 
    10 $this->title = 'Y+后台管理系统';
    11 $this->params['breadcrumbs'][] = $this->title;
    12 ?>
    13 
    14             <?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
    15 
    16                 <?= $form->field($model, 'username')->textInput(['autofocus' => true])->label('用户名') ?>
    17 
    18                 <?= $form->field($model, 'password')->passwordInput()->label('密码') ?>
    19                 <?= $form->field($model, 'email')->label('邮箱') ?>         
    20                 <div class="form-group">
    21                     <?= Html::submitButton('登录', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
    22                 </div>
    23 
    24             <?php ActiveForm::end(); ?>
    25        
  • 相关阅读:
    SGU 271 Book Pile (双端队列)
    POJ 3110 Jenny's First Exam (贪心)
    HDU 4310 Hero (贪心)
    ZOJ 2132 The Most Frequent Number (贪心)
    POJ 3388 Japanese Puzzle (二分)
    UVaLive 4628 Jack's socks (贪心)
    POJ 2433 Landscaping (贪心)
    CodeForces 946D Timetable (DP)
    Android Studio教程从入门到精通
    Android Tips – 填坑手册
  • 原文地址:https://www.cnblogs.com/lujiang/p/7422221.html
Copyright © 2011-2022 走看看