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

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

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

/*
 * 验证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;
    }
} 

模板写法

<!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>

各位大牛看看有何问题没有 求指教

转载于:https://my.oschina.net/u/2367105/blog/879526

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值