NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Simple human verification code.

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
7 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 03-02-2010, 06:55 PM THREAD STARTER               #1 (permalink)
Account Suspended
Join Date: Dec 2008
Location: Boston, Ma
Posts: 650
CrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to all
 



Marrow Donor Program Animal Rescue Autism Autism

Cool Simple human verification code.


I hate captcha and I am sure you do to. Here is an alternative that is abit more fun, to me at least.

Put this into a file named whatever.php:

PHP Code:
$randnum mt_rand(1,25);
switch(
$randnum)
 {
    case 
1:
          
$botq 'What is 5 + 3?';
          
$bota '8';
       break;
    case 
2:
    
$botq 'What is 3 + 2?';
          
$bota '5';
       break;
    case 
3:
????: NamePros.com http://www.namepros.com/code/642705-simple-human-verification-code.html
    
$botq 'What is 5 + 5?';
          
$bota '10';
????: NamePros.com http://www.namepros.com/showthread.php?t=642705
       break;
    case 
4:
    
$botq 'What is 2 + 7?';
          
$bota '9';
       break;
    case 
5:
    
$botq 'What color is a lemon?';
          
$bota 'yellow';
       break;
    case 
6:
    
$botq 'What is 5 + 1?';
          
$bota '6';
       break;
    case 
7:
    
$botq 'What is 7 + 7?';
          
$bota '14';
       break;
    case 
8:
    
$botq 'What is 8 + 8?';
          
$bota '16';
       break;
    case 
9:
    
$botq 'What shape has no corners?';
          
$bota 'circle';
       break;
    case 
10:
    
$botq 'What is 6 + 6?';
          
$bota '12';
       break;
    case 
11:
    
$botq 'What is 5 x 3?';
          
$bota '15';
       break;
    case 
12:
    
$botq 'What is 16 + 2?';
          
$bota '18';
       break;
    case 
13:
    
$botq 'What is 20 + 25?';
          
$bota '45';
       break;
    case 
14:
    
$botq 'What is 16 + 4?';
          
$bota '20';
       break;
    case 
15:
    
$botq 'What is 9 + 1?';
          
$bota '10';
       break;
    case 
16:
    
$botq 'What is 7 x 3?';
          
$bota '21';
       break;
    case 
17:
    
$botq 'What is 7 + 7 + 1?';
          
$bota '15';
       break;
    case 
18:
    
$botq 'What is 13 + 3?';
          
$bota '16';
       break;
    case 
19:
    
$botq 'What color is a banana?';
          
$bota 'yellow';
       break;
    case 
20:
    
$botq 'What is 15 - 5?';
          
$bota '10';
       break;
    case 
21:
    
$botq 'Are you a bot?';
          
$bota 'no';
       break;
    case 
22:
    
$botq 'Are you a cylon?';
          
$bota 'no';
       break;
    case 
23:
    
$botq 'Are you from mars?';
          
$bota 'no';
       break;
    case 
24:
    
$botq 'Is Earth a cube?';
          
$bota 'no';
       break;
    case 
25:
    
$botq 'Can pigeons fly?';
          
$bota 'yes';
       break;

Ok, now to the form portion. make sure that your script has session_start() at the top of it and call this before your form starts:

PHP Code:
    require('path/to/whatever.php');
    
$_SESSION['antibotcode'] = md5(strtolower($bota));
    
session_write_close(); 
Now in your form, add this field, change teh style how you want and dump the paragragh tags if you want. If your form is not inside php tags, remove this ' . $botq . ' and replace it with <?php echo $botq; ?> . I hate design integrated with code, but to each his/her own, right?

HTML Code:
<p style="margin-top:0px;padding-top:0;"><span style="color:red;">* Human verification: </span><strong>' . $botq . '</strong><br style="height:35px;" /><input type="text" name="b" /></p>
Ok finally, in the script that you parse the form, you want to do this:

PHP Code:
    if (!empty($_POST['b'])) {
        
$b md5(strtolower($_POST['b']));
    } else {
        echo 
'Go back and fill out all fields! Pretty please?';
    }
    if (!empty(
$_SESSION['antibotcode'])) {
        if ( 
$b != $_SESSION['antibotcode'] ) {
            unset(
$_SESSION['antibotcode']);
            echo 
'Code typed incorrectly. Go back.';
        }
    } else {
        echo 
'Hmmm, how did you get here without the proper session being set?';
    }
    unset(
$_SESSION['antibotcode']);
                
// all is good, continue parsing form! 
This is just a sample, but it gives you the general idea and you can change it to your specific needs.

* I like this, because I can include some very funny questions to give my guests a giggle as they sign up.


.
Last edited by CrackFeed.Com; 03-05-2010 at 05:53 PM.
CrackFeed.Com is offline   Reply With Quote
Old 03-03-2010, 05:46 AM THREAD STARTER               #2 (permalink)
Account Suspended
Join Date: Dec 2008
Location: Boston, Ma
Posts: 650
CrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to all
 



Marrow Donor Program Animal Rescue Autism Autism
Note that there are faster and more efficient ways to store the Q and A's. But this is just an example, but it works well.
CrackFeed.Com is offline   Reply With Quote
Old 03-04-2010, 06:09 AM   #3 (permalink)
NamePros Regular
 
qbert220's Avatar
Join Date: Jul 2007
Location: UK
Posts: 394
qbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to behold
 


Protect Our Planet
Great stuff. Thanks for sharing.

You might want to lowercase the visitor's answer before md5ing it to avoid rejecting someone typing "Yes", when you expected "yes".

I personally hate the ones that ask you to complete a word (I find this hard for some reason - I guess just the way my brain is wired). Also some questions are aimed at a particular culture or language variant. This is one I get asked quite often at one forum (it obviously has a limited set of questions):
????: NamePros.com http://www.namepros.com/showthread.php?t=642705

Quote:
Finish this word, these fly through the sky, airp????
The correct answer (for me) is aeroplanes, which obviously doesn't work.

I'd probably use arrays for storing Q's and A's.
qbert220 is offline   Reply With Quote
Old 03-05-2010, 05:52 PM THREAD STARTER               #4 (permalink)
Account Suspended
Join Date: Dec 2008
Location: Boston, Ma
Posts: 650
CrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to all
 



Marrow Donor Program Animal Rescue Autism Autism
Lol, I just first ran into one of those today at the cPanel boards. I hated it! lol

Thanks for pointing that out, changed
CrackFeed.Com is offline   Reply With Quote
Old 03-13-2010, 02:16 PM   #5 (permalink)
Ik
Quality //
Join Date: Sep 2008
Posts: 911
Ik has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud ofIk has much to be proud of
 


Breast Cancer Save a Life Alzheimer's Wildlife Wildlife
You know what! I hate it when the font is unreadable ;-( sometimes I try 3-4 times to get it right :-(

I'm with math. Thanks for the code.
Ik is offline   Reply With Quote
Old 03-13-2010, 05:31 PM THREAD STARTER               #6 (permalink)
Account Suspended
Join Date: Dec 2008
Location: Boston, Ma
Posts: 650
CrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to all
 



Marrow Donor Program Animal Rescue Autism Autism
Tell me about it, I retried a captcha 7 times earlier. lol

You are quite welcome!
CrackFeed.Com is offline   Reply With Quote
Old 03-14-2010, 01:41 PM   #7 (permalink)
NamePros Member
Join Date: Sep 2008
Posts: 109
intheflow is on a distinguished road
 



Nice code, thanks for sharing.

Just what I need for my contact form.
intheflow is offline   Reply With Quote
Old 03-16-2010, 07:27 PM THREAD STARTER               #8 (permalink)
Account Suspended
Join Date: Dec 2008
Location: Boston, Ma
Posts: 650
CrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to all
 



Marrow Donor Program Animal Rescue Autism Autism
Thanks and you are welcome
CrackFeed.Com is offline   Reply With Quote
Old 03-16-2010, 08:20 PM   #9 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
I changed 'whatever.php' to the below. I dunno, that switch just made me cringe (not that all these arrays are that much better, but meh )
PHP Code:
<?php

$questions   
= array();
$questions[] = array('q' => 'What is 5 + 3?',             'a' => '8');
$questions[] = array('q' => 'What is 3 + 2?',             'a' => '5');
$questions[] = array('q' => 'What is 5 + 5?',             'a' => '10');
$questions[] = array('q' => 'What is 2 + 7?',             'a' => '9');
$questions[] = array('q' => 'What color is a lemon?',     'a' => 'yellow');
$questions[] = array('q' => 'What is 5 + 1?',             'a' => '6');
$questions[] = array('q' => 'What is 7 + 7?',             'a' => '14');
$questions[] = array('q' => 'What is 8 + 8?',             'a' => '16');
$questions[] = array('q' => 'What shape has no corners?''a' => 'circle');
$questions[] = array('q' => 'What is 6 + 6?',             'a' => '12');
$questions[] = array('q' => 'What is 5 x 3?',             'a' => '15');
$questions[] = array('q' => 'What is 16 + 2?',            'a' => '18');
$questions[] = array('q' => 'What is 20 + 25?',           'a' => '45');
$questions[] = array('q' => 'What is 16 + 4?',            'a' => '20');
$questions[] = array('q' => 'What is 9 + 1?',             'a' => '10');
????: NamePros.com http://www.namepros.com/showthread.php?t=642705
$questions[] = array('q' => 'What is 7 x 3?',             'a' => '21');
$questions[] = array('q' => 'What is 7 + 7 + 1?',         'a' => '15');
$questions[] = array('q' => 'What is 13 + 3?',            'a' => '16');
$questions[] = array('q' => 'What color is a banana?',    'a' => 'yellow');
$questions[] = array('q' => 'What is 15 - 5?',            'a' => '10');
$questions[] = array('q' => 'Are you a bot?',             'a' => 'no');
$questions[] = array('q' => 'Are you a cylon?',           'a' => 'no');
$questions[] = array('q' => 'Are you from mars?',         'a' => 'no');
$questions[] = array('q' => 'Is Earth a cube?',           'a' => 'no');
$questions[] = array('q' => 'Can pigeons fly?',           'a' => 'yes');

$randnum array_rand($questions);

$botq $questions[$randnum]['q'];
$bota $questions[$randnum]['a'];
????: NamePros.com http://www.namepros.com/showthread.php?t=642705

?>
Eric is offline   Reply With Quote
Old 03-17-2010, 03:11 PM THREAD STARTER               #10 (permalink)
Account Suspended
Join Date: Dec 2008
Location: Boston, Ma
Posts: 650
CrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to all
 



Marrow Donor Program Animal Rescue Autism Autism
Unless I am mistaken, that will use more resources from having to read all of that, where-as in a switch, it ignores the statements unless the key is matched.
CrackFeed.Com is offline   Reply With Quote
Old 03-17-2010, 04:19 PM   #11 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Originally Posted by PHPGURU View Post
Unless I am mistaken, that will use more resources from having to read all of that, where-as in a switch, it ignores the statements unless the key is matched.
Not necessarily. As more cases are added, the switch would become slower at executing than the array's. For this particular situation, the difference in speed of execution is negligible.
????: NamePros.com http://www.namepros.com/showthread.php?t=642705

My main reason for doing it the way I did, is readability. I think using the array's instead of giant switch statement is much more readable. JMO
Eric is offline   Reply With Quote
Old 03-17-2010, 04:30 PM THREAD STARTER               #12 (permalink)
Account Suspended
Join Date: Dec 2008
Location: Boston, Ma
Posts: 650
CrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to all
 



Marrow Donor Program Animal Rescue Autism Autism
Yeah, is pretty much up to personal pref unless you add a lot more.
CrackFeed.Com is offline   Reply With Quote
Old 04-09-2010, 12:16 AM   #13 (permalink)
Senior Member
 
Nodws's Avatar
Join Date: Jun 2008
Location: nodws.com
Posts: 1,666
Nodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud of
 



Save a Life
much better to say
Code:
$n1=rand(0,9);
$n2=rand(0,9);
$result=$n1+$n2+1; //we add 1, or any
$question="what is $n1 + $n2?";

//we echo the form and hide the result
echo"<form method=post>$question <input name=question><input type=hidden name=result value=$result></form>";

//we validate and subtract 1
if(($_POST[result]-1)===$_POST[question])
{
echo"you are human!"; //hurray, no switch()
}
simple as that
__________________
Last edited by Nodws; 04-09-2010 at 12:20 AM.
Nodws is offline   Reply With Quote
Old 04-09-2010, 02:45 PM   #14 (permalink)
Account Suspended
Join Date: Apr 2010
Posts: 13
IsaacTacken is an unknown quantity at this point
 




Thanks. I will use this instead of a captcha from now on.

Is there any problem if I replace the questions with my own? Or it doesn't matter to you?
IsaacTacken is offline   Reply With Quote
Old 05-03-2010, 06:58 PM THREAD STARTER               #15 (permalink)
Account Suspended
Join Date: Dec 2008
Location: Boston, Ma
Posts: 650
CrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to all
 



Marrow Donor Program Animal Rescue Autism Autism
Nah, change it to fit your needs
CrackFeed.Com is offline   Reply With Quote
Reply

Tags
captcha alternative, php code


LinkBacks (?)
LinkBack to this Thread: http://www.namepros.com/code/642705-simple-human-verification-code.html
Posted By For Type Date
Simple Human Verification with CodeIgniter Form Validation | Web Success - atomni This thread Refback 10-28-2011 01:08 AM
Simple Human Verification with CodeIgniter Form Validation | Web Success This thread Pingback 10-21-2010 12:41 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 07:32 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger