Does anyone have any experience with working with image validation (to presvent spamming on forms)? I have a script that generates a random image but the image just flicks up on the page for a split second then dissappears and for the life of me I don't know why. Can anyone shed some light on this? Or point me in the right direction for a script that works. Thanks
Yep, thats the tutorial Ive been using. I can't put it up at the moment because my server doesnt have GD installed. I am working on it locally
yea loadz ok heres a sample code i use to generate a random image randomImage.php <?php //start session session_start(); error_reporting(E_ALL); //characters that will be used $alphanum = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; // generate the verication code $rand = substr(str_shuffle($alphanum), 0, 4); // create an image object using the chosen background $image = imagecreatefromgif('images/randomImage.gif'); $textColor = imagecolorallocate($image, 150, 0, 150); // write the code on the background image imagestring ($image, 7, 2, 4, $rand, $textColor); // create the hash for the verification code // and put it in the session $_SESSION['random_number'] = md5($rand); // send several headers to make sure the image is not cached header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified // HTTP/1.1 header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); // HTTP/1.0 header("Pragma: no-cache"); // send the content type header so the image is displayed properly header('Content-type: image/gif'); // send the image to the browser imagegif($image); // destroy the image to free up the memory imagedestroy($image); ?> PHP: now all u need to do is take in a 4character alphanumeric from a form md5 encrypt this string check encrypted string against the session contact me if anything
Thanks for the code, tried it, same problem. The image just ficks up on the screen then dissappears. Wierd. I tested it on another server and it's doing the same thing. Got any suggestions?
happened to me (the graphics library is not usually enabled by default) check the GD library is properly installed!! or enabled if its a windows server anyways the code i gave u will spit out all sorts of errors if something is wrong go to http://www.yoursitesnamehere.com/randomImage.php then paste here what error messages u get also paste your PHPINFO()
...also u need to put this image in your images folder example http://www.yoursitesnamehere.com/images/randomImage.php http://img80.imageshack.us/img80/783/randomimage5sa.gif the script uses that as a background u can make ur own background as well i have never script that also servs random background everytime along with random inages but anyways paste here what error messages u get
OK guys...as far as I know gd is ok... GD Support enabled GD Version bundled (2.0.28 compatible) FreeType Support enabled FreeType Linkage with freetype GIF Read Support enabled GIF Create Support enabled JPG Support enabled PNG Support enabled WBMP Support enabled XBM Support enabled here is the link to the form http://www.masetek.com.au/test/register.php You'l see the image fick up at the bottom of the form. If I add alt="" it wil display the alt text. // create an image object using the chosen background $image = imagecreatefromgif('images/random.gif'); I created my own background image...and thats the name there (random.gif) As far as error messages go, I dont get any
ok can u please post the exact code for random1.php ok i visited http://www.masetek.com.au/test/random1.php and i got these error message (see below) this should show ur image but somethings wrong now seems theres problems with headers send (did u put session_start() twice? is there any blank lines before code?) also i dug up my code, seems even if GD is enabled gif support can be disabled (happened on my server php was compiled with GD but i couldnt generate gif or jpg images) now i learned the hardway that php doesnt come with mysql / sessions / gd compiled with Suse 9.1 Linux (dunno why i had to recompile it myself) Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/masetek/public_html/test/random1.php:2) in /home/masetek/public_html/test/random1.php on line 5 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/masetek/public_html/test/random1.php:2) in /home/masetek/public_html/test/random1.php on line 5 Warning: Cannot modify header information - headers already sent by (output started at /home/masetek/public_html/test/random1.php:2) in /home/masetek/public_html/test/random1.php on line 29 Warning: Cannot modify header information - headers already sent by (output started at /home/masetek/public_html/test/random1.php:2) in /home/masetek/public_html/test/random1.php on line 30 Warning: Cannot modify header information - headers already sent by (output started at /home/masetek/public_html/test/random1.php:2) in /home/masetek/public_html/test/random1.php on line 33 Warning: Cannot modify header information - headers already sent by (output started at /home/masetek/public_html/test/random1.php:2) in /home/masetek/public_html/test/random1.php on line 34 Warning: Cannot modify header information - headers already sent by (output started at /home/masetek/public_html/test/random1.php:2) in /home/masetek/public_html/test/random1.php on line 37 Warning: Cannot modify header information - headers already sent by (output started at /home/masetek/public_html/test/random1.php:2) in /home/masetek/public_html/test/random1.php on line 40 GIF87ax PHP:
God I feel like an idiot! What a rookie mistake There was a blank line at the tp of the code... Thanks for your help wwm, appreciate it. 1 more thing though...do you know how to increase the font size? Have a look at my image and see the size of the font - u can barey read it. It's set as font 5 in imagestring(). http://www.masetek.com.au/test/register.php Thanks.
Nevermind, I got a font for it... $font = imageloadfont("anonymous.gdf"); Here it is... http://www.masetek.com.au/test/register.php now just got to sort out the validation processing and I'm done. Thanks again.
no probs as for the font size, the 2nd argument for imagestring() is the font size http://ie.php.net/imagestring but of course u can use ur own fonts (hmm now that i think of it ill try to find whats that whacky font that google use for their registrations)