Create Captcha from total two numbers of PHP


Posted on 2015-07-02 08:16:09


tao-captcha-dua-tren-tong-2-so-bang-php   To create captcha function as above, first we need to take two random numbers and a total of 2 of which are as follows:

<? Php
$ Number1 = rand (1.9);
$ Number2 = rand (1.9);
$ Sum = $ number1 + $ number2 ;
?>
<Form>
   ....
</ Form>
Then we will insert the value of this total inside a card  hidden  in the form, this job is for us to compare this value with the value entered by the user.
<Form>
   <Input type = "hidden" name = "correctsum" value = "<? php echo $ sum;?>" />
   ...
</ Form>
Now we will show two random numbers to the screen for users to calculate and enter the result.
<Form>
   ...
   <? Php echo $ number1 . ' + ' . $ number2 . ' = ' ; ?>
   <Input type = "text" name = "captcha" />
   ...
</ Form>
And finally, when a user enters a result and click " Submit "we will compare whether or not to coincide may lead to pages that users want.
<? Php
$ Correctsum = $ _ POST [ 'correctsum' ];
$ Captcha = $ _ POST [ 'captcha' ];
if ( $ correctsum == $ captcha ) {
   // TODO: success
} else {
   // TODO: Invalid Captcha
}
?>
That's it, you see this app as simple right? If you have any questions, you can leave a message as a comment.
You can see more guides PHP language at here
Good luck