Home > Backend Development > PHP Tutorial > How to make a PHP verification code

How to make a PHP verification code

炎欲天舞
Release: 2023-03-14 18:14:01
Original
3377 people have browsed it

PHP creates a verification code, mainly using array related functions

<?php
    header("content-type:text/html;charset=UTF-8");
    //建立3个范围数组
    $shuzi = range(0,9);
    $zimuda = range(&#39;A&#39;,&#39;Z&#39;);
    $zimuxiao = range(&#39;a&#39;,&#39;z &#39;);

    //将这3个数组合并成新数组
    $arr=array_merge($shuzi,$zimuxiao,$zimuda);

    //遍历新数组
    foreach ($arr as $key => $value) {
        //如果新数组里的元素包含下面的字,则将其删除(unset);
        if($value == &#39;0&#39; || $value == &#39;1&#39; ||$value == &#39;2&#39; ||$value == &#39;o&#39;  ||$value == &#39;O&#39; 
            ||$value == &#39;I&#39; ||$value == &#39;i&#39; ||$value == &#39;Z&#39; ||$value == &#39;z&#39; ||$value == &#39;l&#39;){
            //删除键,值也随之删除
            unset ($arr[$key]);
            }
        }

    //将数组 元素打乱,其KEY值跟随元素被打乱
    shuffle($arr);
    //随机取出原数组的4个key值
    $arr_rand = array_rand($arr,4);
    //将原数组的key遍历输出,得到4个随机验证码
    foreach ($arr_rand as $value) {
        echo $arr[$value];
    }

    //优化一下:0,1,2这3个数原本就需要排除,刚好在数组最前、连续,在定义范围数组的时候可以不写,直接从3开始,减少if判断;大小写字母Z在最后也可以不写
Copy after login

The above is the detailed content of How to make a PHP verification code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template