2012-01-12 3 views
0

セッション変数を別のページから設定しようとするときに問題が発生しています。セッション変数のスコープが機能しない

私はCaptchaクラス(CaptchaSecurityImages.php By Simon Jarvis)ですが、コードは少し古いかもしれません(最終更新日07/02/07)。ローカルマシンで動作し、GoDaddyサーバーでは動作しません。

これは、キャプチャコードを生成し、セッション変数を設定するために呼び出される関数です。 「$ _SESSION [エコーとき

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

ので:私は

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

?> 

ページが呼び出し、これは次のようになります($のsessionvar)カスタムセッション変数名を設定することができるように、コードを少し修正しましたpaysecuritycode ']と表示されていますか?何が欠けている?クラスから関数に代わって変数を設定することは可能でしょうか?

おそらく、私が再確認する必要のあるサーバー構成上の問題はありますか?セッション変数の作成は.phpコードの中で動作しますが、これがなぜなのか分かりません。

私はこの作業を行うために注意する必要があるセッション/ PHPの要件はありますか?

+0

ここで、関数を呼び出していますか?デフォルト値を変更していない場合は、 '$ _SESSION'に不正なキーがあります – cspray

+1

' session_start() 'はスクリプトの先頭に移動してから、HTMLがクライアントに送信されます。 – Phil

+1

これは関数を呼び出します。 を読み取ろうとしています。 $_SESSION['paysecuritycode']$sessionvar'paysecuritycode'に設定されている場合にのみ空ではありませんが、デフォルトでは'security_code'に設定されています。

さらに、最初のスクリプトではif (!isset($_SESSION)) { session_start(); }が役に立たなくなっています。セッションがまだ開始されていない場合は警告が表示されます(ヘッダーは既にheader('Content-Type: image/jpeg');によって送信されています)。

+0

は、captchaクラスのコード全体を追加したばかりです。 $ _GET ['session']があり、img urlから検出し、渡されたセッション変数名を設定する必要があります。たとえそれがsecurity_codeにデフォルト設定されていても、クラス内から作成されるセッション変数はないようです。 – Ourx

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

私は最後の2行に掲載し、$_SESSION['captcha_word'] = $code;コードの最初の部分でsession_start();を交換してください。

+0

ニース - ありがとう!私のために働く – Ourx

関連する問題