它山之石可以攻玉

键盘上的生活
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

开源 PHP 验证码 Securimage 带语音识别

Posted on 2012-11-05 15:48  陈达辉  阅读(772)  评论(0编辑  收藏  举报

开源 PHP 验证码 Securimage 带语音识别 2cb6b96a412e17b0

官方网站:http://www.phpcaptcha.org/
下载地址:http://www.phpcaptcha.org/download/
原文地址:http://www.21andy.com/blog/20100417/1886.html

Securimage 使用方法:
开源 PHP 验证码 Securimage 带语音识别 790b45d0c7c0b69b

验证码图片:

<img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image"  />

验证码文本框:

<input type="text" name="captcha_code" size="10" maxlength="6" />

看不清验证码:

<a href="#" rel="nofollow" target="_blank">Reload Image</a>

原文地址:http://www.21andy.com/blog/20100417/1886.html

后端PHP验证:

<?php
session_start();
 
include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';
$securimage = new Securimage();
 
if ($securimage->check($_POST['captcha_code']) == false) {
 
// the code was incorrect
 
// handle the error accordingly with your other error checking
 
 
// or you can do something really basic like this
 
die('The code you entered was incorrect.  Go back and try again.');
}

Securimage 类属性:

$img->image_width = 275;
$securimage->image_height = 90;
$securimage->perturbation = 0.9; // 1.0 = high distortion, higher numbers = more distortion
$securimage->image_bg_color = new Securimage_Color("#0099CC");
$securimage->text_color = new Securimage_Color("#EAEAEA");
$securimage->text_transparency_percentage = 65; // 100 = completely transparent
$securimage->num_lines = 8;
$securimage->line_color = new Securimage_Color("#0000CC");
$securimage->signature_color = new Securimage_Color(rand(0, 64), rand(64, 128), rand(128, 255));
$securimage->image_type = SI_IMAGE_PNG;
$securimage->show('backgrounds/bg5.jpg')

Securimage AJAX 完整示例

AJAX 前端HTML:

<html>
<head>
 
<title>PHP Captcha with Ajax</title>
 
<script type="text/javascript" src="prototype.js"></script>
 
 
<script type="text/javascript">
 
<!--
 
  function processForm()
  {
    $('submit').disabled = true;
    $('submit').value = "Processing.  Please Wait...";
 
    $('contact_form').request({
      onSuccess: function(transport)
      {
        if(transport.responseText.match(/^OK/) != null) {
          alert('Your message has been sent!');
          $('contact_form').reset();
        } else {
          alert(transport.responseText);
        }
 
        $('submit').value = 'Send Message';
        $('submit').disabled = false;
      }
    });
 
    return false;
  }
 
  -->

 
</script>
</head>
 
<body>
 
<form id="contact_form" action="process.php" method="post" onsubmit="return processForm()">
 
<div style="float: left; width: 100px">Your name:</div>
 
<div style="float: left"><input type="text" name="sender_name" size="20" /></div>
 
<div style="clear: both"></div>
 
 
<div style="float: left; width: 100px">Email:</div>
 
<div style="float: left"><input type="text" name="sender_email" size="30" /></div>
 
<div style="clear: both"></div>
 
 
<div style="float: left; width: 100px">Message:</div>
 
<div style="float: left"><textarea name="message" rows="4" cols="30"></textarea></div>
 
<div style="clear: both"></div>
 
 
<div style="float: left; width: 100px">Security Image:</div>
 
<div style="float: left"><img src="securimage/securimage_show.php" alt="CAPTCHA Image" /></div>
 
<div style="clear: both"></div>
 
 
<div style="float: left; width: 100px">Security Code:</div>
 
<div style="float: left"><input type="text" name="code" size="8" /></div>
 
<div style="clear: both"></div>
 
 
<div style="float: left; width: 100px">&nbsp;</div>
 
<div style="float: left"><input id="submit" type="submit" value="Send Message" /></div>
 
<div style="clear: both"></div>
</form>
 
 
</body>
</html>

AJAX 后端PHP:

下载: process.php
<?php
 
/**
 * Securimage with AJAX
 *
 * Author: Drew Phillips (www.phpcaptcha.org)
 *
 * This code is released to the public domain.
 *
 */

 
$your_email = 'you@example.com'// Email to send message to
 
if ($_SERVER['REQUEST_METHOD'] != 'POST') exit; // Quit if it is not a form post
 
// quick way clean up incoming fields
foreach($_POST as $key => $value) $_POST[$key] = urldecode(trim($value));
 
// get form data into shorter variables
// each $_POST variable is named based on the form field's id value
$name    = $_POST['sender_name'];
$email   = $_POST['sender_email'];
$message = $_POST['message'];
$code    = $_POST['code'];
 
$errors  = array(); // array of errors
 
// basic validation
if ($name == '') {
 
$errors[] = "Please enter your name";
}
 
if ($email == '') {
 
$errors[] = "Please enter your email address";
} else if (strpos($email, '@') === false) {
 
$errors[] = "Please enter a valid email address";
}
 
if ($message == '') {
 
$errors[] = "Please enter a message to send";
}
 
 
if (sizeof($errors) == 0) {
 
// only check the code if there are no other errors
 
require_once 'securimage/securimage.php';
 
$img = new Securimage;
 
if ($img->check($code) == false) {
    
$errors[] = "Incorrect security code entered";
 
} // if the code checked is correct, it is destroyed to prevent re-use
}
 
if (sizeof($errors) > 0) {
 
// if errors, send the error message
 
$str = implode("\n", $errors);
 
die("There was an error with your submission!  Please correct the following:\n\n" . $str);
}
 
$time = date('r');
$body = <<<EOD
Hi!
 
A message was sent to you from
$name on $time.
 
Here is their message:
 
$message
EOD;
 
// send email
mail($your_email, "Contact Form Sent", $body, "From: $your_email\r\nReply-To: $email\r\nContent-Type: text/plain; charset=ISO-8859-1\r\nMIME-Version: 1.0");
 
die('OK'); // send success indicator
 
?>

更详细的文档请看官方网站