【发布时间】:2012-02-08 12:05:32
【问题描述】:
我在尝试从另一个页面设置会话变量时遇到问题。
我有一个验证码类(Simon Jarvis 的 CaptchaSecurityImages.php),虽然代码可能有点过时(上次更新时间是 07/02/07),但它可以在我的本地机器上运行,而不是在我的 GoDaddy 服务器上运行。
这是生成验证码和设置会话变量的函数。我稍微修改了代码以允许设置自定义会话变量名称 ($sessionvar)
<?php
session_start();
class CaptchaSecurityImages {
var $font = 'monofont.ttf';
function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = '23456789bcdfghjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}
function CaptchaSecurityImages($width='120',$height='40',$characters='6',$sessionvar='security_code') {
$code = $this->generateCode($characters);
if (!isset($_SESSION)) { session_start(); }
$_SESSION[$sessionvar] = $code; // doesnt actually set anything
/* font size will be 75% of the image height */
$font_size = $height * 0.75;
$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
}
}
$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';
$sessionvar = isset($_GET['session']) ? $_GET['session'] : 'security_code';
$captcha = new CaptchaSecurityImages($width,$height,$characters, $sessionvar);
// if the next line was enabled, the session would be set
//echo $_SESSION[$sessionvar];
?>
调用它的页面如下所示:
<?php
if (!isset($_SESSION)) { session_start(); }
echo $_SESSION['paysecuritycode']; // should output something when the page is refreshed
?>
<html>
...
<body>
<img src='captchacode.inc.php?width=100&height=40&characters=5&session=paysecuritycode'; />
</body>
</html>
所以当 echo $_SESSION['paysecuritycode'] 被调用时没有显示?少了什么东西?是否可以从函数而不是类中设置变量?
也许我应该仔细检查任何服务器配置问题?虽然在 .php 代码中创建会话变量确实有效 - 所以不确定这是为什么?
我是否需要了解任何会话/PHP 要求才能完成这项工作??
【问题讨论】:
-
你在哪里调用函数?除非您更改默认值,否则您的
$_SESSION中的键不正确 -
session_start()应该放在脚本的顶部,在任何 HTML 发送到客户端之前 -
猜猜这会调用函数...
-
就像@Phil 所说,通过在脚本顶部调用
session_start()来确保您的会话有效。 -
djot 是对的,调用captchacode.inc.php页面时调用该函数。该文件在页面顶部和 HTML 文件的顶部都有 session_start() (对不起,把它写在我的头上)。是否可以从类中设置会话变量?还是有什么特别的事情需要做?
标签: php session variables session-variables captcha