captcha.php_在您PHP网站上快速获取Captcha

captcha.php

If you're like me and don't have tons of time to discover and implement new things/features on your website then you'll like this quick and easy example to keep automated bots from exploiting your web application forms. In just a couple of steps I'll walk you through on how to get the Captcha tool on your website.

如果您像我一样,没有足够的时间在您的网站上发现和实现新事物/功能,那么您将喜欢这个简单快捷的示例,以防止自动化机器人利用您的Web应用程序表单。 仅需几个步骤,我将引导您逐步了解如何在您的网站上获取验证码工具。

screen shot of Captcha

To get started head on over to http://www.google.com/recaptcha/whyrecaptcha to sign up (for free of course) and get your public and private keys (which you'll use in the next step). Once you're all signed up all you'll need is the recaptchalib.php file to put into your directory.

要开始使用,请先转到http://www.google.com/recaptcha/whyrecaptcha进行注册(当然是免费的)并获取您的公共密钥和私有密钥(将在下一步中使用)。 完成所有注册后,您只需要将recaptchalib.php文件放入目录中。

If you have successfully signed up and have the recaptchalib.php file and a private and public key you're ready to finish up.

如果您已成功注册并拥有recaptchalib.php文件以及私钥和公钥,则可以准备完成。

First of all let's start with a simple php page (form.php) with a form that submits the inputted text on another page (success.php) if the captcha entry was valid; if not, then we show an error message below the captcha on the form page.

首先,让我们从一个简单的php页面(form.php)开始,如果验证码条目有效,则该表单会在另一个页面(success.php)上提交输入的文本; 如果不是,那么我们在表单页面的验证码下方显示错误消息。

This example asks the user to input his favorite food and insert the Captcha. If the Captcha is correct, the user will be directed to a success page that shows his entry - otherwise an error message is displayed telling the user that the Captcha was entered incorrectly.

本示例要求用户输入他喜欢的食物并插入验证码。 如果验证码正确,则会将用户定向到显示其输入内容的成功页面-否则,将显示一条错误消息,告知用户验证码输入错误。

form.php

form.php

<form action="form.php" method="post">
            Favorite Food: <input type="text" id="favorite_food" name="favorite_food" value="" size="40" />
            <br /><br />
            <?php
            require_once('recaptchalib.php');
            $publickey = "your public key goes here...";
            $privatekey = "your private key goes here...";
            echo recaptcha_get_html($publickey, $error);
            ?>
            <br />
            <input type="submit" value="Submit" />
        </form>
        <br />
        <?php
        if ($_POST["favorite_food"]) { //checks to see if the form has been submitted
            $response = recaptcha_check_answer($privatekey, // captcha's function to validate input
                    $_SERVER["REMOTE_ADDR"],
                    $_POST["recaptcha_challenge_field"],
                    $_POST["recaptcha_response_field"]);
            if ($response->is_valid) {
                $fav_food = $_POST['favorite_food'];
                printf("<script>location.href='success.php?fav_food=$fav_food';</script>");
            } else {
                # set the error code in GREEN!
                echo "<p style='color: #95ca05; font-size: larger; font-family: 'tempus sans itc';>You entered the Captcha incorrectly.</p><br /><br />";
            }
        }
        ?>
Your favorite food is: <?php echo($_GET['fav_food']);?>

That's it, you can see how easy it is to determine what your web application will do when the captcha is correct or not. There are more customization options available if you check out http://code.google.com/apis/recaptcha/docs/customization.html and also there are 'PHP-less' solutions that I am not covering here: http://code.google.com/apis/recaptcha/docs/display.html.

就是这样,您可以确定在验证码正确与否时确定Web应用程序将执行的操作有多么容易。 如果您查看http://code.google.com/intl/zh-CN/apis/recaptcha/docs/customization.html ,还有更多的自定义选项可用,并且还有一些“无PHP”解决方案,我在这里不介绍: code.google.com/apis/recaptcha/docs/display.html

If you want to see a working example of the example you can check it out here: http://www.patsmitty.com/captcha_tut/form.php

如果您想查看该示例的工作示例,可以在此处进行查看: http : //www.patsmitty.com/captcha_tut/form.php

Also the source files are attached.

源文件也被附加。

Cheers

干杯

form.php form.php recaptchalib.php recaptchalib.php success.php success.php

翻译自: https://www.experts-exchange.com/articles/3657/Quickly-get-Captcha-on-your-PHP-site.html

captcha.php

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值