captcha 是現在防止機器人最常用的一種防止方式,而且現在很多都有提供class可以使用,本篇將實作cool-php-captcha和Captcha Numbers V2.1這二套captcha,並且使用的是Codeigniter framework。
.htaccess
將_images目錄排除rewrite
1 2 3 | RewriteEngine on RewriteCond $1 !^(index.php|_images|_css|_js|robots.txt) RewriteRule ^(.*)$ /index.php/$1 [L] |
cool-php-captcha
1. 下載 cool-php-captcha-0.3.zip
2. 解壓縮放置_images目錄下
3. 在view檔案裡面放置captcha images
1 | <img src="<?=base_url()?>_images/cool-php-captcha-0.3/captcha.php"> |
Captcha Numbers V2.1
1. 下載 captchanumbersv21-2006-03-07.zip
2. 解壓縮放置_images目錄下
3. 新增fonts目錄並將arial.ttf字型檔放入
4. 在view檔案裡面放置captcha images
1 | <img src="<?=base_url()?>_images/captchanumbersv21/example.php"> |
驗證 captcha
1 2 3 | if (strtolower(trim($_REQUEST['captcha'])) != $_SESSION['captcha']) { return "Invalid captcha"; } |
參考資料
- http://code.google.com/p/cool-php-captcha/
- http://www.phpclasses.org/package/2614-PHP-Generate-CAPTCHA-validation-images.html
- http://www.filestube.com/a/arial+ttf+download