cvs: peardoc /en/package/text/text-captcha example.xml

From: Date: Sun, 26 Nov 2006 17:50:52 +0000
Subject: cvs: peardoc /en/package/text/text-captcha example.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-8908@lists.php.net to get a copy of this message
davidc Sun Nov 26 17:50:52 2006 UTC Modified files: /peardoc/en/package/text/text-captcha example.xml Log: - Reverted my changes, sorry for that. http://cvs.php.net/viewvc.cgi/peardoc/en/package/text/text-captcha/example.xml?r1=1.2&r2=1.3&diff_format=u Index: peardoc/en/package/text/text-captcha/example.xml diff -u peardoc/en/package/text/text-captcha/example.xml:1.2 peardoc/en/package/text/text-captcha/example.xml:1.3 --- peardoc/en/package/text/text-captcha/example.xml:1.2 Sun Nov 26 17:24:20 2006 +++ peardoc/en/package/text/text-captcha/example.xml Sun Nov 26 17:50:52 2006 @@ -1,5 +1,5 @@ -<!-- $Id: example.xml,v 1.2 2006/11/26 17:24:20 davidc Exp $ --> -<sect2 id="package.text.text-captcha-numeral.example"> +<!-- $Revision: 1.3 $ --> +<sect2 id="package.text.text-captcha.example"> <title>Example</title> <para> @@ -8,71 +8,86 @@ solved correctly. </para> - <example id="package.text.text-captcha-numeral.example.create"> - <title>Creating a Numeral CAPTCHA</title> + <example id="package.text.text-captcha.example.create"> + <title>Creating a CAPTCHA</title> <para> - This example is just to show you how to generate a simple mathematic - operation with Text_CAPTCHA_Numeral. + The following code creates a CAPTCHA, provides the relevant + information for the package, anhd retrieves the CAPTCHA as a + PNG image. </para> <programlisting role="php"> -require_once 'Text/CAPTCHA/Numeral.php'; -$num = new Text_CAPTCHA_Numeral; -$operation = $num->getOperation(); - -/** - * This will print the mathematical operation - * that has been generated by the package. - */ -print $operation; +require_once 'Text/CAPTCHA.php'; + +// Set CAPTCHA options (font must exist!) +$options = array( + 'font_size' => 24, + 'font_path' => './', + 'font_file' => 'COUR.TTF' +); + +// Generate a new Text_CAPTCHA object, Image driver +$c = Text_CAPTCHA::factory('Image'); +$retval = $c->init(200, 80, null, $options); +if (PEAR::isError($retval)) { + echo 'Error generating CAPTCHA!'; + exit; +} + +// Get CAPTCHA secret passphrase +$_SESSION['phrase'] = $c->getPhrase(); + +// Get CAPTCHA image (as PNG) +$png = $c->getCAPTCHAAsPNG(); +if (PEAR::isError($png)) { + echo 'Error generating CAPTCHA!'; + exit; +} +file_put_contents(md5(session_id()) . '.png', $png); </programlisting> </example> - <example id="package.text.text-captcha-numeral.example.secure"> - <title>Securing a form with a Numeral CAPTCHA</title> + <example id="package.text.text-captcha.example.secure"> + <title>Securing a form with a CAPTCHA</title> <para> - This example will show you how to secure a form using the - numeral captcha. It takes generates an operation and store - it's result into a session variable + The following code implements the functionality to check whether + a CAPTCHA was solved correctly or not. for this, the + CAPTCHA's phrase is stored in a session variable to + retain this information between requests. It is + important to unset the session after solving the + CAPTCHA to avoid the reuse of the session ID. </para> <programlisting role="php"> <![CDATA[ -require_once 'Text/CAPTCHA/Numeral.php'; -$numcap = new Text_CAPTCHA_Numeral; - -if (isset($_POST['captcha']) && isset($_SESSION['answer'])) { - if ($_POST['captcha'] == $_SESSION['answer']) { - $errors[] = 'Ok.. YOu might be human..'; +session_start(); +$ok = false; +$msg = 'Please enter the text in the image in the field below!'; +if ($_SERVER['REQUEST_METHOD'] == 'POST') { + if (isset($_POST['phrase']) && isset($_SESSION['phrase']) && + strlen($_POST['phrase']) > 0 && strlen($_SESSION['phrase']) > 0 && + $_POST['phrase'] == $_SESSION['phrase']) { + $msg = 'OK!'; + $ok = true; + unset($_SESSION['phrase']); } else { - $errors[] = 'You are or not human or dumb'; + $msg = 'Please try again!'; } + unlink(md5(session_id()) . '.png'); } - if (!empty($errors)) { - foreach ($errors as $error) { - print "<h1><font color='red'>$error</font></h1><br />"; - } - } +print "<p>$msg</p>"; - - print ' - <form name="capter" action="index.php?page=liveExample" method="post"> - <table> - <tr> - <th>What is this result pilgrim?: '.$numcap->getOperation().'</th> - <td><input type="text" value="" name="captcha" /></td> - </tr> - <tr> - <th/> - <td><input type="submit" value="Let me prove you that I am human!" /></td> - </tr> - </form> - '; - $_SESSION['answer'] = $numcap->getAnswer(); +if (!$ok) { + // create the CAPTCHA as seen above + // and send it to the client +} ]]> </programlisting> + <para>See the file <filename>CAPTCHA_test.php</filename> + in the package distribution for a full, working + example (GD and TTF support required).</para> </example> </sect2>

« previous php.pear.doc (#8908) next »