/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP Session Not Working for CAPTCHA Script

Hey everyone, I’ve followed some tutorials and created my own CAPTCHA script. The problem with my script is that, for some reason, the SESSION variable string is always different from the string that is displayed in the image, meaning that the form will never validate. What is wrong with this code? If I echo $_SESSION[‘captcha’] and $_POST[‘captcha’], it seems that $_SESSION[‘captcha’] is an old $chars from the last time i visited captcha.php. I seriously cannot figure this problem out, does anyone see what I might have done wrong? I believe the problem has something to do with the session variable not updating instantly, but how do I get this to happen? Thanks for your time.

Actual file:

[code=php]<?
session_start();
include(“/home/tusserte/library/includes/headerhtml.php”);
include(“/home/tusserte/library/validate.php”);
if(!$validform) {
include(“/home/tusserte/library/includes/signupform.php”);
}
include(“/home/tusserte/library/includes/footerhtml.php”);
?>[/code]

Important part of signupform.php:

[code=html]<td colspan=”3″>
<img src=”http://www.thebiasphere.org/captcha.php” alt=”Type the text you see in this image” />
</td>
</tr>
<tr>
<td>
Type the 6 character code in the image above:
</td>
<td colspan=”2″ class=”input”>
<input type=”text” name=”captcha” id=”captcha” />
</td>[/code]

captcha.php file:

[code=php]<?
session_start();
header(“Content-type: image/jpeg”);
header(“Cache-Control: no-cache, must-revalidate”);
function create_font_image($size, $angle, $font, $char) {
$bx=mt_rand(10,20);
$by=mt_rand(60,70);
$bx2=mt_rand(250,280);
$by2=mt_rand(0,75);
$confuse1=substr(md5(time()), 0, 5);
$confuse1size=6;
$bg=mt_rand(1,3);
$bg=”/home/tusserte/library/captchabg”.$bg.”.png”;
$im=imagecreatefrompng($bg);
$i=0;
while ($i<4) {
$x1=mt_rand(0,300);
$x2=mt_rand(0,300);
$colorval=mt_rand(0,255);
$color=imagecolorallocate($im, $colorval, $colorval, $colorval);
imageline($im, $x1, 3, $x2, 72, $color);
$i++;
}
$colorval=mt_rand(0,50);
$color=imagecolorallocate( $im, $colorval, $colorval, $colorval );
imagettftext($im, $size, $angle, $bx, $by, $color, $font, $char);
imagettftext($im, $confuse1size, $angle, $bx2, $by2, $color, $font, $confuse1);
imagefilter($im, IMG_FILTER_EMBOSS);
imagejpeg($im);
imagedestroy($im);
}

$chars= md5(time() * microtime() / date(“myd”));
$chars=substr($chars,1,6);
$_SESSION[‘captcha’]=$chars;
$angle=mt_rand(-3,3);
$size=mt_rand(45,50);
create_font_image($size, $angle, “/home/tusserte/library/BorisBlackBloxx.ttf”, $chars);
?>[/code]

The script in action:
[url]http://www.thebiasphere.org/signup.php[/url]
The form will send you to a page that simply displays “$_POST[‘captcha’] <hr /> $_SESSION[‘captcha’]”

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @Tusserte spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...