Home  >  Article  >  Backend Development  >  PHP class to generate random strings and verification codes

PHP class to generate random strings and verification codes

怪我咯
怪我咯Original
2017-07-04 13:57:341212browse

This article mainly introduces PHP examples of classes that generate random strings and verification codes. Friends in need can refer to them.

There are many online There are few codes and articles about PHP random numbers and verification codes that are really applicable.

Just make one yourself.

Let’s start this section’s php tutorial. The implementation of the following code is mainly to distinguish one get_code() and the other create_check_image(). The output image directly calls the latter one. session() When getting the verification code, just get_code() is ok. By the way, when using session, you must put session_star() at the front.

The code is as follows:

<?php
class RandCheckCode
{
        /*函数名称:get_code()
        *作用:取得随机字符串
        * 参数:
        1、(int)$length = 32 #随机字符长度
        2、(int)$mode = 0    #随机字符类型,
        0为大小写英文和数字,1为数字,2为小写字母,3为大写字母,
        4为大小写字母,5为大写字母和数字,6为小写字母和数字
        *返回:取得的字符串
        */
        function get_code($length=32,$mode=0)//获取随机验证码函数
        {
                switch ($mode)
                {
                        case &#39;1&#39;:
                                $str=&#39;123456789&#39;;
                                break;
                        case &#39;2&#39;:
                                $str=&#39;abcdefghijklmnopqrstuvwxyz&#39;;
                                break;
                        case &#39;3&#39;:
                                $str=&#39;ABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;;
                                break;
                        case &#39;4&#39;:
                                $str=&#39;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&#39;;
                                break;
                        case &#39;5&#39;:
                                $str=&#39;ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890&#39;;
                                break;
                        case &#39;6&#39;:
                                $str=&#39;abcdefghijklmnopqrstuvwxyz1234567890&#39;;
                                break;
                        default:
                                $str=&#39;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890&#39;;
                                break;
                }
                $checkstr=&#39;&#39;;
                $len=strlen($str)-1;
                for ($i=0;$i<$length;$i++)
                {
                        //$num=rand(0,$len);//产生一个0到$len之间的随机数
                        $num=mt_rand(0,$len);//产生一个0到$len之间的随机数
                        $checkstr.=$str[$num];

                       
                }
                return $checkstr;
        }

/**     函数名称:create_check_image()
        函数作用:产生一个校验码的图片
        参    数:$checkcode:校验码字符串
        返 回 值:返回该图片
*/
        function create_check_image($checkcode)//产生一个
        {
                $im=imagecreate(65,22);//产生一个图片
                $black=imagecolorallocate($im,0,0,0);//背景颜色
                $white=imagecolorallocate($im,255,255,255);//前景颜色
                $gray=imagecolorallocate($im,200,200,200);
                imagefill($im,30,30,$gray);//在$im图像的坐标30,30(图像左上角为0,0)处用$gray 颜色执行区域填充(即与30,30点颜色相同且相邻的点都会被填充)

                imagestring($im,5,8,3,$checkcode,$white);//用$white颜色将字符串$checkcode画到$im 所代表的图像的8,3坐标处(这是字符串左上角坐标,整幅图像的左上角为0,0),5是字体大小, 字体只能是1,2,3,4或5,使用内置字体
                for ($i=0;$i<120;$i++)
                {
                        $randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
                        imagesetpixel($im,rand()%70,rand()%30,$randcolor);//在$im图象上用$randcolor颜色在(rand()%70,rand()%30)坐标(图像左上角为0,0)上画一个点
                }
                header("Content-type:image/png");
                imagepng($im);//以PNG格式将图像输出到浏览器或文件
                imagedestroy($im);//销毁图像$im
        }
}
/*
$randcode=new RandCheckCode();
$checkstring=$randcode->get_code(5,7);
$image=$randcode->create_check_image($checkstring);
echo $image;
*/
?>

The above is the detailed content of PHP class to generate random strings and verification codes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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