Image validation

Discussion in 'PHP' started by Masetek, Jan 26, 2006.

  1. #1
    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
     
    Masetek, Jan 26, 2006 IP
  2. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't know much about this, can you post your url?

    I found a tutorial here
     
    dave487, Jan 27, 2006 IP
  3. Masetek

    Masetek Peon

    Messages:
    820
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    Masetek, Jan 27, 2006 IP
  4. wwm

    wwm Peon

    Messages:
    308
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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 ;)
     
    wwm, Jan 28, 2006 IP
    Masetek likes this.
  5. Masetek

    Masetek Peon

    Messages:
    820
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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?
     
    Masetek, Jan 28, 2006 IP
  6. tccoder

    tccoder Peon

    Messages:
    69
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hmm can u link me to somewhere where the script is uploaded?
     
    tccoder, Jan 28, 2006 IP
  7. wwm

    wwm Peon

    Messages:
    308
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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()
     
    wwm, Jan 29, 2006 IP
  8. wwm

    wwm Peon

    Messages:
    308
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    ...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
     
    wwm, Jan 29, 2006 IP
  9. Masetek

    Masetek Peon

    Messages:
    820
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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 :)
     
    Masetek, Jan 29, 2006 IP
  10. wwm

    wwm Peon

    Messages:
    308
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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:
     
    wwm, Jan 29, 2006 IP
  11. Masetek

    Masetek Peon

    Messages:
    820
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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.
     
    Masetek, Jan 29, 2006 IP
  12. Masetek

    Masetek Peon

    Messages:
    820
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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.

    :D
     
    Masetek, Jan 29, 2006 IP
  13. wwm

    wwm Peon

    Messages:
    308
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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)
     
    wwm, Jan 30, 2006 IP