PHP drawing function generates image verification code

WBOY
Release: 2016-07-29 09:02:53
Original
908 people have browsed it

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

<code><?php
  session_start();

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

  <span>// Store the encrypted pass-phrase in a session variable</span><span>$_SESSION</span>[<span>'pass_phrase'</span>] = SHA(<span>$pass_phrase</span>);

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

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

  <span>// Sprinkle in some random dots</span><span>for</span> (<span>$i</span> = <span>0</span>; <span>$i</span> < <span>50</span>; <span>$i</span>++) {
    imagesetpixel(<span>$img</span>, <span>rand</span>() % CAPTCHA_WIDTH, <span>rand</span>() % CAPTCHA_HEIGHT, <span>$graphic_color</span>);
  }
  <span>// Draw the pass-phrase string</span>
  imagettftext(<span>$img</span>, <span>18</span>, <span>0</span>, <span>5</span>, CAPTCHA_HEIGHT - <span>5</span>, <span>$text_color</span>, <span>'Courier New Bold.ttf'</span>, <span>$pass_phrase</span>);

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

  <span>// Clean up</span>
  imagedestroy(<span>$img</span>);
?></code>
Copy after login

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

<code><span><span><?php</span>
  session_start();
<span>?></span></span><span><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></span><span><<span>html</span><span>xmlns</span>=<span>"http://www.w3.org/1999/xhtml"</span><span>xml:lang</span>=<span>"en"</span><span>lang</span>=<span>"en"</span>></span><span><<span>head</span>></span><span><<span>meta</span><span>http-equiv</span>=<span>"Content-Type"</span><span>content</span>=<span>"text/html; charset=utf-8"</span> /></span><span><<span>title</span>></span>Guitar Wars - Add Your High Score<span></<span>title</span>></span><span><<span>link</span><span>rel</span>=<span>"stylesheet"</span><span>type</span>=<span>"text/css"</span><span>href</span>=<span>"style.css"</span> /></span><span></<span>head</span>></span><span><<span>body</span>></span><span><<span>h2</span>></span>Guitar Wars - Add Your High Score<span></<span>h2</span>></span><span><span><?php</span><span>require_once</span>(<span>'appvars.php'</span>);
  <span>require_once</span>(<span>'connectvars.php'</span>);

  <span>if</span> (<span>isset</span>(<span>$_POST</span>[<span>'submit'</span>])) {
    <span>// Connect to the database</span><span>$dbc</span> = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    <span>// Grab the score data from the POST</span><span>$name</span> = mysqli_real_escape_string(<span>$dbc</span>, trim(<span>$_POST</span>[<span>'name'</span>]));
    <span>$score</span> = mysqli_real_escape_string(<span>$dbc</span>, trim(<span>$_POST</span>[<span>'score'</span>]));
    <span>$screenshot</span> = mysqli_real_escape_string(<span>$dbc</span>, trim(<span>$_FILES</span>[<span>'screenshot'</span>][<span>'name'</span>]));
    <span>$screenshot_type</span> = <span>$_FILES</span>[<span>'screenshot'</span>][<span>'type'</span>];
    <span>$screenshot_size</span> = <span>$_FILES</span>[<span>'screenshot'</span>][<span>'size'</span>]; 

    <span>// Check the CAPTCHA pass-phrase for verification</span><span>$user_pass_phrase</span> = SHA(<span>$_POST</span>[<span>'verify'</span>]);
    <span>if</span> (<span>$_SESSION</span>[<span>'pass_phrase'</span>] == <span>$user_pass_phrase</span>) {
      <span>if</span> (!<span>empty</span>(<span>$name</span>) && is_numeric(<span>$score</span>) && !<span>empty</span>(<span>$screenshot</span>)) {
        <span>if</span> (((<span>$screenshot_type</span> == <span>'image/gif'</span>) || (<span>$screenshot_type</span> == <span>'image/jpeg'</span>) || (<span>$screenshot_type</span> == <span>'image/pjpeg'</span>) || (<span>$screenshot_type</span> == <span>'image/png'</span>))
          && (<span>$screenshot_size</span> > <span>0</span>) && (<span>$screenshot_size</span> <= GW_MAXFILESIZE)) {
          <span>if</span> (<span>$_FILES</span>[<span>'screenshot'</span>][<span>'error'</span>] == <span>0</span>) {
            <span>// Move the file to the target upload folder</span><span>$target</span> = GW_UPLOADPATH . <span>$screenshot</span>;
            <span>if</span> (move_uploaded_file(<span>$_FILES</span>[<span>'screenshot'</span>][<span>'tmp_name'</span>], <span>$target</span>)) {
              <span>// Write the data to the database</span><span>$query</span> = <span>"INSERT INTO guitarwars (date, name, score, screenshot) VALUES (NOW(), '$name', '$score', '$screenshot')"</span>;
              mysqli_query(<span>$dbc</span>, <span>$query</span>);

              <span>// Confirm success with the user</span><span>echo</span><span>'<p>Thanks for adding your new high score! It will be reviewed and added to the high score list as soon as possible.</p>'</span>;
              <span>echo</span><span>'<p><strong>Name:</strong> '</span> . <span>$name</span> . <span>'<br />'</span>;
              <span>echo</span><span>'<strong>Score:</strong> '</span> . <span>$score</span> . <span>'<br />'</span>;
              <span>echo</span><span>'<img src="'</span> . GW_UPLOADPATH . <span>$screenshot</span> . <span>'" alt="PHP绘图函数生成图片验证码" /></p>'</span>;
              <span>echo</span><span>'<p><a href="index.php">&lt;&lt; Back to high scores</a></p>'</span>;

              <span>// Clear the score data to clear the form</span><span>$name</span> = <span>""</span>;
              <span>$score</span> = <span>""</span>;
              <span>$screenshot</span> = <span>""</span>;

              mysqli_close(<span>$dbc</span>);
            }
            <span>else</span> {
              <span>echo</span><span>'<p>Sorry, there was a problem uploading your screen shot image.</p>'</span>;
            }
          }
        }
        <span>else</span> {
          <span>echo</span><span>'<p>The screen shot must be a GIF, JPEG, or PNG image file no greater than '</span> . (GW_MAXFILESIZE / <span>1024</span>) . <span>' KB in size.</p>'</span>;
        }

        <span>// Try to delete the temporary screen shot image file</span>
        @unlink(<span>$_FILES</span>[<span>'screenshot'</span>][<span>'tmp_name'</span>]);
      }
      <span>else</span> {
        <span>echo</span><span>'<p>Please enter all of the information to add your high score.</p>'</span>;
      }
    }
    <span>else</span> {
      <span>echo</span><span>'<p>Please enter the verification pass-phrase exactly as shown.</p>'</span>;
    }
  }
<span>?></span></span><span><<span>hr</span> /></span><span><<span>form</span><span>enctype</span>=<span>"multipart/form-data"</span><span>method</span>=<span>"post"</span><span>action</span>=<span>"<?php echo $_SERVER['PHP_SELF']; ?>"</span>><span><<span>input</span><span>type</span>=<span>"hidden"</span><span>name</span>=<span>"MAX_FILE_SIZE"</span><span>value</span>=<span>"<?php echo GW_MAXFILESIZE; ?>"</span> /><span><<span>label</span><span>for</span>=<span>"name"</span>></span>Name:<span></<span>label</span>></span><span><<span>input</span><span>type</span>=<span>"text"</span><span>id</span>=<span>"name"</span><span>name</span>=<span>"name"</span><span>value</span>=<span>"<?php if (!empty($name)) echo $name; ?>"</span> /><span><<span>br</span> /></span><span><<span>label</span><span>for</span>=<span>"score"</span>></span>Score:<span></<span>label</span>></span><span><<span>input</span><span>type</span>=<span>"text"</span><span>id</span>=<span>"score"</span><span>name</span>=<span>"score"</span><span>value</span>=<span>"<?php if (!empty($score)) echo $score; ?>"</span> /><span><<span>br</span> /></span><span><<span>label</span><span>for</span>=<span>"screenshot"</span>></span>Screen shot:<span></<span>label</span>></span><span><<span>input</span><span>type</span>=<span>"file"</span><span>id</span>=<span>"screenshot"</span><span>name</span>=<span>"screenshot"</span> /></span><span><<span>br</span> /></span><span><<span>label</span><span>for</span>=<span>"verify"</span>></span>Verification:<span></<span>label</span>></span><span><<span>input</span><span>type</span>=<span>"text"</span><span>id</span>=<span>"verify"</span><span>name</span>=<span>"verify"</span><span>value</span>=<span>"Enter the pass-phrase."</span> /></span><span><<span>img</span><span>src</span>=<span>"captcha.php"</span><span>alt</span>=<span>"Verification pass-phrase"</span> /></span><span><<span>hr</span> /></span><span><<span>input</span><span>type</span>=<span>"submit"</span><span>value</span>=<span>"Add"</span><span>name</span>=<span>"submit"</span> /></span><span></<span>form</span>></span><span></<span>body</span>></span><span></<span>html</span>></span></code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template