An PHP instance of a class that generates random strings and captchas
- 2020-11-26 18:44:31
- OfStack
There are a lot of php random Numbers and captCHA codes and articles on the Internet, but few are really applicable.
Just do it yourself.
Start the php tutorial in this section, the following code implementation, mainly do can be very good to distinguish 1 get_code(), another create_check_image(), output image directly call the following, session() to get the captchas get_code() on ok, by the way when using session must be session_star() in the first place.
The code is as follows:
<?php
class RandCheckCode
{
/* The name of the function :get_code()
* role : Get a random string
* parameter :
1 , (int)$length = 32 # Random character length
2 , (int)$mode = 0 # Random character type,
0 For case and Numbers ,1 For the digital ,2 Is a lowercase letter ,3 Is capital letter ,
4 Is upper and lower case ,5 Are capital letters and Numbers ,6 Are lowercase letters and Numbers
* return : Retrieved string
*/
function get_code($length=32,$mode=0)// Gets a random captcha function
{
switch ($mode)
{
case '1':
$str='123456789';
break;
case '2':
$str='abcdefghijklmnopqrstuvwxyz';
break;
case '3':
$str='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case '4':
$str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
break;
case '5':
$str='ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
break;
case '6':
$str='abcdefghijklmnopqrstuvwxyz1234567890';
break;
default:
$str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
break;
}
$checkstr='';
$len=strlen($str)-1;
for ($i=0;$i<$length;$i++)
{
//$num=rand(0,$len);// produce 1 a 0 to $len Between random Numbers
$num=mt_rand(0,$len);// produce 1 a 0 to $len Between random Numbers
$checkstr.=$str[$num];
}
return $checkstr;
}
/** The name of the function :create_check_image()
Function are : produce 1 A picture of a check code
ginseng The number :$checkcode: Check code string
return Back to the value : Return the image
*/
function create_check_image($checkcode)// produce 1 a
{
$im=imagecreate(65,22);// produce 1 A picture
$black=imagecolorallocate($im,0,0,0);// The background color
$white=imagecolorallocate($im,255,255,255);// The foreground color
$gray=imagecolorallocate($im,200,200,200);
imagefill($im,30,30,$gray);// in $im Coordinates of the image 30,30( Image top left corner is 0,0) Place with $gray Color execution area fill ( with 30,30 Points of the same color and adjacent points will be filled )
imagestring($im,5,8,3,$checkcode,$white);// with $white Color string $checkcode painting $im Represents the image of 8,3 Coordinate place ( This is the upper-left coordinate of the string , The upper left corner of the entire image is 0,0),5 It's the font size , Fonts can only be 1,2,3,4 or 5, Use built-in fonts
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);// in $im Images with $randcolor Color in (rand()%70,rand()%30) coordinates ( Image top left corner is 0,0) On the picture 1 A point
}
header("Content-type:image/png");
imagepng($im);// In order to PNG Format outputs an image to a browser or file
imagedestroy($im);// Destroy the image $im
}
}
/*
$randcode=new RandCheckCode();
$checkstring=$randcode->get_code(5,7);
$image=$randcode->create_check_image($checkstring);
echo $image;
*/
?>