What are some common uses of captchas in PHP applications?
Captchas are commonly used in PHP applications to prevent automated bots from submitting forms or accessing certain resources. By implementing a captcha, users are required to complete a challenge (such as typing distorted text or selecting images) to prove they are human.
// Generate a simple captcha image
session_start();
$random_number = rand(1000, 9999);
$_SESSION['captcha'] = $random_number;
$im = imagecreatetruecolor(100, 38);
$bg_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 5, 5, 10, $random_number, $text_color);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);