我在尝试从另一个页面设置会话变量时遇到问题。
我有一个验证码类(CaptchaSecurityImages.php作者Simon Jarvis)虽然代码可能有点过时(最后更新时间为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要求才能使其工作?
答案 0 :(得分:4)
好的,我找到了以下解决方案(不喜欢加载完整框架的想法,但它有效)。也许别人觉得这很有用:
<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../..' ));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$session =& JFactory::getSession();
$session->set('security_code', $code);
将session_start();
替换为我发布的代码的第一部分,将$_SESSION['captcha_word'] = $code;
替换为最后两行。
答案 1 :(得分:1)
我假设您正在设置和阅读不同的会话变量。您正在设置$_SESSION[$sessionvar] = $code;
,但尝试阅读$_SESSION['paysecuritycode']
。
仅当$_SESSION['paysecuritycode']
设置为$sessionvar
时,'paysecuritycode'
才为空,但默认设置为'security_code'
。
此外,if (!isset($_SESSION)) { session_start(); }
在第一个脚本中没用,因为如果会话尚未启动(标题已由header('Content-Type: image/jpeg');
发送),它将发出警告。