PHP create your own custom captcha easily
Create you own custom captcha easily in PHP, before coding I want to explain what is Captcha and how it works?
Captcha is used to distinguish human from given input, and it is ensured that form is submitted by human. Generally computers or bot are not capable of solving a Captcha.
Bots are kind of software which has database of commonly used password or they can also work on sequences of different patterns. lets says our password is 599, Bot starts submitting your form with password=0 next time with password=1 and so on upto lets say 1000. when it reaches 599 it will get login and will send an email to hacker that on abc url, username=mno and password=599 worked.
these attacks are also called brute-force attacks. the only solution to distinguish between human and bot is Captcha.
Why captcha is hard to read
Captcha is actually an image with random characters, its fonts are intentionally made irregular and its alignment is messed up to avoid computers from reading, because some software are there which can read text from image but they work on visual algorithms and need some regular fonts in regular pattern and alignment.
How Custom captcha is built and how it works
Custom Captcha Creation
- Create new php file eg: captcha.php
- First we start session at top of page
- We generate random word
- Create image with above word in a complex font
- We can optionally put some background dots and lines to make it more complex visually.
- put this word in $_SESSION[‘alam_captcha’] variable
- Do not write other code in this file.
Custom Captch Using
- At form we create an img tag with src=”captcha.php”
- And a textbox with name=”captcha” where user write captcha string
- On each page refresh a different captcha will be displayed and that string will also be saved in session
- At verification will will be matching session and input
Custom Verifying Captcha
- At form post first of all we check if $_SESSION[‘alam_captcha’] is equal to $_POST[‘captcha’]
- if matched we proceed our normal login procedure
- if captcha is not matched we stop the execution and display an error.
here is the code to generate captcha image dynamically with PHP for this code we need a font file I have used alam_font.ttf you can use any other font, I have used bluesh color you can use different color matching your website theme. in short you can customize every thing.
captcha.php code
index.php has code to display captcha in form and its verification code on form submission. '; '; ';
Index.php code
Logged in sussessfully.
//set session to keep logged-in user data
//redirect
}else{
echo 'Invalid username and password.
}//end if user&pwd match
}else{
echo 'Wrong Captcha entered.
}//end if captcha=session
}//end if post
?>
I am not able to upload the font file here, you can download ttf file along with both of these two files from the following gihub link
Hope this will help you creating your own Custom captcha, its development and implementation is easy. comment if you have issues or if you can improve it.








Visit Today : 179
Total Visit : 932257
Total Hits : 3547724677
Comments
Thanks,Nice work, i will surely use it in my university project
Nice work keep it up for sharing such informative knowledge.
Magnifico, muchas gracias. Pura Vida