


How to implement PHP algorithm verification code and Chinese character verification code
在PHP网站开发中,验证码可以有效地保护我们的表单不被恶意提交,但是如果不使用算式验证码或者汉字验证码,仅仅使用简单的字母或者数字验证码,这样的验证码方案真的安全吗?
大家知道简单数字或者字母验证码很容易被破解,但是算式验证码或者中文汉字验证码不容易被破解,所以建议大家在使用验证码的时候,尽量用算式验证码或者中文汉字验证码。
下面是两种验证码代码,有用到的朋友可以参考下:
1.算式验证码:
<?php session_start(); header("Content-type: image/png"); $num1 = mt_rand(0,9);//第一位数 $num2 = mt_rand(1,9);//第二位数 $type_str = "+-*";//方法字符串集合 $type = substr($type_str,rand(0,2),1);//随机方法 $change = mt_rand(1,3); if($change==1){ $code = "$num1$type$num2=?"; $result = "\$verifyCode=$num1$type$num2;"; eval($result); $_SESSION['authnum_session'] = $verifyCode; }elseif($change==2){ $result = "\$verifyCode=$num1$type$num2;"; eval($result); $code = $num1.$type."_=".$verifyCode; $_SESSION['authnum_session'] = $num2; }elseif($change==3){ $result = "\$verifyCode=$num1$type$num2;"; eval($result); $code = "_".$type.$num2."=".$verifyCode; $_SESSION['authnum_session'] = $num1; } $im = imagecreate(68,28); $black = imagecolorallocate($im, 0,0,0); $white = imagecolorallocate($im, 255,255,255); $gray = imagecolorallocate($im, 200,200,200); $red = imagecolorallocate($im, 255, 0, 0); imagefill($im,0,0,$white); imagestring($im, 5, 10, 8, $code, $black); for($i=0;$i<70;$i++) { imagesetpixel($im, mt_rand(0, 58) , mt_rand(0, 28) , $black); imagesetpixel($im, mt_rand(0, 58) , mt_rand(0, 28) , $red); imagesetpixel($im, mt_rand(0, 58) , mt_rand(0, 28) , $gray); } imagepng($im); imagedestroy($im); ?>
2.中文汉字验证码:
<?php session_start(); $ch_str="的一是在了不和有大这主中人上为们地个用工时要动国产以我到他会作来分生对于学下级就年阶义发成部民可出能方进 同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反 体合斗路图把结第里正新开论之物从当两些还天资事队批如应形想制心样干都向变关点育重其思与间内去因件日利相"; $len = mb_strlen($ch_str,"utf-8");//汉字长度 $str = array(); for($i=0;$i<4;$i++){ $pos = mt_rand(0,$len-5);//开始位置 $str[] = mb_substr($ch_str,$pos,1,"utf-8"); } $authnum_session = implode("",$str); $_SESSION['authnum_session'] = $authnum_session; //记录到session Header("Content-type: image/PNG"); //图片的长和高 $image_x=100; $image_y=50; $im = imagecreate($image_x,$image_y); //这里取图片底色为白色 $bkg = ImageColorAllocate($im,255,255,255); //显示的字体样式,这个要把文件放到对应的目录中,如果你没有文件就去window的字体文件中找一个吧。 $fnt = "simhei.ttf"; //为图像分配一些颜色 $white=ImageColorAllocate($im,234,185,95); //在图片上画椭圆弧,指定下坐标点 imagearc($im, 150, 8, 20, 20, 75, 170, $white); imagearc($im, 180, 7,50, 30, 75, 175, $white); //在图片上画一条线段,指定下坐标点 imageline($im,20,20,180,30,$white); imageline($im,20,18,170,50,$white); imageline($im,25,50,80,50,$white); //乱点的数量 $noise_num=3000; $line_num=50; //各种混乱字符的颜色 $rectangle_color=imagecolorallocate($im,0xAA,0xAA,0xAA); $noise_color=imagecolorallocate($im,0x00,0x00,0x00); $font_color=imagecolorallocate($im,0x00,0x00,0x00); for($i=0;$i<$noise_num;$i++) { //在一个坐标点上画一个单一像素,这个点上面定义了,是黑色的。 //imagesetpixel($im,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color); } for($i=0;$i<$line_num;$i++) { $line_color=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //在两个坐标点间画一条线,颜色在上面定义 imageline($im,mt_rand(0,$image_x),mt_rand(0,$image_y),mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color); } for ($i=0;$i<4;$i++) { ImageTTFText($im, rand(18,20), rand(0,20), rand(($image_x/4)*$i+$image_x/100,($image_x/4)*$i+$image_x/8), rand($image_y/2+$image_y/10,$image_y/2+$image_y/5), $font_color, $fnt, $str[$i]); } ImagePNG($im); ImageDestroy($im); ?>
注意:
中文汉字验证码单独执行,然后获取session会发现验证码和session内容不一致。但是在img 标签中src属性中引用这个中文汉字验证码文件时,然后获取session,这时两者内容时一致的。
感兴趣的朋友可以对此进一步加以完善。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
