Php captcha session variable not found - php

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;
?>

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

How to validate my PHP captcha case insensitive?

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();
?>

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 */

Is there a maxium amount of "Draws" in PHP GD

As the title says is there a max amount of objects you can draw using the GD library in PHP. Given the code below i can only get about 140 squares to appear. The line and square at the bottom only show if you don't max out the grid first. Anyone know of another way to get this done?
<?php
header('Content-type: image/png');
$png_image = imagecreate(3000, 3000);
imagecolorallocate($png_image, 15, 142, 210);
$gridx = 100;
$gridy = 100;
$count = 0;
$currentx = 0;
$currenty = 0;
function makeRec($xpos, $ypos, $c1, $c2, $c3) {
global $png_image, $currentx, $currenty;
imagefilledrectangle($png_image, $xpos, $ypos, $xpos+10, $ypos+10, imagecolorallocate($png_image, $c1, $c2, $c3));
$currentx = $xpos+11;
}
function makexLine($xpos, $ypos) {
global $png_image, $currentx, $currenty;
imageline($png_image, $xpos, $ypos, $xpos, $ypos+10, imagecolorallocate($png_image, 0, 0, 0));
$currentx = $xpos+1;
}
function makeyLine($xpos, $ypos) {
global $png_image, $currentx, $currenty;
imageline($png_image, $xpos, $ypos, $xpos+100, $ypos, imagecolorallocate($png_image, 0, 0, 0));
$currenty = $ypos+1;
}
for($y = 0; $y < $gridy; $y++) {
makeyline($currentx, $currenty);
for($x = 0; $x < $gridx; $x++) {
makexLine($currentx, $currenty);
if($count == 0) {
makeRec($currentx, $currenty, 255, 1, 255);
$count++;
}
else {
makeRec($currentx, $currenty, 255, 255, 255);
$count = 0;
}
}
makexLine($currentx, $currenty);
$currentx = 0;
$currenty = $currenty + 11;
}
makeyline(0, 200);
makeRec(200, 200, 255, 1, 255);
imagepng($png_image);
imagedestroy($png_image);
?>
Running a test script on my system that draws 1 million squares with imagefilledrectangle() in a chessboard pattern works fine (and surprisingly quickly). If there is a draw limit it's at least that high.
Your problem seems to be too many calls to imagecolorallocate(). Below I've modified your code to call imagecolorallocate() only once per colour:
header('Content-type: image/png');
$png_image = imagecreate(3000, 3000);
imagecolorallocate($png_image, 15, 142, 210);
$magenta = imagecolorallocate($png_image, 255, 1, 255);
$black = imagecolorallocate($png_image, 0, 0, 0);
$white = imagecolorallocate($png_image, 255, 255, 255);
$gridx = 100;
$gridy = 100;
$count = 0;
$currentx = 0;
$currenty = 0;
function makeRec($xpos, $ypos, $colour) {
global $png_image, $currentx, $currenty;
imagefilledrectangle($png_image, $xpos, $ypos, $xpos+10, $ypos+10, $colour);
$currentx = $xpos+11;
}
function makexLine($xpos, $ypos, $colour) {
global $png_image, $currentx, $currenty;
imageline($png_image, $xpos, $ypos, $xpos, $ypos+10, $colour);
$currentx = $xpos+1;
}
function makeyLine($xpos, $ypos, $colour) {
global $png_image, $currentx, $currenty;
imageline($png_image, $xpos, $ypos, $xpos+100, $ypos, $colour);
$currenty = $ypos+1;
}
for($y = 0; $y < $gridy; $y++) {
makeyline($currentx, $currenty, $black);
for($x = 0; $x < $gridx; $x++) {
makexLine($currentx, $currenty, $black);
if($count == 0) {
makeRec($currentx, $currenty, $magenta);
$count++;
}
else {
makeRec($currentx, $currenty, $white);
$count = 0;
}
}
makexLine($currentx, $currenty, $black);
$currentx = 0;
$currenty = $currenty + 11;
}
makeyline(0, 200, $black);
makeRec(200, 200, $magenta);
imagepng($png_image);
imagedestroy($png_image);

Trying to add text into an image

I'm trying to add 2 line of text. One for the name of the person and the other for the name of the clan.
Could you help me of what I'm doing wrong? I don't get any error message or anything and the image is loading fine.
This is my code:
//Ribbons Image
$ribbons = array(
"ribbons/1STAID.png",
"ribbons/BMT.png",
"ribbons/BRA1A.png",
"ribbons/CCMD.png",
"ribbons/CCMDV2.png",
"ribbons/donator.png",
"ribbons/FTR.png",
"ribbons/GC.png",
"ribbons/GRA1A.png",
"ribbons/IOTP.png",
"ribbons/MEDIC.png",
"ribbons/PHA1A.png",
"ribbons/PILOT.png",
"ribbons/RCT1A.png",
"ribbons/RCT2A.png",
"ribbons/RCT3A.png",
"ribbons/RCT4A.png",
"ribbons/SRA1A.png",
"ribbons/SVR1A.png",
"ribbons/SVR2A.png",
"ribbons/SVR3A.png",
"ribbons/SVR4A.png",
"ribbons/SVR-ALTIS.png",
"ribbons/XOCMD.png",
"ribbons/XOCMDV2.png");
//Rank Image
$rank = imagecreatefrompng("rank/2LT.png");
//Background
$frame = imagecreatefrompng("sign3.png");
//imagecopymergy(output,image,x,y,0,0,w,h,100)
//Adding rank
imagecopymerge($frame, $rank, 30, 30, 0, 0, 10, 25, 100);
//Trying to add the text on the image
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = 'Test...';
$font = 'arial.ttf';
imagettftext($im, 20, 0, 10, 20, $white, $font, $text);
//Adding the ribbon on the image
$z = 0;
$i = 0;
for ($y = 0; $y <= 120; $y += 20) {
$z++;
for ($x = 0; $x <= 150; $x += 50) {
if ($i <= 24) {
$rib = imagecreatefromjpeg($ribbons[$i]);
imagecopymerge($frame, $rib, $x + 375, $y + 50, 0, 0, 50, 20, 100);
}
$i++;
}
}
//Save the image + Display
imagepng($frame, 'generate/test.png');
imagepng($frame);
header('Content-Type: image/png');
Thanks for helping me!
Working code:
//Ribbons Image
$ribbons = array(
"ribbons/1STAID.png",
"ribbons/BMT.png",
"ribbons/BRA1A.png",
"ribbons/CCMD.png",
"ribbons/CCMDV2.png",
"ribbons/donator.png",
"ribbons/FTR.png",
"ribbons/GC.png",
"ribbons/GRA1A.png",
"ribbons/IOTP.png",
"ribbons/MEDIC.png",
"ribbons/PHA1A.png",
"ribbons/PILOT.png",
"ribbons/RCT1A.png",
"ribbons/RCT2A.png",
"ribbons/RCT3A.png",
"ribbons/RCT4A.png",
"ribbons/SRA1A.png",
"ribbons/SVR1A.png",
"ribbons/SVR2A.png",
"ribbons/SVR3A.png",
"ribbons/SVR4A.png",
"ribbons/SVR-ALTIS.png",
"ribbons/XOCMD.png",
"ribbons/XOCMDV2.png");
//Rank Image
$rank = imagecreatefrompng("rank/2LT.png");
//Background
$frame = imagecreatefrompng("sign3.png");
//imagecopymergy(output,image,x,y,0,0,w,h,100)
//Adding rank
imagecopymerge($frame, $rank, 30, 30, 0, 0, 10, 25, 100);
//Trying to add the text on the image
$im = imagecreatetruecolor(50, 30);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 0, 82, 255);
imagefilledrectangle($im, 4, 4, 50, 25, $white);
$text = 'Nato Military Corp';
$font = 'arial.ttf';
imagettftext($frame, 12, 0, 450, 220, $white, $font, $text);
//imagecopymerge($frame, $im, 10, 20, 0, 0, 10, 25, 100);
//Adding the ribbon on the image
$z = 0;
$i = 0;
for ($y = 0; $y <= 120; $y += 20) {
$z++;
for ($x = 0; $x <= 150; $x += 50) {
if ($i <= 24) {
$rib = imagecreatefromjpeg($ribbons[$i]);
imagecopymerge($frame, $rib, $x + 375, $y + 50, 0, 0, 50, 20, 100);
}
$i++;
}
}
//Save the image + Display
imagepng($frame, 'generate/test.png');
imagepng($frame);
header('Content-Type: image/png');

Categories