登录验证之图片点选字符串详解编程语言

实现思路:

1、首先在一个范围内生成坐标点,然后选出x坐标不相同的几个坐标,每个坐标之间保持一定的距离

2、验证时,根据前端传回来的坐标点,做判断,表示在一定范围内及正确,否则刷新重新选择

代码展示:

首先生成不重复的随机字符串

    /** 
     * 返回随机字符串 
     * @param string/array $str 字符串或者数组 
     * @param string $length 长度 
    */ 
    private function make_rand_str($str,$length = 4) 
    { 
        if (!is_array($str)){ 
            $str = explode(',',$str); 
        } 
        // 在 $chars 中随机取 $length 个数组元素键名 
        $keys = array_rand($str,$length); 
        $rand = ''; 
        for($i = 0; $i < $length; $i++) 
        { 
            // 将 $length 个数组元素连接成字符串 
            $rand .= $str[$keys[$i]]; 
        } 
        return $rand; 
    }

 

其次在一定范围内生成坐标点,返回不相邻的几个坐标点

 

    /** 
     * 生成坐标点方法 
     * @param string $width 宽 
     * @param string $height 高 
     * @param string $legth 需要生成的长度 
    */ 
    private function create_position($width,$height,$length = 4){ 
        $Allpoint = []; 
        //在范围内生成所有可能的坐标点 设置字符串坐标点的起始位置 距离边设置一定的最小距离 
        //设置最左边的值 
        $left_width = floor(intval($width/10)); 
        //设置最右边的值 
        $right_width = floor($width-intval($width/10)); 
        //设置最下边的值 
        $down_height = floor($height-intval($height/10)); 
        //设置间隔值 
        $middle_size = intval(floor($width-$left_width-$left_width)/4)-1; 
//        echo $width;die; 
        for ($i = $left_width; $i <= $right_width; $i++){ 
            for ($j = 20; $j <= $down_height; $j ++){ 
                $point = $i.','.$j; 
                $j = $j + 2; 
                $Allpoint[] = $point; 
            } 
            $i = $i + $middle_size; 
        } 
        //所有坐标点的可能性数量 
        $count = count($Allpoint); 
        $selectPoint = []; 
        $xArr = []; 
        //从所有的坐标点中取出4个 
        for ($i = 0 ; $i < $length ; $i ++){ 
            //从所有坐标点的可能性中取随机数 
            $index = rand(0,intval($count-1)); 
            //取出随机数中的坐标 
            $point = $Allpoint[$index]; 
            //做处理 
            $pointArr = explode(',',$point); 
            $x = $pointArr[0]; 
            //判断坐标点x是否已经存在 如果存在重新生成 不存在 
            if (!in_array($x,$xArr)){ 
                $selectPoint[] = $point; 
                $xArr[] = $x; 
            } else { 
                $i--; 
            } 
        } 
        return $selectPoint; 
    }

 

最后把坐标点和文字字符串一一对应匹配

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/16329.html

(0)
上一篇 2021年7月19日 19:14
下一篇 2021年7月19日 19:14

相关推荐

发表回复

登录后才能评论