前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【thinkphp】app接口签名+验证签名

【thinkphp】app接口签名+验证签名

作者头像
96php.cn
发布2018-04-28 15:18:09
3K0
发布2018-04-28 15:18:09
举报
文章被收录于专栏:www.96php.cnwww.96php.cn

【thinkphp】app接口签名+验证签名 app接口签名+验证签名 比较简单 求各位大牛指教 IndexController.class.php

代码语言:javascript
复制
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        //缓存token过期时间
        F(session_id().'tokenTime',time() + 1200);
        $this->display();
    }
    /*
     * 接受数据
     */
    public function getMas(){
        //接受token参数,强制转换字符串
        $token=I('post.token/s');
        //验证
        $check=checkToken($token);
        if ($check== 10001){
            $this->ajaxReturn("接口时间过期");
        }elseif ($check== 10002){
            $this->ajaxReturn("非法调用接口");
        }elseif ($check== 10003){
            $this->ajaxReturn("正常!");
        }
        
    }
}

Common\function.php

代码语言:javascript
复制
/*
 * 验证token
 * 10001 时间过期
 * 10002 签名失败
 * 10003 验证通过
 */
function checkToken($token){
    //生成当前要验证的token
    $check=md5(session_id().'str1');
    //取过期时间
    $tokenTime=S(session_id().'tokenTime');
    //截取接收到的token里面的时间
    $time=substr($token,32);
    //截取接收到的token md5
    $token=substr($token,0,32);
    //判断是否过期
    if ($tokenTime>$time){
        //没有过期   
        if ($check == $token){
            //更新token过期时间
            F(session_id().'tokenTime',time() + 1200);
            //返回正常
            return 10003;
        }else {
            //签名验证失败
            return 10002;
        }
    }else {
        //返回过期,并且清空缓存
        S(session_id().'tokenTime',NULL);
        return 10001;
    }
}

模板写法

代码语言:javascript
复制
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>测试</title>
        <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>
        <input type="text" name="mas" id="mas" value="" />
        <input type="button" id="tijiao" value="点击发送" onclick="getMasg()" />
        <input type="hidden" name="token" id="token" value="{:md5(session_id().'str1').base64_encode(time())}" />
        
        <p>回复的消息:<span id="str"></span></p>
    </body>
    <script type="text/javascript">
        function getMasg(){
            $.ajax({
                url:'{:U('Home/Index/getMas')}',
                type:'POST', //GET
                data:{
                    token:$("#token").val(),
                },
                success:function(data){
                    $("#str").text(data)
                    console.log(data)
                },
            });            
        }
    </script>
</html>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年4月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档