Important: Version 1.0 of the reCAPTCHA API is no longer supported, please upgrade to Version 2.0. Learn more
- Registration Form In Html With Css
- Contact Form In Html With Captcha Code For Php Form
- Contact Form In Html With Captcha Code For Php Login
The PHP code above creates a new instance of the Captcha class defined by the BotDetect PHP Captcha library, tells it which textbox is used to input Captcha codes, and calls the Html function to generate all needed BotDetect Html elements. How To Create a Contact Form Step 1) Add HTML. Use a form> element to process the input. You can learn more about this in our PHP tutorial. Then add inputs (with a matching label) for each field. About HTML Preprocessors. HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug. In this pen I use jQuery to code contact form with captcha code and custom filter validation for other fields. In this tutorial you will learn how to create a simple php captcha code for contact forms using PHP and some math operations to increase the security of your html form and. Demo Download files Demo Bootstrap 3. Register your site. To be able to use ReCaptcha, you will need to register your website on ReCaptcha's website first. After successful registration, you will get a pair of keys to use with your ReCaptcha. Leave the page open or copy the keys to a.
The reCAPTCHA PHP Library provides a simple way to place a CAPTCHA onyour PHP website, helping you stop bots from abusing it. The library wraps thereCAPTCHA API.
To use reCAPTCHA with PHP, you can download reCAPTCHA PHPlibrary. You will only need one file from there (recaptchalib.php). The other files areexamples, readme and legal stuff -- they don't affect functionality.
Quick Start
After you've signed up for your API keys, below are basic instructions for installing reCAPTCHA on your site. A fullreference guide to the PHP plugin can be found below.
Client Side (How to make the CAPTCHA image show up)
If you want to use the PHP library to display the reCAPTCHA widget, you'll need to insertthis snippet of code inside the <form> element where the reCAPTCHA widget will beplaced:
With the code, your form might look something like this:
![Contact form in html with captcha code for php contact Contact form in html with captcha code for php contact](/uploads/1/2/6/2/126228136/538571393.jpg)
Don't forget to set $publickey by replacing
your_public_key
withyour API public key.Note that the value of the 'action' attribute is 'verify.php'. Now, verify.php is thedestination file in which the values of this form are submitted to. So you will need a fileverify.php in the same location as the client html.
The require_once function in the example above expects recaptchalib.php to be in the samedirectory as your form file. If it is in another directory, you must link it appropriately.For example if your recaptchalib.php is in the directory called 'captcha' that is on the samelevel as your form file, the function will look like this:
require_once('captcha/recaptchalib.php')
.Server Side (How to test if the user entered the right answer)
The following code should be placed at the top of the verify.php file:
In the code above:
recaptcha_check_answer
returns an object that represents whether the usersuccessfully completed the challenge.$resp->is_valid
is true then the captcha challenge was correctly completed and you should continue with form processing.$resp->is_valid
is false then the user failed to provide the correct captchatext and you should redisplay the form to allow them another attempt. In this case$resp->error
will be an error code that can be provided to recaptcha_get_html
.Passing the error code makes the reCAPTCHA control display a message explaining that the userentered the text incorrectly and should try again.Notice that this code is asking for the private key, which should not be confusedwith the public key. You get that from the same page as the public key.
Also make sure your form is set to get the form variables using $_POST, instead of$_REQUEST, and that the form itself is using the POST method.
That's it! reCAPTCHA should now be working on your site.
Further Reading
PHP Plugin Reference Guide
Below is a comprehensive list of all the methods of the reCAPTCHA PHP Plugin.
The recaptcha_get_html function
The
recaptcha_get_html
function displays the HTML that presents reCAPTCHA totheuser.recaptcha_get_html | |
Parameter | |
$pubkey -- string. required. | Your reCAPTCHA public key, from the API Signup Page |
$error -- string. optional (null is the default) | If this string is set, the reCAPTCHA area will display the error code given. This error code comes from ReCaptchaResponse->$error |
$use_ssl -- boolean. optional (false is default) | Should the SSL-based API be used? If you are displaying a page to the user over SSL, be sure to set this to true so an error dialog doesn't come up in the user's browser. |
Return value | A string containing HTML to put on the web page. |
The recaptcha_check_answer function
After the user has filled out the HTML form, including their answer for the CAPTCHA, wewant to check their answer when they submit the form using the
recaptcha_check_answer
function. The user's answer will be in two form fields,recaptcha_challenge_field
and recaptcha_response_field
. ThereCAPTCHA library will make an HTTP request to the reCAPTCHA server and verify the user'sanswer.Registration Form In Html With Css
recaptcha_check_answer | |
Parameter | |
$privkey -- string. required. | Your reCAPTCHA private key, from the API Signup Page. |
$remoteip -- string. required. | The user's IP address, in the format 192.168.0.1 |
$challenge -- string. required. | The value of the form field recaptcha_challenge_field |
$response -- string. required | The value of the form field recaptcha_response_field |
Return value | An instance of the ReCaptchaResponse class |
ReCaptchaResponse | |
Field | |
$is_valid -- boolean | Did reCAPTCHA believe the answer was valid? |
$error -- string | If the answer was invalid what was the problem? This error code can be used in recaptcha_get_html |
Return value | The HTML or raw url to decode the email address, depending on which you function youcalled. |
Mailhide
The reCAPTCHA PHP Library includes bindings for the Mailhide API. ThisAPI allows you to wrap an email in a reCAPTCHA to prevent spammers from seeing it:[email protected].
Contact Form In Html With Captcha Code For Php Form
The Mailhide portion of the PHP Library requires the PHP mcrypt module.
The Mailhide API consists of two functions
recaptcha_mailhide_html
andrecaptcha_mailhide_url
. The functions have the same parameters. The _html versionreturns HTMLthat can be directly put on your web page. The username portion of the email that is passed inis truncated and replaced with a link that calls Mailhide. The _url version gives you the urlto decode the email and leaves it up to you to place the email in HTML.recaptcha_mailhide_url / recaptcha_mailhide_html | |
Parameter | |
$pubkey -- string | The Mailhide public key from the signup page |
$privkey -- string | The Mailhide private key from the signup page |
$email -- string | The email address you want to hide. |
Contact Form In Html With Captcha Code For Php Login
The following example shows how to use Mailhide: