登录接口

来源:互联网 发布:知识更新速度数据 编辑:程序博客网 时间:2024/05/17 06:16

//表单页面

<center>    <form action="?r=user/login_pro" method="post">        <table>            <input type="hidden" id="_csrf" name="_csrf" value="<?php echo yii::$app->request->csrfToken?>"/>            <tr>                <td>用户名:</td>                <td><input type="text" name="name"/></td>            </tr>            <tr>                <td>密码:</td>                <td><input type="password" name="pwd"/></td>            </tr>            <tr>                <td><input type="submit"/></td>                <td></td>            </tr>        </table>    </form></center>
//前台控制器

 //接值,调用接口    public function actionLogin_pro(){        $request = yii::$app->request;        $time = time();//        echo $time;        $arr = $request->post();        unset($arr['_csrf']);       // 去空        $arr1 = $this->qukong($arr);       // 排序        ksort($arr1);        //拼接      $sign='name'."=".$arr1['name'].'&pwd='.$arr1['pwd'].'&time='.$time;        $sign1=md5($sign);//        print_r($sign1);die;        //调接口        $name=yii::$app->request->post('name');        $pwd=yii::$app->request->post('pwd');        $url = "http://localhost/dashixun1/jieko_token/book/public/Book/login1?name=$name&pwd=$pwd&time=$time&sign=$sign1";//        echo $url;die;        $str = file_get_contents($url);        $this->logResult($str);        $arr = json_decode($str,true);        $token = $arr['token'];        if(isset($token)) {            $session = Yii::$app->session;            $session['token'] = $token;            echo "登录成功";        }    }    function logResult($word='') {        $fp = fopen("css/log.txt","a");        flock($fp, LOCK_EX) ;        fwrite($fp,"执行日期:".strftime("%Y%m%d%H%M%S",time())."\n".$word."\n");        flock($fp, LOCK_UN);        fclose($fp);    }    public function actionLists(){        echo "123";    }       //去空    public function qukong($arr){        foreach ($arr as $k => $v) {            if($v==''){                unset($arr[$k]);            }        }        return $arr;    }    //排序    public function ksort($arr){        $arr = ksort($arr);        return $arr;    }    //拼接    public function pinjie($arr,$time){        $array = "";        foreach ($arr as $key => $val) {            $array .=$key."=".$val."&";        }        //去掉最后一个&符号        $array = substr($array,0,count($array)-2);        return $array."&time=$time";    }
//后台接口

 //登录接口    public function login1(Request $request){        $time2 = time();         $name = $request->input('name');        //echo $name;die;         $pwd = $request->input('pwd');         $time = $request->input('time');         $sign = $request->input('sign');         $sign1='name'."=".$name.'&pwd='.$pwd.'&time='.$time;//echo $sign1;die;        $sign2 = md5($sign1);//        echo $sign;die;        // 判断请求是否超时        if(($time2-$time)>5*60){            exit("请求超时");        }        //签名校验        if($sign == $sign2 ){            $arr = DB::table('user')->where('username',$name)->where('pwd',$pwd)->first();            if($arr){                $token='';                $token =MD5( $name."dan");                $l_id=$arr['id'];                $token1=DB::table('user')->where(['id'=>$l_id])->update(['token'=>$token]);//                print_r($token1);die;                //var_dump($arr);                return json_encode($arr);            }else{                return false;            }        }    }

0 0
原创粉丝点击