zoukankan      html  css  js  c++  java
  • 登录验证之图片点选字符串

    实现思路:

    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是否已经存在 如果存在重新生成 不存在
                while (!in_array($x,$xArr)){
                    $selectPoint[] = $point;
                    $xArr[] = $x;
                }
            }
            return $selectPoint;
        }

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

    以上就是这次的全部内容!

  • 相关阅读:
    Django-models,继承AbstractUser类
    Django-views,用户认证,login_requierd()
    django前篇
    jquery插件
    jquery事件及插件
    jquery操作元素
    jquery选择器筛选器
    js作用域与作用域链
    js之DOM(二)
    bootstrap
  • 原文地址:https://www.cnblogs.com/jingxiaoniu/p/9558797.html
Copyright © 2011-2022 走看看