开发者

PHP captcha Image

开发者 https://www.devze.com 2023-04-07 13:33 出处:网络
I am using a captcha image verifier in my PHP form. The form uses a cookie to check the verfication code entered by the user. Is there anyother w开发者_JAVA技巧ay to do this other than using a cookie?

I am using a captcha image verifier in my PHP form. The form uses a cookie to check the verfication code entered by the user. Is there anyother w开发者_JAVA技巧ay to do this other than using a cookie?


Cookies are a very bad idea in this case. Instead, you should use a variable set in the $_SESSION PHP superglobal. At the top of your captcha-input page, add this (minimal version):

session_start();
$_SESSION['captchaCode'] = /* whatever */

session_start() documentation can be found here.

Then, when the form is submitted, check that the value submitted from the form is the same as the one in $_SESSION['captchaCode']:

session_start();

if($_SESSION['captchaCode'] == $_POST['captchaCode'])
{
    // Do interesting things
}

Do bear in mind that this is a very simple, generic way of doing it. If you're using reCAPTCHA or Securimg then they will have their own ways of validating the captchas they generate.


I don't use cookies, you can just see if what they enter matches directly with what you have in the database of captcha entries; that is if you have a database of possible captchas?


Here is a script that is very easy to use and setup. This script just creates the image but it is not tough to incorporate the verification. You can download the script here to verify the captach image use either php or javascript to make sure the post value is equal to $_SESSION['security_number'] and your done.

0

精彩评论

暂无评论...
验证码 换一张
取 消