How to validate my PHP captcha case insensitive? - php

I would like users submitting a captcha to be able to enter a mix of uppercase and lowercase, regardless of what the captcha image says, and the captcha to still be validated.
So for example, if the captcha image says 'r9zvk1', users should be able to input 'R9zvk1' and still have the captcha validated as correct.
My current captcha code:
<?php
session_start();
$permitted_chars = 'abcdefghjklmnpqrstuvwxyz0123456789';
function generate_string($input, $strength = 10) {
$input_length = strlen($input);
$random_string = '';
for($i = 0; $i < $strength; $i++) {
$random_character = $input[mt_rand(0, $input_length - 1)];
$random_string .= $random_character;
}
return $random_string;
}
$image = imagecreatetruecolor(200, 50);
imageantialias($image, true);
$colors = [];
$red = rand(125, 175);
$green = rand(125, 175);
$blue = rand(125, 175);
for($i = 0; $i < 5; $i++) {
$colors[] = imagecolorallocate($image, $red - 20*$i, $green - 20*$i, $blue - 20*$i);
}
imagefill($image, 0, 0, $colors[0]);
for($i = 0; $i < 10; $i++) {
imagesetthickness($image, rand(2, 10));
$line_color = $colors[rand(1, 4)];
imagerectangle($image, rand(-10, 190), rand(-10, 10), rand(-10, 190), rand(40, 60), $line_color);
}
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
$textcolors = [$black, $white];
$fonts = [dirname(__FILE__).'/fonts/font.ttf'];
$string_length = 5;
$captcha_string = generate_string($permitted_chars, $string_length);
$_SESSION['captcha_text'] = $captcha_string;
for($i = 0; $i < $string_length; $i++) {
$letter_space = 170/$string_length;
$initial = 15;
imagettftext($image, 24, rand(-15, 15), $initial + $i*$letter_space, rand(25, 45), $textcolors[rand(0, 1)], $fonts[array_rand($fonts)], $captcha_string[$i]);
}
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
My current captcha validation code:
<?PHP
session_start();
if($_POST['captcha'] != $_SESSION['captcha_text']) die("Sorry, the CAPTCHA code entered was incorrect!");
session_destroy();
?>

Related

Try a simple captcha in CI4

I want to implement a simple captcha in ci4, I try to search a sample code in google and then copas it. But when run the code get result like this. Please help what wrong in my code ?
My controller is :
private function captcha()
{
$i = 0;
$imgHeight = 80;
$imgWidth = 250;
$randTotal = 7;
$randomDots = 50;
$randomLines = 25;
$font = realpath('./fonts/monofont.ttf');
$random = '';
$captTextColor = "0x142864";
$noiseColor = "0x142864";
$fontSize = $imgHeight * 0.65;
$random = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0,
$randTotal);
$image = #imagecreate($imgWidth, $imgHeight);
$arrTextColor = $this->hexToRGB($captTextColor);
$arrNoiseColor = $this->hexToRGB($noiseColor);
$bgColor = imagecolorallocate($image, 255, 255, 255);
$captTextColor = imagecolorallocate($image, $arrTextColor['red'], $arrTextColor['green'], $arrTextColor['blue']);
$imgNoiseColor = imagecolorallocate($image, $arrNoiseColor['red'], $arrNoiseColor['green'], $arrNoiseColor['blue']);
// Cetak titik acak di latar belakang gambar
for ($i = 0; $i < $randomDots; $i++) {
imagefilledellipse($image, mt_rand(0, $imgWidth), mt_rand(0, $imgHeight), 2, 3, $imgNoiseColor);
}
// Cetak garis acak di latar belakang gambar
for ($i = 0; $i < $randomLines; $i++) {
imageline(
$image,
mt_rand(0, $imgWidth),
mt_rand(0, $imgHeight),
mt_rand(0, $imgWidth),
mt_rand(0, $imgHeight),
$imgNoiseColor
);
}
// Cetak kotak teks dan tambahkan 6 kode huruf captcha
$textBox = imagettfbbox($fontSize, 0, $font, $random);
$x = ($imgWidth - $textBox[4]) / 2;
$y = ($imgHeight - $textBox[5]) / 2;
$_SESSION['captcha'] = $random;
// imagettftext($image, $fontSize, 0, $x, $y, $captTextColor, $font, $random);
imagettftext($image, $fontSize, 0, $x, $y, $captTextColor, $font, $random);
header('Content-Type: image/jpeg');
imagejpeg($image, null, null);
imagedestroy($image);
}
Thank you
Thank you.
Try adding the exit; command after the imagedestroy($image); line

imagejpeg function shows a long content

I'm trying to write a code for making captcha images with GD in PHP. everything works fine but there is a problem that when I output the image with imagejpeg() function in addition to photos, it also shows some content including the strange Unicodes that I don't know what are they.
The file that the image is making in it (captcha.php):
<?php
$captcha_code=strtoupper(substr(sha1(random_bytes(64)),13,6));
$image = imagecreatetruecolor(200, 50);
imageantialias($image, true);
$colors = [];
$red = rand(125, 175);
$green = rand(125, 175);
$blue = rand(125, 175);
$black = imagecolorallocate($image, 30, 30, 30);
$white = imagecolorallocate($image, 230, 230, 230);
$textcolors = [$black, $white];
for($i = 0; $i < 5; $i++) {
$colors[] = imagecolorallocate($image, $red - 20*$i, $green - 20*$i, $blue - 20*$i);
}
imagefill($image, 0, 0, $colors[0]);
for($i = 0; $i < 10; $i++) {
imagesetthickness($image, rand(2, 10));
$rect_color = $colors[rand(1, 4)];
imagerectangle($image, rand(-10, 190), rand(-10, 10), rand(-10, 190), rand(40, 60), $rect_color);
}
$size = 24; $x = 5; $y = 30; $angle = 0; $quality = 100;
$color = imagecolorallocate($image, 255, 255, 255);
for($i = 0; $i < 6; $i++) {
$letter_space = 170/6;
$initial = 15;
imagettftext($image, 25, rand(-15, 15), $initial + $i*$letter_space, rand(20, 40), $textcolors[rand(0, 1)], __DIR__."\Acme.ttf", $captcha_code[$i]);
}
imagejpeg($image);
And in this part I try to show the image:
<div class="form-group col-6">
<label>Captcha Code</label>
<img src="scripts/captcha.php" alt="PHP Captcha">
</div>
The captcha image works fine but on the page that I try to show that there is a long content .
The content above the captcha image
I also tried to put header with image content type but another problem is that it causes just a black page with a small blank image in the middle of it although I don't have any errors.
I searched about this problem very much but I didn't find the exact way.
I'll be very happy if you help me :)

Wordpress problem with header('Content-type: image/png');

i'am trying to put a CAPTCHA in a wordpress custom plugin.
I did follow that tutorial :
https://code.tutsplus.com/tutorials/build-your-own-captcha-and-contact-form-in-php--net-5362
<?php
session_start();
$permitted_chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
function generate_string($input, $strength = 10) {
$input_length = strlen($input);
$random_string = '';
for($i = 0; $i < $strength; $i++) {
$random_character = $input[mt_rand(0, $input_length - 1)];
$random_string .= $random_character;
}
return $random_string;
}
$image = imagecreatetruecolor(200, 50);
imageantialias($image, true);
$colors = [];
$red = rand(125, 175);
$green = rand(125, 175);
$blue = rand(125, 175);
for($i = 0; $i < 5; $i++) {
$colors[] = imagecolorallocate($image, $red - 20*$i, $green - 20*$i, $blue - 20*$i);
}
imagefill($image, 0, 0, $colors[0]);
for($i = 0; $i < 10; $i++) {
imagesetthickness($image, rand(2, 10));
$line_color = $colors[rand(1, 4)];
imagerectangle($image, rand(-10, 190), rand(-10, 10), rand(-10, 190), rand(40, 60), $line_color);
}
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
$textcolors = [$black, $white];
$fonts = [dirname(__FILE__).'/Acme.ttf'];
$string_length = 6;
$captcha_string = generate_string($permitted_chars, $string_length);
$_SESSION['captcha_text'] = $captcha_string;
for($i = 0; $i < $string_length; $i++) {
$letter_space = 170/$string_length;
$initial = 15;
imagettftext($image, 24, rand(-15, 15), $initial + $i*$letter_space, rand(25, 45), $textcolors[rand(0, 1)], $fonts[array_rand($fonts)], $captcha_string[$i]);
}
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
At first wordpress didn't find the font, but i manage to pass the error.
Then i call by shortcode the html. And i all i got is the alt and broken img. If i put directly the script in the function chrome tell me that my image contain errors. I did try to add utf8 encode and to pass the image without the header, none of that work.
public function CAPTCHA_shortcode_function() {
ob_start();
?>
<div class="elem-group">
<label for="captcha">Please Enter the Captcha Text</label>
<img src="CAPTCHA-generate-image.php" alt="CAPTCHA" class="captcha-image"><i class="fas fa-redo refresh-captcha"></i>
<br>
<input type="text" id="captcha" name="captcha_challenge" pattern="[A-Z]{6}">
</div>
<?php
$CAPTCHA = ob_get_clean();
return $CAPTCHA;
}
If you have any idea, something must be wrong ^^ Thanks a lot !
We did find a solution :
imagepng($captcha_image,"captcha_image.png");
$imgData = file_get_contents('captcha_image.png');
imagedestroy($captcha_image);
// $imgData = ob_get_clean();
return $imgData;
Then call it in the function like this
ob_start();
$imgData = $this->CAPTCHA_generate();
?>
<div id="CAPTCHA_box">
<label for="captcha">Entrez les </label><br>
<?php echo '<img class="captcha_image" src="data:image/png;base64,'.base64_encode($imgData).'" />'; ?>
<i class="fas fa-redo refresh_captcha"></i>
<br>
<input type="text" id="captcha" name="captcha_challenge" pattern="[A-Z]{6}">
</div>
<?php
$CAPTCHA = ob_get_clean();
return $CAPTCHA;

custom captcha image wordpress form

I want to create a captcha image for my WordPress ajax singup form. for this reason, I create a captcha.php file in root for generating images that worked successfully.
session_start();
$permitted_chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
function generate_string($input, $strength = 10) {
$input_length = strlen($input);
$random_string = '';
for($i = 0; $i < $strength; $i++) {
$random_character = $input[mt_rand(0, $input_length - 1)];
$random_string .= $random_character;
}
return $random_string;
}
$image = imagecreatetruecolor(130, 40);
imageantialias($image, true);
$colors = [];
$red = rand(125, 175);
$green = rand(125, 175);
$blue = rand(125, 175);
for($i = 0; $i < 5; $i++) {
$colors[] = imagecolorallocate($image, $red - 20*$i, $green - 20*$i, $blue - 20*$i);
}
imagefill($image, 0, 0, $colors[0]);
for($i = 0; $i < 10; $i++) {
imagesetthickness($image, rand(2, 10));
$line_color = $colors[rand(1, 4)];
imagerectangle($image, rand(-10, 190), rand(-10, 10), rand(-10, 190), rand(40, 60), $line_color);
}
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
$textcolors = [$black, $white];
$fonts = [dirname(__FILE__).'/wp-content/themes/theme_name/fonts/ttf/dana-fanum-black.ttf'];
$string_length = 5;
$captcha_string = generate_string($permitted_chars, $string_length);
$_SESSION['captcha_text'] = $captcha_string;
for($i = 0; $i < $string_length; $i++) {
$letter_space = 120/$string_length;
$initial = 15;
imagettftext($image, 24, rand(-15, 15), $initial + $i*$letter_space-10, rand(25, 45), $textcolors[rand(0, 1)], $fonts[array_rand($fonts)], $captcha_string[$i]);
}
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
And in my form show an image and text field.
<img data-no-lazy="1" src="/captcha.php" alt="CAPTCHA" class="captcha-image">
<input type="text" id="captcha" name="captcha_challenge" pattern="[A-Za-z]{5}" required="" placeholder="">
when i get $_SESSION['captcha_text'] in my wordpress ajax function this have null value.
session_start();
var_dump($_SESSION['captcha_text']);
/* return null */

Php captcha session variable not found

I have this captcha.php file that I load on the page I want the captcha to appear on using jQuery load.
In the captcha.php file I start a new session and I set a variable (theCaptchaCode) which contains the code you see on the image, but when I try to get the session variable with $theCaptchaCode = $_SESSION['theCaptchaCode']. It says
undefined index: theCaptchaCode.
Captcha.php code:
<?php
//Killing previous session
session_start();
session_unset();
session_destroy();
?>
<?php
session_start();
$theCaptchaCode = "";
$thisCaptchaImg = imagecreatetruecolor(200, 50);
$color = imagecolorallocate($thisCaptchaImg, 0, 0, 0);
$dotColor = imagecolorallocate($thisCaptchaImg, 46, 46, 46);
$backgroundColor = imagecolorallocate($thisCaptchaImg, 255, 255, 255);
imagefilledrectangle($thisCaptchaImg, 0, 0, 200, 70, $backgroundColor);
$characters = '123456789QWERTYUIOPASDFGHJKLZXCVBNM';
$length = strlen($characters);
for ($l = 0; $l < 4; $l++)
{
imageline($thisCaptchaImg, 0, rand() % 50, 200, rand() % 50, $color);
}
for ($d = 0; $d < 1500; $d++)
{
imagesetpixel($thisCaptchaImg, rand() % 200, rand() % 50, $dotColor);
}
for ($c = 0; $c < 7; $c++)
{
$selCharacter = $characters[rand(0, $length - 1)];
imagestring($thisCaptchaImg, 5, 5 + ($c * 30), 20, $selCharacter, $color);
$theCaptchaCode .= $selCharacter;
}
imagepng($thisCaptchaImg, 'thisCaptchaImage.png');
$_SESSION['theCaptchaCode'] = $theCaptchaCode;
session_destroy();
?>
At the end of your code remove the session_destroy() method.
Edited:
Use the below code:
<?php session_start();
$theCaptchaCode = "";
$thisCaptchaImg = imagecreatetruecolor(200, 50);
$color = imagecolorallocate($thisCaptchaImg, 0, 0, 0);
$dotColor = imagecolorallocate($thisCaptchaImg, 46, 46, 46);
$backgroundColor = imagecolorallocate($thisCaptchaImg, 255, 255, 255);
imagefilledrectangle($thisCaptchaImg, 0, 0, 200, 70, $backgroundColor);
$characters = '123456789QWERTYUIOPASDFGHJKLZXCVBNM';
$length = strlen($characters);
for($l=0;$l < 4;$l++)
{
imageline($thisCaptchaImg, 0, rand()%50, 200, rand()%50, $color);
}
for($d=0;$d < 1500;$d++)
{
imagesetpixel($thisCaptchaImg, rand()%200, rand()%50, $dotColor);
}
for($c=0;$c < 7;$c++)
{
$selCharacter = $characters[rand(0, $length-1)];
imagestring($thisCaptchaImg, 5, 5+($c*30), 20, $selCharacter, $color);
$theCaptchaCode .= $selCharacter;
}
imagepng($thisCaptchaImg, 'thisCaptchaImage.png');
$_SESSION['theCaptchaCode'] = $theCaptchaCode;
?>

Categories