houdriwiokug houdriwiokug - 3 months ago 19
PHP Question

PHP Does Not Make The Captcha Image

I'm having a problem with the PHP image creation that contains captcha code. Basically it used to work fine but just now when I opened the page ,I suddenly saw the image does not appear for the first time. And this happened actually for the first time. Here's the

index.php
file which contains the html code:

<div class="wrapper">
<form name="counter"><input type="text" size="8" name="d2"></form>
<form class="login" method="POST" action="">
<p class="title">Are you human?</p>
<div id="captchaimage">
<img src="generate.php">
</div>
<input type="text" name="scr" size="6" placeholder="Write what you see here" autofocus/>
<a href="http://zite.pouyavagefi.com/helpcenter.php">Can not read the code?</a>
<input class="submititon" type="submit" value="Submit" name="submit"></input>
</form>
<footer><a target="_blank" href="http://www.zite.pouyavagefi.com">Zite v2.0</a></footer>
</p>
</div>


And this is
generate.php
file that creates the captcha:

<?php
session_start();
header('Content-type: image/jpeg');

$text = $_SESSION['secure'];
$font_size = 26;
$image_width = 100;
$image_height = 40;

$image = imagecreate($image_width, $image_height);
imagecolorallocate($image,255,255,255);
$text_color = imagecolorallocate($image,0,0,0);

for($x=1;$x<=30;$x++){
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);

imageline($image, $x1, $y1 ,$x2 ,$y2, $text_color);
}

imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpeg($image);
?>


And this the print screen of my webpage:

print screen

Seriously I don't what on earth is going on because everything looks perfect and it used to work properly! So if you know what's the problem ,please let me know. Thanks

Answer

Your problem is here:

    <?php 
session_start();
header('Content-type: image/jpeg');

This gap before the <?php is causing the session_start to throw an error. You need to have NO SPACES OR OTHER BROWSER OUTPUT before the session_start (and other header) function(s).

You are also in effect passing off 4/5 whitespaces as the start of an image file data stream which will obviously result in a failure for any image to display correctly.