2013年2月6日 星期三

phpcapcha 使用方法

* phpcapcha 會順便檢查表單欄位值。但這功能不一定用的到,因為表單內容的檢查我習慣會在程式的其它部份做。所以本篇把圖片驗證之外的程式碼都拿掉。(其實 phpcapcha 的範例也可以只做圖片驗證,把 DEBUG_MODE 設成 1 就可以了。)

<?php
session_start(); 
process_si_contact_form(); // Process the form, if it was submitted
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Securimage Example Form</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING'] ?>" id="contact_form">
 <input type="hidden" name="do_phpcapcha" value="phpcapcha" />
    <img id="siimage" style="border: 1px solid #000; margin-right: 15px" src="./securimage_show.php?sid=<?php echo md5(uniqid()) ?>" alt="CAPTCHA Image" align="left" />
    <object type="application/x-shockwave-flash" data="./securimage_play.swf?bgcol=#ffffff&amp;icon_file=./images/audio_icon.png&amp;audio_file=./securimage_play.php" height="32" width="32">
    <param name="movie" value="./securimage_play.swf?bgcol=#ffffff&amp;icon_file=./images/audio_icon.png&amp;audio_file=./securimage_play.php" />
    </object>&nbsp;
    <a tabindex="-1" style="border-style: none;" href="#" title="Refresh Image" onclick="document.getElementById('siimage').src = './securimage_show.php?sid=' + Math.random(); this.blur(); return false">
    <img src="./images/refresh.png" alt="Reload Image" height="32" width="32" onclick="this.blur()" align="bottom" border="0" /></a><br />
    <strong>Enter Code*:</strong><br />
     <?php echo @$_SESSION['ctform']['captcha_error'] ?>
    <input type="text" name="ct_captcha" size="12" maxlength="8" /><br />
    <input type="submit" value="Submit Message" />
</form>
</body>
</html>
<?php
// The form processor PHP code
function process_si_contact_form(){
 $_SESSION['ctform'] = array(); // re-initialize the form session data
 if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do_phpcapcha'] == 'phpcapcha') {
  $_POST['ct_captcha'] = htmlspecialchars(stripslashes(trim($_POST['ct_captcha'])));
  $captcha = @$_POST['ct_captcha']; // the user's entry for the captcha code
  require_once dirname(__FILE__) . '/securimage.php';
  $securimage = new Securimage();
  if ($securimage->check($captcha) == false) {
   $error = 'Incorrect security code entered<br />';
  }
  if(empty($error)) {
   $_SESSION['ctform']['error'] = false;  // no error with form
   $_SESSION['ctform']['success'] = true; // message sent
  }
  else {
   $_SESSION['ctform']['captcha_error'] = "<span style=\"font-weight: bold; color: #f00\">$error</span>";
   $_SESSION['ctform']['error'] = true; // set error floag
  } 
 } // POST
}
$_SESSION['ctform']['success'] = false; // clear success value after running
?>
上面的程式碼只需要修改 phpcapcha 的路徑:
./images/xxx.png
./securimage_show.php
./securimage_play.swf
./securimage_play.php

沒有留言:

張貼留言