【问题标题】:my captcha image not showing on my page when the captcha.php is called调用 captcha.php 时,我的验证码图像未显示在我的页面上
【发布时间】:2014-07-29 16:46:01
【问题描述】:

我正在尝试在我的页面上创建自己的验证码以进行登录,但是此 php 代码不会在我的网页上显示我的验证码图像。所以任何建议都会很好。 这是我的 captcha.php

  <?php 
    session_start();

    // generate random number and store in session
    $randomnr = rand(1000, 9999);
    $_SESSION['randomnr2'] = md5($randomnr);
    //generate image
    $im = imagecreatetruecolor(100, 38);
    //colors:
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 200, 35, $black);

    // -------------      your fontname    -------------
    //  example font http://www.webpagepublicity.com/free-fonts/a/Anklepants.ttf
    $font = 'Anklepants.ttf';

    //draw text:
    imagettftext($im, 35, 0, 22, 24, $grey, $font, $randomnr);

    imagettftext($im, 35, 0, 15, 26, $white, $font, $randomnr);

    // prevent client side  caching
    header("Expires: Wed, 1 Jan 2015 00:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revаlidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");

    //send image to browser
    header ("Content-type: image/PNG");
    imagegif($im);
    imagedestroy($im);
    ?>

我的 html 代码显示验证码图像是试用代码。

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>books Collection</title>


    </head>
    <body>
        <div >
        <form>
        <img src="captcha.php" /></br>
        <img src="captcha.png" /></br>
        <input type="text" name="answer" placeholder="Enter captcha here" />
        <input type="submit" value="CHECK" />
      </form>
        </div>
    </body>
    </html>

【问题讨论】:

  • 您的问题在于您的 imagettftext() 函数。看看这个Example

标签: php html css caching fonts


【解决方案1】:

试试这个

<img src="<?= 'captcha.php'; ?>">

<img src="<?php echo 'captcha.php'; ?>">

【讨论】:

    【解决方案2】:

    您的ttf 字体肯定有问题。尝试使用其他字体文件,或者字体太大或太小。我用另一种字体Akashi.ttf 测试了你的脚本,它运行完美。这是屏幕截图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多