Jump to content

[SOLVED] Displaying dynamicly created images


MmmVomit

Recommended Posts

I'm working on the registration page of my website, and am building in a system where the user has to read something out of a dynamically created image.  I can create the image just fine, but how do I get it to display in a web page?

 

Here's what I've figured out to do on my own.

 

<?php
// mainpage.php

session_start()

$_SESSION['capcha_string'] = 'capcha';
?>

<html><head><title>
Main Page
</title></head>

<body>

<p>Capcha</p>

<img src="image.php">

</body></html>

 

<?php

// image.php

session_start();
header("Content-type: image/gif");

$img = imagecreate();

/*
snip. 

print $_SESSION['capcha_string'] in $img
do more image manipulation
*/

imagegif($img);
imagedestroy($img);

?>

Now, this works, but it seems very kludgy.  Is there a better way to do this that I'm simply missing?

 

I checked the faq/code repository and didn't find anything.

Okay, edited.

 

That wasn't my question, though.  I called the header correctly in the live code, I just forgot to include it in my post.

 

My question is if there's a better way to do it than having separate php files and passing data via the $_SESSION array.

Okay, I think I solved it.

 

<?php

// image.php

session_start();
header("Content-type: image/gif");

$img = imagecreate();

$rand_string = md5(rand());
$_SESSION['capcha'] = $rand_string;

/*
snip. 

print $rand_string in image

do more image manipulation
*/

imagegif($img);
imagedestroy($img);

?>

The string in the image is stored in the session data, and this can be checked against what was entered in the form.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.