我有以下代码:
<?php session_name('mysession');
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Captcha</title>
</head>
<body>
Captcha Image:
<img src="get_captcha.php" width="139" height="139" />
<?php
echo 'Captcha text:'.$_SESSION['random_number']
?>
</body>
</html>
get_captcha.php 是:
<?php
session_name('mysession');
session_start();
$string = '';
for ($i = 0; $i < 5; $i++) {
$string .= chr(rand(97, 122));
}
$_SESSION['random_number'] = $string;
$dir = 'fonts/';
$image = imagecreatetruecolor(165, 50);
// random number 1 or 2
$num = rand(1,2);
if($num==1)
{
$font = "Capture it 2.ttf"; // font style
}
else
{
$font = "Molot.otf";// font style
}
// random number 1 or 2
$num2 = rand(1,2);
if($num2==1)
{
$color = imagecolorallocate($image, 113, 193, 217);// color
}
else
{
$color = imagecolorallocate($image, 163, 197, 82);// color
}
$white = imagecolorallocate($image, 255, 255, 255); // background color white
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, $dir.$font, $_SESSION['random_number']);
imagepng($image);
?>
问题是,当我第一次运行 test-captcha.php 时,出现以下错误: Undefined index: random_number in test-captcha.php on line 17 ($_SESSION['random_number'] is not set!!!!)
刷新页面后,$_SESSION['random_number'] 会显示之前的验证码文本!有没有人知道为什么会发生这种情况??????