How to create captcha in PHP using GD library Today I will tell you about very important thing – captcha protection. Usually this is very important to prevent automated sign-ups in your registration forms, comment spam in blogs and guestbooks, or another brute force attacks. I will give sample how to draw protection image using our hands.
如何使用GD库在PHP中创建验证码今天,我将向您介绍非常重要的事情-验证码保护。 通常,这对于防止自动注册表格,在博客和留言簿中评论垃圾邮件或其他暴力攻击非常重要。 我将举例说明如何用我们的双手绘制保护图像。
Here are samples and downloadable package:
以下是示例和可下载的软件包:
[sociallocker]
[社交储物柜]
[/sociallocker]
[/ sociallocker]
Ok, download the example files and lets start coding !
好的,下载示例文件并开始编码!
As usual, we start with the HTML.
和往常一样,我们从HTML开始。
This is our main page code.
这是我们的主页代码。
Captcha generation using GD, using MD5 to encrypt security text
Security image:
Verification (Type what you see):
Current sample draw security image using GD library, each time when page loads it show different symbol-digit combinations. This combination processing through MD5 encription and storing in cookies. So you will able to check cookies after (where it necessary). In my sample I checking it using javascript. Try it!
Captcha generation using GD, using MD5 to encrypt security text
Security image:
Verification (Type what you see):
Current sample draw security image using GD library, each time when page loads it show different symbol-digit combinations. This combination processing through MD5 encription and storing in cookies. So you will able to check cookies after (where it necessary). In my sample I checking it using javascript. Try it!
To display captcha (security image) we will just draw image with php-source: captcha.php. It will generate image for us. Generated text will processed via MD5 and stored in cookies to following recognition and checking.
为了显示验证码(安全图像),我们将使用php-source:captcha.php绘制图像。 它将为我们生成图像。 生成的文本将通过MD5处理,并存储在Cookie中,以供后续识别和检查。
Here are used CSS styles.
这是使用CSS样式。
body{background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0}
.captcha_example{background:#FFF;width:865px;font-size:80%;border:1px #000 solid;margin:0.5em 10% 0.5em;padding:1em 2em 2em;-moz-border-radius: 3px;-webkit-border-radius: 3px}
.lines{margin:10px 0}
body{background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0}
.captcha_example{background:#FFF;width:865px;font-size:80%;border:1px #000 solid;margin:0.5em 10% 0.5em;padding:1em 2em 2em;-moz-border-radius: 3px;-webkit-border-radius: 3px}
.lines{margin:10px 0}
Here are necessary JS files to our project.
这是我们项目的必要JS文件。
function getCookie(c_name) {
if (document.cookie.length>0) {
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1) {
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
function checkCaptcha() {
// check captcha
var sInsCaptcha = calcMD5($('.captcha').val());
var sCookieCaptcha = getCookie('strSec');
if (sInsCaptcha == sCookieCaptcha) {
alert('Wow, great, you enter correct captcha');
} else {
alert('Sorry, but wrong');
}
}
function getCookie(c_name) {
if (document.cookie.length>0) {
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1) {
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
function checkCaptcha() {
// check captcha
var sInsCaptcha = calcMD5($('.captcha').val());
var sCookieCaptcha = getCookie('strSec');
if (sInsCaptcha == sCookieCaptcha) {
alert('Wow, great, you enter correct captcha');
} else {
alert('Sorry, but wrong');
}
}
We will using getCookie function to get cookies information, and checkCaptcha to validate captcha.
我们将使用getCookie函数获取Cookie信息,并使用checkCaptcha验证验证码。
This is common files – jQuery library with addon for working with MD5. No need to give full code of that file here. It always available as a download package
这是常见的文件–带有用于MD5的插件的jQuery库。 无需在此处提供该文件的完整代码。 它始终可以作为下载包获得
Ok, here are all used PHP file:
好的,这都是用过PHP文件:
Here are several comments by code:
以下是一些代码注释:
In start we will define array of used characters – $chars. After, we will compose string (5 letters) with random characters. After, will MD5 this result string and save it into cookies. We will use this to more security. So any bot or special script will unable to find this key even after detail investigation of result page.
首先,我们将定义使用字符数组– $ chars。 之后,我们将用随机字符组成字符串(5个字母)。 之后,将MD5这个结果字符串并保存到cookie中。 我们将使用它来提高安全性。 因此,即使在对结果页进行详细调查后,任何漫游器或特殊脚本也无法找到该密钥。
produceCaptchaImage is main function which will draw our captcha. It take just 1 param – text which we going to draw. We will using GD library to draw. We will randomly choose position to text in captcha, angle (+- 5 degrees), font color. After will get our background image which will mage reading of our code more difficult, and, will draw result text on this image.
ProduceCaptchaImage是主要功能,将绘制我们的验证码。 仅需1个参数-我们将要绘制的文本。 我们将使用GD库进行绘制。 我们将随机选择验证码,角度(+/- 5度),字体颜色的文本位置。 之后将得到我们的背景图像,这将使我们的代码阅读更加困难,并将在该图像上绘制结果文本。
Today I told you how to create our own captcha – security image. I sure that you will use it in your projects. Quite any serious project using it to protect spamming. Good luck!
今天,我告诉您如何创建自己的验证码-安全映像。 我确定您将在项目中使用它。 任何使用它来保护垃圾邮件的严肃项目。 祝好运!
翻译自: https://www.script-tutorials.com/how-to-create-captcha-in-php-using-gd-library/