Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.