Log in

View Full Version : Empty Session Variable



HFard
04-26-2009, 12:19 AM
I set and give value to a session variable in one module, but when I go to another module, it is empty.
From the caller module (CaptchaIn.htm), I access a module (CaptchaGen.php) which sets a session variable (captcha). But when I go to another module (CaptchaProc.php) to work with that session variable, it is empty.

Any help is highly appreciated.



CaptchaIn.htm
-------------
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</HEAD>
<BODY>
<FORM ACTION="CaptchaProc.php" METHOD="post">
<img src="CaptchaGen.php" />
<INPUT TYPE="text" NAME="captcha">
<BR>
<INPUT TYPE="submit" VALUE="Check It">
</FORM>
</BODY>
</HTML>

CaptchaGen.php
--------------
<?php
session_start();
$_SESSION['captcha'] = '';
$captcha_chars = array('2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','J','K','M','N','P','Q','R','S','Y','U','V','W','X','Y','Z');
for($i = 0; $i < 5; $i++){
$rand_char = rand(0, (count($captcha_chars) -1));
$_SESSION['captcha'] .= $captcha_chars[$rand_char];
}
$im = imagecreate(200, 60);
$bg_color = imagecolorallocate($im, 0, 122, 87);
$font_color = imagecolorallocate($im, 0, 0, 0);
$characters = preg_split("//", $_SESSION['captcha'], -1, PREG_SPLIT_NO_EMPTY);
$x = 5;
foreach($characters as $char){
$y = rand(25,45);
$size = rand(16,28);
$rotation = rand(-17,17);
imagettftext($im, $size, $rotation, $x, $y, $font_color, './arial.ttf', $char);
$x += 38;
}
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>

CaptchaProc.php
---------------
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</HEAD>
<body>
<?php
echo "Session Value = " . $_SESSION['captcha'] . "<br />";
echo "Input Value = " . $_POST['captcha'] . "<br />";
if($_POST['captcha'] != $_SESSION['captcha']){die('Incorrect Captcha Entry');}
else {echo "Correct Captcha Entry";}
?>
</body>
</HTML>