2012-01-12 3 views
0

다른 페이지에서 세션 변수를 설정하는 데 문제가 있습니다.세션 변수 범위가 작동하지 않습니다.

Captcha 클래스 (CaptchaSecurityImages.php By Simon Jarvis)와 코드가 약간 날짜가 있지만 (최종 업데이트 07/02/07) 로컬 컴퓨터에서 작동하지만 GoDaddy 서버에서는 작동하지 않습니다.

이것은 captcha 코드를 생성하고 세션 변수를 설정하기 위해 호출되는 함수입니다. '$ _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); 

이 마지막 두 줄과 내가 게시 된 코드와 $_SESSION['captcha_word'] = $code;의 첫 번째 부분 session_start();를 교체합니다.

+0

Nice - thanks! 나를 위해 일한다. – Ourx

관련 문제