📘 Lesson  ·  Lesson 50

CAPTCHA Validation

CAPTCHA Validation

About

A CAPTCHA stops spam bots by asking the user to prove they are human. Here is a simple text CAPTCHA.

Generate CAPTCHA

session_start();
$code = substr(str_shuffle("ABCDEFGHJKMNPQRSTUVWXYZ23456789"), 0, 5);
$_SESSION["captcha"] = $code;
echo "Type this code: <b>$code</b>";

Verify CAPTCHA

if (strtoupper($_POST["captcha"]) === $_SESSION["captcha"]) {
    echo "Correct! You are human.";
} else {
    echo "Wrong code, try again.";
}

For Production

For real sites, Google reCAPTCHA is stronger and easier than building your own. Use it for important forms.

Summary

  • Generate a random code, store in session, ask the user to type it.
  • Compare on submit; use Google reCAPTCHA for production sites.

परिचय

CAPTCHA spam bots को रोकता है, user से इंसान होने का सबूत माँगकर। यह रहा simple text CAPTCHA।

CAPTCHA Generate करें

session_start();
$code = substr(str_shuffle("ABCDEFGHJKMNPQRSTUVWXYZ23456789"), 0, 5);
$_SESSION["captcha"] = $code;
echo "Type this code: <b>$code</b>";

CAPTCHA Verify करें

if (strtoupper($_POST["captcha"]) === $_SESSION["captcha"]) {
    echo "Correct! You are human.";
} else {
    echo "Wrong code, try again.";
}

Production के लिए

असली sites के लिए Google reCAPTCHA अपना बनाने से मज़बूत और आसान है। ज़रूरी forms के लिए use करें।

सारांश

  • Random code generate करें, session में store करें, user से type करवाएं।
  • Submit पर compare करें; production sites के लिए Google reCAPTCHA use करें।
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।