这是我几个月前写的,有一天我意识到我犯了一个错误。此验证码仅显示小写字符a-z (97-122)。如何添加大写A-Z (65-90)和0-9 (48-57)?我把需要修理的地方和其他地方分开了。
我首先尝试做的是:
for ($i = 0; $i < CAPTCHACharas; $i++) {
$PassPhrase .= chr (rand (48, 57));
$PassPhrase .= chr (rand (65, 90));
$PassPhrase .= chr (rand (97, 122));
}但它只用"a-z“做了两倍的时间。然后我试着:
for ($i = 0; $i < CAPTCHACharas; $i++) {
$PassPhrase .= chr (rand (48, 57), (65, 90), (97, 122));
}但没有成功。我还用) + (替换了), (,但它不起作用。谢谢你的帮助。
PHP验证码:
<?php
session_start ();
// Sets some important definitions
define ("CAPTCHACharas", 7); // the number of characters in the pass-phrase
define ("CAPTCHAWidth", 100); // the width of the image
define ("CAPTCHAHeight", 40); // the height of the image
// Generates the random pass-phrase
$PassPhrase = "";
// The characters used
for ($i = 0; $i < CAPTCHACharas; $i++) {
// "48" - "57" = "0" - "9"
// "65" - "90" = "A" - "Z"
// "97" - "122" = "a" - "z"
$PassPhrase .= chr (rand (97, 122));
}
// Store the encrypted pass-phrase in a session variable
// Connects to the form.
$_SESSION ["PassPhrase"] = SHA1 ($PassPhrase);
// Creates the image
$img = imagecreatetruecolor (CAPTCHAWidth, CAPTCHAHeight);
// Set the background, text and line\dots colors
$BackgroundColor = imagecolorallocate ($img, 255, 255, 255); // current color: white
$TextColor = imagecolorallocate ($img, 0, 0, 0); // current color: black
$BorderColor = imagecolorallocate ($img, 64, 64, 64); // current color: dark gray
// Fills the background
imagefilledrectangle ($img, 0, 0, CAPTCHAWidth, CAPTCHAHeight, $BackgroundColor);
// Formats everything
imagettftext ($img, 18, 0, 5, CAPTCHAHeight - 5, $TextColor, "captcha-1.ttf", $PassPhrase);
// Draws some random lines here and there.
for ($i = 0; $i < 5; $i++) {imageline ($img, 0, rand () % CAPTCHAHeight, CAPTCHAWidth, rand () % CAPTCHAHeight, $BorderColor);}
// Puts in some random dots here and there.
for ($i = 0; $i < 50; $i++) {imagesetpixel ($img, rand () % CAPTCHAWidth, rand () % CAPTCHAHeight, $BorderColor);}
// Output the image as a ".png" file using a header
header ("content-type: image/png");
imagepng ($img);
imagedestroy ($img);
?>发布于 2015-10-31 05:33:31
像这样生成验证码怎么样:
define( 'CAPTCHACharas', 7 );
// Allowed characters
$letters = 'ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789';
$letters_no = strlen( $letters );
$PassPhrase = '';
// Generate CAPCHA
for ( $i=0; $i<CAPTCHACharas; $i++ ) {
$PassPhrase .= $letters{rand( 0, $letters_no )};
}
// Use it like you used it before
echo $PassPhrase;https://stackoverflow.com/questions/33445099
复制相似问题