It's my first time posting here as it's also the first time I've decided to build a site from scratch for myself. I'm creating a contact page with a basic captcha script. For reasons you might think are crazy, I want to display the captcha as text. I decided against png as the (anti)aliasing was pretty bad with a transparent background and I don't think my site is going to cause too much unwanted attention yet.
Here is my captcha.php
<?php
session_start();
$characters_on_image = 4;
$possible_letters = '0123456789';
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
echo $code;
header('Content-Type: text/plain');
$_SESSION['code'] = $code;
?>
The problem I'm having is that if I use <?php print $_SESSION['code']; ?>
it will print the previous code. My programming is pretty ropey and I was wondering if there is a way to show the current generated code in a <label>
tag?
You could also output Unicode characters that look like regular ones (but aren't) and ask users to repeat them. The Cyrillic alphabet has many characters that look familiar to Latin ones, but are encoded differently:
А vs A В vs B Е vs E Ѕ vs S І vs I К vs K
М vs M Н vs H О vs O Р vs P Т vs T Х vs X
This would be pretty easy to crack, but easy to implement and easy for users to solve.
(With respect to your code sample: skip the whole session thing and just echo the CAPTCHA from your captcha.php file. Make sure to include a hash of the solution.)
Instead of plain text consider using FIGlet fonts:
Font: big
__ __
| \/ |
| \ / | ___ ___
| |\/| |/ _ \ / _ \
| | | | (_) | (_) |
|_| |_|\___/ \___/
Here is a PHP library to do it
http://bolknote.ru/files/figlet/
Unfortunately page is in Russian so here is Google translated one (note that translator has broken layout of code examples so you may need to go back to original for those):