2012-08-06 134 views
1

我为人们运行PTC网站广告网站。而且我需要轻松降低或完全防止漫游器的使用。单个数字验证码匹配,PHP

在广告计数器完成倒计时之后,我想要加载验证码。
但我不想验证码,你必须输入任何东西。只需点击一下鼠标。

如果验证码可以在javascript中完成,那将非常棒。

我要找的那种是这样的:

计数器:0选择匹配数:7 [1] [0] [7] [2]

用户必须点击与数字匹配的框中的数字。

位数将在1-9之内。

+2

是的,我认为那也会很棒。祝你好运。 – lafor 2012-08-06 22:36:34

+0

我试过四处寻找我想要的验证码,但只有我能找到的源代码是您必须在代码中输入的类型..这不是我正在寻找的...我只是想一个简单的方法即使只是匹配的图像名称可能就足够了? – 2012-08-06 22:39:32

+0

另一件需要考虑的事项:验证码有4个数字,范围为'0-9'。一个是正​​确的。这只给出了不同的选择。如果一个机器人尝试了第一个表单上的第一个表单(假设表单只允许一次尝试),他们只需要四次尝试第一个数字...... – 2012-08-06 23:05:41

回答

0

缺少一个完整的captcha你不会停止确定的机器人。

一些想法。

10分钟内允许10-15个请求,如果超过了,请求验证码或完全阻止。确保这是基于IP的,因为cookies/session不会工作

向窗体中添加一些javascript,以便在没有它的情况下发布它。大多数收集垃圾邮件机器人的电子邮件不会运行JavaScript并放弃。生成一个随机字符串,在页面请求中存储$ _SESSION。使用javascript添加到表单文章。如果该字符串不存在匹配,则在表单上显示一个验证码。

或者使用javascript来建立窗体本身。

确定的刮板可以解决大多数问题,但您只是想增加这样做的成本。

0

我制作了CAPTCHA脚本。它的大小,字符数量以及可供选择的字符均可自定义。

这里是captcha.php

<?php 
function generateCaptcha($num_digits, $challenge_num, $size, $chars, $incode = false) { 
    if(session_id() == '') session_start(); 
    if(isset($_SESSION['captcha'])) unset($_SESSION['captcha']); 
    // You *must* type your own random salt here, *DO NOT* use this one. 
    $salt = 'H#(*h3^[email protected](*E%h$W*WK#vMIv)%(D*(A&*[email protected]^[email protected]*I%u8tgsc#yejdi$d8dee'; 
    $message = ''; 
    if(isset($_POST['hash']) && isset($_POST['code'])) { 
     if(!empty($_POST['hash']) && !empty($_POST['code']) && !empty($_POST['correct_index'])) { 
      if(md5($_POST['hash'] . $_POST['code'] . $salt) == $_POST['correct_index']) { 
       $message = '<p>Correct!</p>'; 
      } else { 
       $message = 'Incorrect code. Please try again.'; 
      } 
     } 
    } 
    $code = ''; 
    if($incode == false) { 
     for($i = 0; $i < $num_digits; $i++) { 
      $digit = substr($chars, floor(mt_rand(0, strlen($chars) - 1)), 1); 
      while(strpos($code, "$digit") !== false) { 
       $digit = substr($chars, floor(mt_rand(0, strlen($chars) - 1)), 1); 
      } 
      $code .= $digit; 
     } 
    } else { 
     for($i = 0; $i < $num_digits; $i++) { 
      $digit = substr($incode, floor(mt_rand(0, strlen($incode) - 1)), 1); 
      while(strpos($code, "$digit") !== false) { 
       $digit = substr($incode, floor(mt_rand(0, strlen($incode) - 1)), 1); 
      } 
      $code .= $digit; 
     } 
    } 
    $parts = str_split($code); 
    $width = $num_digits * $size; 
    $height = $size * 2; 
    $image = imagecreatetruecolor($width, $height); 
    $background = imagecolorallocate($image, floor(mt_rand(96, 255)), floor(mt_rand(96, 255)), floor(mt_rand(96, 255))); 
    imagefilledrectangle($image, 0, 0, $width, $height, $background); 
    $num_spots = floor(mt_rand($size * 2, $size * 15)); 
    for($i = 0; $i < $num_spots; $i++) { 
     $color = imagecolorallocate($image, floor(mt_rand(30, 255)), floor(mt_rand(30, 255)), floor(mt_rand(30, 255))); 
     $x = floor(mt_rand(0, $width)); 
     $y = floor(mt_rand(0, $height)); 
     $ellipse_width = floor(mt_rand(0, $size/2)); 
     $ellipse_height = floor(mt_rand(0, $size/2)); 
     imagefilledellipse($image, $x, $y, $ellipse_width, $ellipse_height, $color); 
     $x1 = floor(mt_rand(0, $width)); 
     $y1 = floor(mt_rand(0, $height)); 
     $x2 = floor(mt_rand(0, $width)); 
     $y2 = floor(mt_rand(0, $height)); 
     imageline($image, $x1, $y1, $x2, $y2, $color); 
    } 
    $num_dots = floor(mt_rand($size * 50, $size * 80)); 
    for($i = 0; $i < $num_dots; $i++) { 
     $color = imagecolorallocate($image, floor(mt_rand(30, 255)), floor(mt_rand(30, 255)), floor(mt_rand(30, 255))); 
     $x = floor(mt_rand(0, $width)); 
     $y = floor(mt_rand(0, $height)); 
     imagesetpixel($image, $x, $y, $color); 
    } 
    for($i = 0; $i < count($parts); $i++) { 
     $color = imagecolorallocate($image, floor(mt_rand(0, 150)), floor(mt_rand(0, 150)), floor(mt_rand(0, 150))); 
     $x = floor(mt_rand($size * 0.9, $size * 1.1)); 
     $y = floor(mt_rand($size, $size * 2)); 
     imagettftext($image, $size, floor(mt_rand(-10, 10)), $i * $x, $y, $color, 'Justus-Bold.ttf', $parts[$i]); 
     imagettftext($image, $size, floor(mt_rand(-10, 10)), $i * $x + floor(mt_rand(1, 7)), $y, $color, 'Justus-Bold.ttf', $parts[$i]); 
     imagettftext($image, $size, floor(mt_rand(-10, 10)), $i * $x + floor(mt_rand(1, 7)), $y, $color, 'Justus-Bold.ttf', $parts[$i]); 
     imagettftext($image, $size, floor(mt_rand(-10, 10)), $i * $x + floor(mt_rand(1, 7)), $y, $color, 'Justus-Bold.ttf', $parts[$i]); 
    } 
    $white = imagecolorallocate($image, 255, 255, 255); 
    $filename = md5(time() . $_SERVER['REMOTE_ADDR'] . mt_rand(0, 1000)) . '.png'; 
    imagepng($image, $filename); 
    imagedestroy($image); 
    $file = file_get_contents($filename); 
    $imgsize = getimagesize($filename); 
    unlink($filename); 
    $captcha = 'data:' . $imgsize['mime'] . ';base64,' . base64_encode($file); 
    $challenge = array('captcha' => '', 'code' => null, 'size' => 0, 'digits' => 0); 
    if($incode == false) { 
     $challenge = generateCaptcha($challenge_num, 0, $size, $chars, $code); 
    } 
    $hash = md5($challenge['code'] . $salt); 
    $correct_index = array(); 
    for($i = 0; $i < strlen($challenge['code']); $i++) { 
     $correct_index[] = strpos($code, substr($challenge['code'], $i, 1)); 
    } 
    $result = array(
     'captcha' => $captcha, 
     'challenge' => array($challenge['captcha'], $challenge['size'], $challenge['digits']), 
     'size' => array($imgsize[0], $imgsize[1]), 
     'hash' => $hash, 
     'code' => $code, 
     'message' => $message, 
     'width' => $size, 
     'digits' => $num_digits, 
     'correct_index' => md5($hash . implode('', $correct_index) . $salt) 
    ); 
    return $result; 
} 
?> 

......这里是captcha.html

<!DOCTYPE HTML> 
<!-- 
<?php 
include 'captcha.php'; 
$captcha = generateCaptcha(4, 2, 100, ''); 
?> 
--> 
<html lang="en-US"> 
<head> 
    <meta charset="UTF-8" /> 
    <title>Click-captcha test</title> 
    <style type="text/css"> 
#challenge, #captcha-img { 
    margin: 10px 
} 
#captcha-img { 
    overflow: hidden 
} 
.captcha-digit { 
    display: block; 
    float: left; 
    width: <?php echo $captcha['width']; ?>px; 
    height: 100%; 
    cursor: pointer 
} 
.captcha-digit-selected { 
    background: #ccc; 
    opacity: .75; 
    filter: progid:DXImageTransform.Microsoft.Gradient(StartColorStr='#f2cccccc', EndColorStr='#f2cccccc') 
} 
    </style> 
    <script type="text/javascript"> 
var captchaLinks = []; 
var digits = []; 
var num_digits = <?php echo $captcha['challenge'][2]; ?> - 1; 
addEvent(window, 'load', init); 
function init() { 
    captchaLinks = ['<?php 
$digits = array(); 
for($i = 'digit0'; $i < 'digit' . $captcha['digits']; $i++) { 
    $digits[] = $i; 
} 
echo implode("', '", $digits); 
?>']; 
    for(var i = 0; i < captchaLinks.length; i++) { 
    //for(var link in captchaLinks) { 
     addEvent(document.getElementById(captchaLinks[i]), 'click', newCaptchaDigit); 
    } 
} 
function newCaptchaDigit(e) { 
     if(e.target.className == 'captcha-digit captcha-digit-selected') { 
      digits.splice(digits.indexOf(e.target.id.substr(-1, 1)), 1); 
      e.target.className = 'captcha-digit'; 
     } else if(digits.length <= num_digits) { 
      digits.splice(num_digits, digits.length - num_digits, e.target.id.substr(-1, 1)); 
      e.target.className = 'captcha-digit captcha-digit-selected'; 
     } 
     document.getElementById('code').value = digits.join(''); 
} 
function addEvent(elem, event, handler) { 
    if(elem !== null & typeof elem !== 'undefined') { 
     if(elem.addEventListener) { 
      elem.addEventListener(event, handler, false); 
     } else if(elem.attachEvent) { 
      elem.attachEvent('on' + event, handler); 
     } else if(elem['on' + event]) { 
      elem['on' + event] = handler; 
     } 
    } 
} 
    </script> 
</head> 
<body> 
    <div> 
     <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post"> 
      <div id="captcha"> 
       <div><?php echo $captcha['message']; ?></div> 
       <div>Click the following number sequence: 
        <div id="challenge" style="width: <?php echo $captcha['challenge'][1][0]; ?>px; height: <?php echo $captcha['challenge'][1][1]; ?>px; background-image: url('<?php echo $captcha['challenge'][0]; ?>')"></div> 
       </div> 
       <div id="captcha-img" style="width: <?php echo $captcha['size'][0]; ?>px; height: <?php echo $captcha['size'][1]; ?>px; background-image: url('<?php echo $captcha['captcha']; ?>')"> 
<?php for($i = 'digit0'; $i < 'digit' . $captcha['digits']; $i++) { ?> 
        <a class="captcha-digit" id="<?php echo $i; ?>"></a> 
<?php } ?> 
       </div> 
      </div> 
      <input type="hidden" name="hash" value="<?php echo $captcha['hash']; ?>" /> 
      <input type="hidden" name="correct_index" value="<?php echo $captcha['correct_index']; ?>" /> 
      <input type="hidden" name="code" id="code" value="" /> 
      <input type="submit" value="Submit" /> 
     </form> 
    </div> 
</body> 
</html> 

希望你会看到是怎么回事,但我会解释这里。 :-)

该函数被称为generateCaptcha,它接受参数$num_digits, $challenge_num, $size, $chars, $incode = false


  • $num_digits:把字符数在CAPTCHA
  • $challenge_num:字符数装进挑战
  • $size:的CAPTCHA
  • $chars尺寸:包括哪些字符(例如对于数字:''
  • $incode:这只是如此脚本c告诉它是否被自己调用来产生挑战。 请勿设置。

所以创建一个CAPTCHA图像有4个字符,一个1个字符的挑战(如您的问题),大小30,只有用数字,用这个代码:

<?php 
include 'captcha.php'; 
$captcha = generateCaptcha(4, 1, 30, ''); 
?> 

然后变量$captcha将结束这样的事情:

array(9) { 
    ["captcha"]=> 
    string(118058) "data:image/png;base64,iVBORw0KG...kSuQmCC" 
    ["challenge"]=> 
    array(3) { 
    [0]=> 
    string(76266) "data:image/png;base64,iVBORw0KG...kJggg==" 
    [1]=> 
    array(2) { 
     [0]=> 
     int(200) 
     [1]=> 
     int(200) 
    } 
    [2]=> 
    int(2) 
    } 
    ["size"]=> 
    array(2) { 
    [0]=> 
    int(400) 
    [1]=> 
    int(200) 
    } 
    ["hash"]=> 
    string(32) "81bc501400b8da366e70b26007cb2323" 
    ["code"]=> 
    string(4) "4817" 
    ["message"]=> 
    string(0) "" 
    ["width"]=> 
    int(100) 
    ["digits"]=> 
    int(4) 
    ["correct_index"]=> 
    string(32) "17ae615be69c757505dc7f69fce2afb1" 
} 

如果您需要更多信息,请在评论中提问。

+0

我刚刚注意到它曾经工作正常,但现在有一半时间它不'无法识别何时提交了验证码。我将不得不考虑...... – 2012-08-09 10:16:41

+0

另一个注释:**(1):**我找到了一种无需使用会话的方法,因此您可以从脚本顶部删除所有会话代码。当我修复之前提到的错误时,我会在编辑中删除它。 **(2):** *请记住按照代码中的注释创建您自己的盐。*这是为了安全。 – 2012-08-09 10:24:32