【问题标题】:Session Variable Scope Not Working会话变量范围不起作用
【发布时间】: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&amp;height=40&amp;characters=5&amp;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


【解决方案1】:

好的,我找到了以下解决方案(不喜欢加载完整框架的想法,但它可以工作)。也许其他人觉得这很有用:

<?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; 替换为最后两行。

【讨论】:

    【解决方案2】:

    我假设您正在设置和读取不同的会话变量。您正在设置$_SESSION[$sessionvar] = $code;,但试图读取$_SESSION['paysecuritycode']。 只有$sessionvar设置为'paysecuritycode'$_SESSION['paysecuritycode']才不为空,而是默认设置为'security_code'

    此外,if (!isset($_SESSION)) { session_start(); } 在第一个脚本中毫无用处,因为如果会话尚未启动(标头已由 header('Content-Type: image/jpeg'); 发送),它将引发警告。

    【讨论】:

    • 刚刚添加了验证码类的整个代码。您会看到有一个 $_GET['session'] 应该从 img url 中检测并设置传入的会话变量名称?即使它应该默认为security_code,似乎也没有从类内部创建任何会话变量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 2015-09-30
    • 2013-04-12
    • 2012-08-17
    • 2014-05-16
    相关资源
    最近更新 更多