📘 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 करें।
💻 Live Code Editor
Is page ki language (PHP) mein code likhein aur turant chalाएं — yahीं, bina kuch install kiye.Powered by OneCompiler. Agar editor na khule to yahान naye tab mein kholein.
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.