PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
Here's a simple tutorial on how to generate a graphic verification code, often known as a CAPTCHA, using PHP.
captcha.php
) and open it in your favorite text editor.<?php session_start(); ?>
<?php $width = 200; $height = 60; $font_size = 20; $total_characters = 5;
imagecreatetruecolor()
function, and define the color for the background and the text:$image = imagecreatetruecolor($width, $height); $background_color = imagecolorallocate($image, 255, 255, 255); // white $text_color = imagecolorallocate($image, 0, 0, 0); // black imagefilledrectangle($image, 0, 0, $width, $height, $background_color);
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $random_string = ''; for ($i = 0; $i < $total_characters; $i++) { $random_string .= $characters[rand(0, strlen($characters) - 1)]; } // Save the CAPTCHA string into a session variable $_SESSION['captcha_text'] = $random_string; $font = 'arial.ttf'; // Path to your font file imagettftext($image, $font_size, 0, 15, 30, $text_color, $font, $random_string);
Remember, the path to the font file is relative to the script generating the image.
header('Content-type: image/png'); imagepng($image); imagedestroy($image);
Put all together:
<?php session_start(); $width = 200; $height = 60; $font_size = 20; $total_characters = 5; $image = imagecreatetruecolor($width, $height); $background_color = imagecolorallocate($image, 255, 255, 255); // white $text_color = imagecolorallocate($image, 0, 0, 0); // black imagefilledrectangle($image, 0, 0, $width, $height, $background_color); $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $random_string = ''; for ($i = 0; $i < $total_characters; $i++) { $random_string .= $characters[rand(0, strlen($characters) - 1)]; } $_SESSION['captcha_text'] = $random_string; $font = 'arial.ttf'; imagettftext($image, $font_size, 0, 15, 30, $text_color, $font, $random_string); header('Content-type: image/png'); imagepng($image); imagedestroy($image); ?>
You can then include this CAPTCHA image in your HTML form by using an image tag:
<img src="captcha.php" />
And when the form is submitted, you can check the user's input against the $_SESSION['captcha_text']
value to verify if they entered the correct CAPTCHA.
Creating graphic verification codes in PHP:
<?php session_start(); $verificationCode = generateVerificationCode(); $_SESSION['verification_code'] = $verificationCode; $image = createCaptchaImage($verificationCode); header('Content-Type: image/png'); imagepng($image); imagedestroy($image); function generateVerificationCode($length = 6) { $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $code = ''; for ($i = 0; $i < $length; $i++) { $code .= $characters[rand(0, strlen($characters) - 1)]; } return $code; } function createCaptchaImage($text) { $imageWidth = 150; $imageHeight = 50; $image = imagecreate($imageWidth, $imageHeight); $backgroundColor = imagecolorallocate($image, 255, 255, 255); $textColor = imagecolorallocate($image, 0, 0, 0); // Add noise or distortion effects if desired imagettftext($image, 20, 0, 10, 30, $textColor, 'arial.ttf', $text); return $image; }
Using GD library to generate captcha in PHP:
<?php $verificationCode = generateVerificationCode(); $image = createCaptchaImage($verificationCode); header('Content-Type: image/png'); imagepng($image); imagedestroy($image); // Functions from the previous example
Adding security with graphic verification in PHP forms:
<?php session_start(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $userInput = $_POST['captcha']; $verificationCode = $_SESSION['verification_code']; if ($userInput === $verificationCode) { // Captcha verification successful } else { // Captcha verification failed } }
Customizing and styling captcha images in PHP:
<?php function createCaptchaImage($text) { $imageWidth = 150; $imageHeight = 50; $image = imagecreate($imageWidth, $imageHeight); $backgroundColor = imagecolorallocate($image, 200, 220, 230); $textColor = imagecolorallocate($image, 0, 0, 0); imagettftext($image, 20, 0, 10, 30, $textColor, 'arial.ttf', $text); return $image; }
PHP captcha code examples:
<?php // Example HTML form ?> <form method="post" action="process_form.php"> <label for="captcha">Enter the code:</label> <img src="captcha.php" alt="Captcha Image"> <input type="text" name="captcha" required> <button type="submit">Submit</button> </form> <?php // PHP processing code
Refreshing or reloading captcha images in PHP:
<?php session_start(); if ($_SERVER['REQUEST_METHOD'] === 'GET') { $verificationCode = generateVerificationCode(); $_SESSION['verification_code'] = $verificationCode; $image = createCaptchaImage($verificationCode); header('Content-Type: image/png'); imagepng($image); imagedestroy($image); }
Integrating captcha with user registration in PHP:
<?php session_start(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $userInput = $_POST['captcha']; $verificationCode = $_SESSION['verification_code']; if ($userInput === $verificationCode) { // Captcha verification successful, process user registration } else { // Captcha verification failed echo "Captcha verification failed."; } }
Verifying user input with PHP captcha codes:
<?php session_start(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $userInput = $_POST['captcha']; $verificationCode = $_SESSION['verification_code']; if ($userInput === $verificationCode) { // Captcha verification successful } else { // Captcha verification failed } }
Anti-bot techniques using graphic verification in PHP:
<?php // Add captcha to forms to prevent bot submissions // Example HTML form ?> <form method="post" action="process_form.php"> <label for="captcha">Enter the code:</label> <img src="captcha.php" alt="Captcha Image"> <input type="text" name="captcha" required> <button type="submit">Submit</button> </form> <?php // PHP processing code