ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมสร้าง CAPTCHA และยืนยันผู้ใช้
สำหรับสิ่งนี้ เราจะจัดเตรียมสตริงสุ่มให้กับผู้ใช้และขอให้เขาป้อนสตริงเดิมอีกครั้ง จากนั้นจะต้องตรวจสอบว่าสตริงที่กำหนดและอินพุตตรงกันหรือไม่
CAPTCHA ควรสร้างระบบแบบสุ่มโดยสมบูรณ์ซึ่งประกอบด้วย a-z, AZ และ 0-9
ตัวอย่าง
#include<bits/stdc++.h>
using namespace std;
//checks if the strings are same
bool check_string(string &captcha, string &user_captcha){
return captcha.compare(user_captcha) == 0;
}
//generates a random string as Captcha
string gen_captcha(int n){
time_t t;
srand((unsigned)time(&t));
char *chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHI" "JKLMNOPQRSTUVWXYZ0123456789";
string captcha = "";
while (n--)
captcha.push_back(chrs[rand()%62]);
return captcha;
}
int main(){
string captcha = gen_captcha(9);
cout << captcha;
string usr_captcha;
cout << "\nEnter CAPTCHA : ";
usr_captcha = "fgyeugs56";
if (check_string(captcha, usr_captcha))
printf("\nCAPTCHA Matched");
else
printf("\nCAPTCHA Not Matched");
return 0;
} ผลลัพธ์
nwsraJhiP Enter CAPTCHA : CAPTCHA Not Matched