我有以下代碼:
<?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);
?>
的問題是,當我運行第一次測試-captcha.php我收到以下錯誤: 未定義的索引:第17行中的test-captcha.php中的random_number($ _SESSION ['random_number']未設置!!!!)
刷新頁面後$ _SESSION ['RANDO m_number']顯示前面的驗證碼文本! 有沒有人有任何想法,爲什麼會發生這種情況??????
嘗試在隨機數中設置$ _SESSION test-captcha.php不在get_captcha.php –
同樣的問題在這裏:),我搜索了很多關於這個解決方案,但沒有辦法 –