douwei1174 2011-05-18 08:00
浏览 147
已采纳

PHP的数字Captcha

Is there a numeric captcha available for PHP?

(which doesn't rely on JavaScript being turned on)


EDIT:

  • I know there are JS-independent captchas out there.
  • I know there are PHP captchas out there.
  • I know there are numeric captchas out there.

But I'm looking for a PHP numeric Javascript-independent captcha.
The only numeric captcha's I've found are either for ASP.NET or jQuery/JS based ones.
I don't want any of those as an answer to the question.

And I'm not taking about a small website here. In the answer I'd like to know whether your suggestion puts a lot of strain on the server or not.

  • 写回答

6条回答 默认 最新

  • dousi2251 2011-05-18 08:33
    关注

    I guess it can't be avoided to deal with rendering an image when dealing with captchas? Here's a simple one (and may not be the most elegant):

    sample output

    session_start();
    
    $strings = '123456789';
    $i = 0;
    $characters = 6;
    $code = '';
    while ($i < $characters)
    { 
        $code .= substr($strings, mt_rand(0, strlen($strings)-1), 1);
        $i++;
    } 
    
    $_SESSION['captcha'] = $code;
    
    //generate image
    $im = imagecreatetruecolor(124, 40);
    $foreground = imagecolorallocate($im, 0, 0, 0);
    $shadow = imagecolorallocate($im, 173, 172, 168);
    $background = imagecolorallocate($im, 255, 255, 255);
    
    imagefilledrectangle($im, 0, 0, 200, 200, $background);
    
    // use your own font!
    $font = 'monofont.ttf';
    
    //draw text:
    imagettftext($im, 35, 0, 9, 28, $shadow, $font, $code);
    imagettftext($im, 35, 0, 2, 32, $foreground, $font, $code);     
    
    // prevent client side  caching
    header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    
    //send image to browser
    header ("Content-type: image/png");
    imagepng($im);
    imagedestroy($im);
    

    Display it in a form:

    <img src="captcha.php">
    Enter the code above: <input type="text" name="captcha">
    

    Once submitted, check the entered code:

    if ($_POST['captcha'] == $_SESSION['captcha'])
        // do your thing
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染