会话变量作用域不起作用

发布于 2024-12-26 11:59:03 字数 3287 浏览 4 评论 0原文

我在尝试从另一个页面设置会话变量时遇到问题。

我有一个验证码类(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&amp;height=40&amp;characters=5&amp;session=paysecuritycode'; />
</body>
</html>

那么当调用 echo $_SESSION['paysecuritycode'] 时没有显示?缺少什么?当然可以从函数而不是类中设置变量吗?

也许有什么服务器配置问题需要我仔细检查?虽然创建会话变量在 .php 代码中确实有效 - 所以不确定这是为什么?

为了使这项工作正常进行,我需要注意任何会话/PHP 要求吗?

I am having problems trying to set session variables from another page.

I have a captcha class (CaptchaSecurityImages.php By Simon Jarvis) and though the code might be a bit dated (last updated 07/02/07), it works on my local machine and not on my GoDaddy server.

This is the function called to generate a captcha code and to set a session variable. I modified the code a little bit to allow custom session variables names to be set ($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];

?>

The page calling this looks something like this:

<?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>

So when echo $_SESSION['paysecuritycode'] is called there's no display? Whats missing? Is it possible to set variables from a function instead a class, surely?

Perhaps is there any server configuration issues that I should double check? Though creating session variables do work when inside .php code - so not sure why this is?

Are there any session/PHP requirements I need to be aware of to make this work??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与他有关 2025-01-02 11:59:03

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

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

Ok, I found following solution (don't like the idea to load the complete framework, but it works). Maybe someone else finds this useful:

<?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);

Replace session_start(); with the first part of the code I posted and $_SESSION['captcha_word'] = $code; with the last two lines.

时常饿 2025-01-02 11:59:03

我假设您正在设置和读取不同的会话变量。您正在设置 $_SESSION[$sessionvar] = $code;,但尝试读取 $_SESSION['paysecuritycode']
仅当 $sessionvar 设置为 'paysecuritycode' 时,$_SESSION['paysecuritycode'] 不为空,但默认设置为 <代码>“安全代码”。

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

I assume that you are setting and reading different session variables. You are setting $_SESSION[$sessionvar] = $code;, but trying to read $_SESSION['paysecuritycode'].
$_SESSION['paysecuritycode'] is not empty only when $sessionvar is set to 'paysecuritycode', but by default it is set to 'security_code'.

Besides, if (!isset($_SESSION)) { session_start(); } is useless in the first script as it will raise warning if the session is not started yet (headers already sent by header('Content-Type: image/jpeg');).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文