But some of CAPTCHA image code, cannot stop a lot of spam, cause my comment form always get spam terror every time. So how about random code, then we register it into a session.
function generateWord($length)
{
$registerThis="";
srand ((float) microtime() * 10000000);
$rand1=rand(1,99); $rand2=rand(100,199); $rand3=rand(200,299); $rand4=rand(300,399); $rand5=rand(400,599);
$input = array ("V-!-q", "$rand4", "N-s-z", "the-clash", "i-Y-s", "brush", "pattern", "s-G-l", "gradien", "style", "b-f-D", "glossy", "render", "t-i-J", "$rand1", "$rand2", "H-*-R", "$rand3", "green-day", "danzig", "turtles-jr", "$rand5", "blur", "P-L-l", "motion", "dream", "weaver", "H-r-W", "photo", "w-x-U", "shop", "image", "ready", "flash", "web", "master", "ramones", "sex-pistols", "misfits", "F-t-L", "rancid", "exploited", "bad-religion", "a-O-P", "blink182", "haloween", "R-e-o", "offspring", "sum41", "pennywise", "s-A-Y", "social-distortion", "y-j-K", "sinergy", "last-energ!");
$rand_keys = array_rand ($input, 55);
for ($i=0; $i<$length; $i++)
{
$registerThis = $registerThis . $input[$rand_keys[$i]];
}
return $registerThis;
}
echo "<form name='test' action='' method='post'>
<input type=text name='randomKey'>
<input type=submit name=submit value=submit>
</form>";
$registerThis = generateWord(2);
then register the result with session_register:
session_register('registerThis')
for form action I use this validation:
if($_POST['randomKey'] == $_SESSION['registerThis']) { echo "Your Key is VALID"; } else { echo "Your Key is Invalid"; }
so the complete code will be like this:
<?
function generateWord($length)
{
$registerThis="";
srand ((float) microtime() * 10000000);
$rand1=rand(1,99); $rand2=rand(100,199); $rand3=rand(200,299); $rand4=rand(300,399); $rand5=rand(400,599);
$input = array ("V-!-q", "$rand4", "N-s-z", "the-clash", "i-Y-s", "brush", "pattern", "s-G-l", "gradien", "style", "b-f-D", "glossy", "render", "t-i-J", "$rand1", "$rand2", "H-*-R", "$rand3", "green-day", "danzig", "turtles-jr", "$rand5", "blur", "P-L-l", "motion", "dream", "weaver", "H-r-W", "photo", "w-x-U", "shop", "image", "ready", "flash", "web", "master", "ramones", "sex-pistols", "misfits", "F-t-L", "rancid", "exploited", "bad-religion", "a-O-P", "blink182", "haloween", "R-e-o", "offspring", "sum41", "pennywise", "s-A-Y", "social-distortion", "y-j-K", "sinergy", "last-energ!");
$rand_keys = array_rand ($input, 55);
for ($i=0; $i<$length; $i++)
{
$registerThis = $registerThis . $input[$rand_keys[$i]];
}
return $registerThis;
}
if($_POST['submit'])
{
if($_POST['randomKey'] == $_SESSION['registerThis'])
{
echo "Your Key is VALID";
}
else
{
echo "Your Key is inVALID";
}
}
else
{
$registerThis = generateWord(2);
session_register('registerThis');
echo "
<form name='test' action='' method='post'>
ENTER KEY: $registerThis <BR>
<input type=text name='randomKey'>
<input type=submit name=submit value=submit>
</form>";
}
?>