短信時間驗證

在作短信接口的時候,短信接口的安全性值得注意。分享一個函數,服務器端檢驗60秒內不能重複發短信.php

    /**
     * 用於檢測當前用戶IP的可操做性,time_span爲驗證的時間間隔 秒
     *
     * @param string $ip_str  IP地址
     * @param string $module  操做的模塊     *
     * @param integer $time_span 間隔
     * @param integer $id   操做的數據
     *
     * @return  boolean
     */
    function check_ip_operation($ip_str,$module,$time_span=0,$id=0)
    {
        if(empty($_SESSION[$module."_".$id."_ip"]))
        {
            $check['ip']    = $ip_str;//    get_client_ip();
            $check['time']    =    a_gmtTime();
            $_SESSION[$module."_".$id."_ip"] = $check;

            return true;  //不存在session時驗證經過
        }
        else
        {
            $check['ip']    =    $ip_str;// get_client_ip();
            $check['time']    =    a_gmtTime();
            $origin    =    $_SESSION[$module."_".$id."_ip"];

            if($check['ip']==$origin['ip'])
            {
                if($check['time'] - $origin['time'] < $time_span)
                {
                    return false;
                }
                else
                {
                    $_SESSION[$module."_".$id."_ip"] = $check;
                    return true;  //不存在session時驗證經過
                }
            }
            else
            {
                $_SESSION[$module."_".$id."_ip"] = $check;
                return true;  //不存在session時驗證經過
            }
        }
    }
相關文章
相關標籤/搜索