繁体   English   中英

验证码没有出现在php中

[英]Captcha is not showing up in php

我正在尝试创建一个验证码,它从我命名为dictionary.txt的文件中获取单词,并使用GD库将它们转换为验证码图像。 但有些事情是错的。 我可以将我的字典短语显示为会话变量,但它们不会生成图像。 所以它必须是GD Library代码。 有一个名为captcha_validate.php的最终目标文件,但现在并不重要。 我只是想让图像工作。

form_captcha.php

<form class="" action="captcha_validate.php" method="post">
    Captcha Verify:
    <input type="text" name="captcha" value="" /><br /><br />
    <img src="captcha.php" /><br />
    <input type="submit" name="submit" value="Submit" />
</form>

captcha.php

    <?php

    session_start();

    session_regenerate_id();

    $file = file('dictionary.txt', FILE_IGNORE_NEW_LINES);

    $length = count($file) - 1;
    $random = rand(0, $length);

    $_SESSION['captcha'] = $file[$random];
    session_write_close();

    function msg($msg) {
        $container = imagecreate(250, 170);
        $black = imagecolorallocate($container, 0, 0, 0);
        $white = imagecolorallocate($container, 255, 255, 255);
        $font = "C:\Windows\Fonts\arial.ttf";
        imagefilledrectangle($container, 0, 0, 250, 170, $black);

        $px = (imagesx($container) / (strlen($msg)/1.15));
        $py = (imagesy($container) / 3.5);

        imagefttext($container, 28, -27, $px, $py, $white, $font, $msg);
        header("Content-type: image/png");
        imagepng($container, null);
        imagedestroy($container);
    }

    msg($file[$random]);

     ?>

尝试测试字体路径是否设置正确。 我测试了你的脚本,当我为我的环境设置正确的字体路径时,它工作。

喜欢:

$ font ='./ SomeFont.ttf';

好吧事实证明我在captcha.php文件中使用了html,在这种情况下我不应该使用它。 这是我的代码无效的主要原因。 感谢您的努力,任何评论或回答的人。

暂无
暂无

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

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