【问题标题】:Php captcha session variable not found未找到 PHP 验证会话变量
【发布时间】:2015-09-23 16:48:50
【问题描述】:

我在页面上加载了这个captcha.php 文件,我希望使用jQuery 加载验证码。

captcha.php 文件中,我启动了一个新会话并设置了一个变量(theCaptchaCode),其中包含您在图像上看到的代码,但是当我尝试使用$theCaptchaCode = $_SESSION['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();
?>

【问题讨论】:

    标签: php jquery session captcha


    【解决方案1】:

    在代码末尾删除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
    • @Jojo01,页面开头只有一个 session_start() 并从代码中删除所有其他会话功能,因为每个 session_start 和销毁您都在重置会话。
    • 我在出现验证码的页面的开头只放了一个 session_start() 但我又得到了:未定义的索引:theCaptchaCode
    • Undefined index: theCaptchaCode ,这个错误是出现在同一页面上还是出现在另一个页面上?还有另一个建议是在&lt;?php 标记之后的一个空格处开始会话。如果它出现在另一个页面上,那么在该页面上,您还需要将 session_start() 放在 `
    • 在同一页面上,当我尝试获取变量的内容以检查它是否与用户输入的内容相对应。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 2013-03-18
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多