2015-05-27 3 views
0

У меня есть этот файл captcha.php, который я загружаю на страницу. Я хочу, чтобы заголовок отображался при использовании загрузки jQuery.Php captcha переменная сеанса не найден

В файле captcha.php я начинаю новую сессию, и я установить переменную (theCaptchaCode), который содержит код, который вы видите на изображении, но когда я пытаюсь получить переменную сеанса с $theCaptchaCode = $_SESSION['theCaptchaCode']. В нем говорится:

undefined index: theCaptchaCode.

captcha.php код:

<?php 
    //Killing previous session 
    session_start(); 
    session_unset(); 
    session_destroy(); 
?> 
<?php 
    session_start(); 
    $theCaptchaCode = ""; 
    $thisCaptchaImg = imagecreatetruecolor(200, 50); 
    $color = imagecolorallocate($thisCaptchaImg, 0, 0, 0); 
    $dotColor = imagecolorallocate($thisCaptchaImg, 46, 46, 46); 
    $backgroundColor = imagecolorallocate($thisCaptchaImg, 255, 255, 255); 
    imagefilledrectangle($thisCaptchaImg, 0, 0, 200, 70, $backgroundColor); 
    $characters = '123456789QWERTYUIOPASDFGHJKLZXCVBNM'; 
    $length = strlen($characters); 
    for ($l = 0; $l < 4; $l++) 
    { 
     imageline($thisCaptchaImg, 0, rand() % 50, 200, rand() % 50, $color); 
    } 
    for ($d = 0; $d < 1500; $d++) 
    { 
     imagesetpixel($thisCaptchaImg, rand() % 200, rand() % 50, $dotColor); 
    } 
    for ($c = 0; $c < 7; $c++) 
    { 
     $selCharacter = $characters[rand(0, $length - 1)]; 
     imagestring($thisCaptchaImg, 5, 5 + ($c * 30), 20, $selCharacter, $color); 
     $theCaptchaCode .= $selCharacter; 
    } 
    imagepng($thisCaptchaImg, 'thisCaptchaImage.png'); 
    $_SESSION['theCaptchaCode'] = $theCaptchaCode; 
    session_destroy(); 
?> 

ответ

0

В конце кода удалить метод session_destroy().

Отредактировано: Используйте следующий код:

<?php session_start(); 
$theCaptchaCode = ""; 
$thisCaptchaImg = imagecreatetruecolor(200, 50); 
$color = imagecolorallocate($thisCaptchaImg, 0, 0, 0); 
$dotColor = imagecolorallocate($thisCaptchaImg, 46, 46, 46); 
$backgroundColor = imagecolorallocate($thisCaptchaImg, 255, 255, 255); 
imagefilledrectangle($thisCaptchaImg, 0, 0, 200, 70, $backgroundColor); 
$characters = '123456789QWERTYUIOPASDFGHJKLZXCVBNM'; 
$length = strlen($characters); 
for($l=0;$l < 4;$l++) 
{ 
    imageline($thisCaptchaImg, 0, rand()%50, 200, rand()%50, $color); 
} 
for($d=0;$d < 1500;$d++) 
{ 
    imagesetpixel($thisCaptchaImg, rand()%200, rand()%50, $dotColor); 
} 
for($c=0;$c < 7;$c++) 
{ 
    $selCharacter = $characters[rand(0, $length-1)]; 
    imagestring($thisCaptchaImg, 5, 5+($c*30), 20, $selCharacter, $color); 
    $theCaptchaCode .= $selCharacter; 
} 
imagepng($thisCaptchaImg, 'thisCaptchaImage.png'); 
$_SESSION['theCaptchaCode'] = $theCaptchaCode; 
?> 
+0

Теперь я получаю Неопределенная переменная: _SESSION – Jojo01

+0

@ Jojo01, только один session_start() в начале страницы и удалить все другие функции сессии из ваш код из-за каждого session_start и уничтожает вас, сброс сеанса. –

+0

проверить отредактированный ответ –

Смежные вопросы