在学做网站论坛之前的建站教程中,讲到如何制作网站注册/登录功能,用户通过注册页面可以快速注册会员名。但是这样的注册页面没有安全机制,容易被恶意批量注册,消耗服务器内存。
为了防止自己做网站时,自己的网站被别人恶意批量注册,可以在网站注册页面添加验证码,用户注册时,必须正确填写验证码才可以通过。效果如下图:


//验证码错误
if (!isset($_SESSION))
session_start();
session_regenerate_id(TRUE);
if(empty($_POST['captcha_code'])|| empty($_SESSION['ludou_lcr_secretword'])|| (trim(strtolower($_POST['captcha_code'])) != $_SESSION['ludou_lcr_secretword']) )
$error .= '<strong>错误</strong>:验证码不正确!<br />';
<p class="zc00">
<label for="user_email">验 证 码: <input id="CAPTCHA" class="input" type="text" tabindex="24" size="10" value="" name="captcha_code" /><img id="captcha_img" src="<?php bloginfo('template_url'); ?>/captcha/captcha.php" /><a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='<?php bloginfo('template_url'); ?>/captcha/captcha.php?'+Math.random();document.getElementById('CAPTCHA').focus();return false;" class="kbq">看不清</a>
</label>
</p>