繁体   English   中英

找不到PHP验证码会话变量

[英]Php captcha session variable not found

我在页面上加载了这个captcha.php文件,我希望使用jQuery加载来显示captcha。

captcha.php文件中,我开始一个新会话,并设置一个变量( theCaptchaCode ),其中包含您在图像上看到的代码,但是当我尝试使用$theCaptchaCode = $_SESSION['theCaptchaCode']获取会话变量时。 它说

未定义的索引: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();
?>

在代码末尾,删除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;
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM