美文网首页
THINKPHP5开发API笔记

THINKPHP5开发API笔记

作者: 7abbcd54a89d | 来源:发表于2017-08-14 20:11 被阅读0次

    1.解决跨域的析构方法

    protected  function _initialize()
        {
            header('content-type:text/html;charset=utf-8');
            header('Access-Control-Allow-Origin: *');
            header('Access-Control-Allow-Header:Origin,X-Requested-With,Content-Type,Accept');
            header('Access-Control-Allow-Methods:GET,POST,PUT');
            ksort($_POST);
            ksort($_GET);
        }
    

    2.生成token

    public  function creat_token()
        {
           $token=md5(mt_rand(111111,999999) . time());
           return $token;
        }
    

    3.根据token判断用户身份

    public  function creat_token()
        {
          $token = trim($_POST['token']);
            $mid = Db::name('token')->where("token='{$token}'")->value('mid');
            if ($mid > 0) {
                return $mid;
            } else {
                ajax_msg('非法数据', 0);
            }
        }
    

    4.API数据返回接口

     public function ajax_msg($msg = "", $status = 1, $data = "", $errcode = "")
        {
            $json['$msg'] = $msg;
            $json['$status'] = $status;
            $json['$data'] = $data;
            if ($errcode) {
                $json['$errcode'] = $errcode;
            }
            echo json_encode($json, true);
            exit;
        }
    

    相关文章

      网友评论

          本文标题:THINKPHP5开发API笔记

          本文链接:https://www.haomeiwen.com/subject/lukprxtx.html