【问题标题】:Adding more number values to CAPTCHA向 CAPTCHA 添加更多数值
【发布时间】:2016-01-31 10:00:13
【问题描述】:

我几个月前写了这篇文章,前几天我意识到我犯了一个错误。此验证码仅显示小写字符 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);
?>

【问题讨论】:

    标签: php captcha


    【解决方案1】:

    如何像这样生成您的验证码:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 2015-01-08
      • 2014-12-15
      相关资源
      最近更新 更多