my captcha image not showing on my page when the captcha.php is called

谁都会走 提交于 2019-12-25 03:56:24

问题


i am trying to create my own captcha on my page for log in but this php code won't display my captcha image on my web page. so any suggetion will be good. this is my 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);
    ?>

and my html code to display the captcha image it is trial code.

<!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>

回答1:


There is definitely some problem with your ttf font. Try using another font file or either the font size is too large or too small. I tested your script with another font Akashi.ttf and it works flawlessly. Here is the screen capture:




回答2:


try this

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

or

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


来源:https://stackoverflow.com/questions/24115285/my-captcha-image-not-showing-on-my-page-when-the-captcha-php-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!