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);
Related Questions
- What is the best way to write a backslash (\) to a file in PHP without it being automatically escaped?
- How can a Factory pattern in PHP be adapted for a database-driven hierarchical cookbook system to efficiently generate different types of dishes?
- What are the potential issues with using the && operator in PHP code?