当前位置:Gxlcms > PHP教程 > PHP绘图函数生成图片验证码

PHP绘图函数生成图片验证码

时间:2021-07-01 10:21:17 帮助过:4人阅读

PHP绘图函数生成图片验证码

// Set some important CAPTCHA constants
  define('CAPTCHA_NUMCHARShttps://www.gxlcms.com/', 6);  // number of characters in pass-phrase
  define('CAPTCHA_WIDTHhttps://www.gxlcms.com/', 100);   // width of image
  define('CAPTCHA_HEIGHThttps://www.gxlcms.com/', 25);   // height of image// Generate the random pass-phrase$pass_phrase = "";
  for ($i = 0; $i < CAPTCHA_NUMCHARS; $i++) {
    $pass_phrase .= chr(rand(97, 122));
  }

  // Store the encrypted pass-phrase in a session variable$_SESSION['pass_phrasehttps://www.gxlcms.com/'] = SHA($pass_phrase);

  // Create the image$img = imagecreatetruecolor(CAPTCHA_WIDTH, CAPTCHA_HEIGHT);

  // Set a white background with black text and gray graphics$bg_color = imagecolorallocate($img, 255, 255, 255);     // white$text_color = imagecolorallocate($img, 0, 0, 0);         // black$graphic_color = imagecolorallocate($img, 64, 64, 64);   // dark gray// Fill the background
  imagefilledrectangle($img, 0, 0, CAPTCHA_WIDTH, CAPTCHA_HEIGHT, $bg_color);
  // Draw some random linesfor ($i = 0; $i < 5; $i++) {
    imageline($img, 0, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphic_color);
  }

  // Sprinkle in some random dotsfor ($i = 0; $i < 50; $i++) {
    imagesetpixel($img, rand() % CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphic_color);
  }
  // Draw the pass-phrase string
  imagettftext($img, 18, 0, 5, CAPTCHA_HEIGHT - 5, $text_color, 'Courier New Bold.ttfhttps://www.gxlcms.com/', $pass_phrase);

  // Output the image as a PNG using a header
  header("Content-type: image/png");
  imagepng($img);

  // Clean up
  imagedestroy($img);
?>

在页面中使用这个生成验证码的脚本


  session_start();
?><htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"lang="en"><head><metahttp-equiv="Content-Type"content="text/html; charset=utf-8" /><title>Guitar Wars - Add Your High Scoretitle><linkrel="stylesheet"type="text/css"href="style.css" />head><body><h2>Guitar Wars - Add Your High Scoreh2>require_once('appvars.phphttps://www.gxlcms.com/');
  require_once('connectvars.phphttps://www.gxlcms.com/');

  if (isset($_POST['submithttps://www.gxlcms.com/'])) {
    // Connect to the database$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    // Grab the score data from the POST$name = mysqli_real_escape_string($dbc, trim($_POST['namehttps://www.gxlcms.com/']));
    $score = mysqli_real_escape_string($dbc, trim($_POST['scorehttps://www.gxlcms.com/']));
    $screenshot = mysqli_real_escape_string($dbc, trim($_FILES['screenshothttps://www.gxlcms.com/']['namehttps://www.gxlcms.com/']));
    $screenshot_type = $_FILES['screenshothttps://www.gxlcms.com/']['typehttps://www.gxlcms.com/'];
    $screenshot_size = $_FILES['screenshothttps://www.gxlcms.com/']['sizehttps://www.gxlcms.com/']; 

    // Check the CAPTCHA pass-phrase for verification$user_pass_phrase = SHA($_POST['verifyhttps://www.gxlcms.com/']);
    if ($_SESSION['pass_phrasehttps://www.gxlcms.com/'] == $user_pass_phrase) {
      if (!empty($name) && is_numeric($score) && !empty($screenshot)) {
        if ((($screenshot_type == 'image/gifhttps://www.gxlcms.com/') || ($screenshot_type == 'image/jpeghttps://www.gxlcms.com/') || ($screenshot_type == 'image/pjpeghttps://www.gxlcms.com/') || ($screenshot_type == 'image/pnghttps://www.gxlcms.com/'))
          && ($screenshot_size > 0) && ($screenshot_size <= GW_MAXFILESIZE)) {
          if ($_FILES['screenshothttps://www.gxlcms.com/']['errorhttps://www.gxlcms.com/'] == 0) {
            // Move the file to the target upload folder$target = GW_UPLOADPATH . $screenshot;
            if (move_uploaded_file($_FILES['screenshothttps://www.gxlcms.com/']['tmp_namehttps://www.gxlcms.com/'], $target)) {
              // Write the data to the database$query = "INSERT INTO guitarwars (date, name, score, screenshot) VALUES (NOW(), '$name', '$score', '$screenshot')";
              mysqli_query($dbc, $query);

              // Confirm success with the userecho'

Thanks for adding your new high score! It will be reviewed and added to the high score list as soon as possible.

https://www.gxlcms.com/'
; echo'

Name: https://www.gxlcms.com/' . $name . '
https://www.gxlcms.com/'
; echo'Score: https://www.gxlcms.com/' . $score . '
https://www.gxlcms.com/'
; echo'PHP绘图函数生成图片验证码

https://www.gxlcms.com/'
; echo'

<< Back to high scores

https://www.gxlcms.com/'
; // Clear the score data to clear the form$name = ""; $score = ""; $screenshot = ""; mysqli_close($dbc); } else { echo'

Sorry, there was a problem uploading your screen shot image.

https://www.gxlcms.com/'
; } } } else { echo'

The screen shot must be a GIF, JPEG, or PNG image file no greater than https://www.gxlcms.com/' . (GW_MAXFILESIZE / 1024) . ' KB in size.

https://www.gxlcms.com/'
; } // Try to delete the temporary screen shot image file @unlink($_FILES['screenshothttps://www.gxlcms.com/']['tmp_namehttps://www.gxlcms.com/']); } else { echo'

Please enter all of the information to add your high score.

https://www.gxlcms.com/'
; } } else { echo'

Please enter the verification pass-phrase exactly as shown.

https://www.gxlcms.com/'
; } } ?>
<hr /><formenctype="multipart/form-data"method="post"action=""><inputtype="hidden"name="MAX_FILE_SIZE"value="" /><labelfor="name">Name:label>
<inputtype="text"id="name"name="name"value="" /><br /><labelfor="score">Score:label><inputtype="text"id="score"name="score"value="" /><br /><labelfor="screenshot">Screen shot:label><inputtype="file"id="screenshot"name="screenshot" /><br /><labelfor="verify">Verification:label><inputtype="text"id="verify"name="verify"value="Enter the pass-phrase." /><imgsrc="captcha.php"alt="Verification pass-phrase" /><hr /><inputtype="submit"value="Add"name="submit" />form>body>html>

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('
  • ').text(i)); }; $numbering.fadeIn(1700); }); });

    以上就介绍了PHP绘图函数生成图片验证码,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

  • 人气教程排行