Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problems setting captcha code

    Hi,
    I'm trying to implement a captcha code. This is working fine:
    PHP Code:
    <?php
     
    function imagen() {
    header("Content-type: image/png");
        
    $img imagecreatetruecolor(240,60);
                                
        
    $white imagecolorallocate($img255255255);
        
    $black imagecolorallocate($img000);
        
    $grey imagecolorallocate($img150150150);
        
    $pink imagecolorallocate($img2000150);
        
    $font "./erasbd.ttf"
                                    
        
    imagefill($img00$grey);
                                
        
    $string random_string(rand(7,8));
                                
        
    imagettftext($img2651345$black$font$string);
        
    imagettftext($img2651547$pink$font$string);
        
    imagettftext($img2631849$white$font$string);    

     
    $imagen imagepng($img); 
     
    $fin_magine imagedestroy($img);
     

         if(isset(
    $_FILES[$imagen][$fin_magine])) {
            echo 
    $imagen$fin_magine;
            }  else {
            }
         } 
         
    imagen();

    ?>
    but when trying to use it inside html it will return: The image “http://........./captcha3.php” cannot be displayed because it contains errors.

    here's how I placed it
    PHP Code:
    table
        tr
            td <?php 
             
    function imagen() {
    header("Content-type: image/png");
    $img imagecreatetruecolor(240,60);
                                
        
    $white imagecolorallocate($img255255255);
        
    $black imagecolorallocate($img000);
        
    $grey imagecolorallocate($img150150150);
        
    $pink imagecolorallocate($img2000150);
        
    $font "./erasbd"
                                    
        
    imagefill($img00$grey);
                                
        
    $string random_string(rand(7,8));
                                
        
    imagettftext($img2651345$black$font$string);
        
    imagettftext($img2651547$pink$font$string);
        
    imagettftext($img2631849$white$font$string);    

     
    $imagen imagepng($img); 
     
    $fin_magine imagedestroy($img);
     

         if(isset(
    $_FILES[$imagen][$fin_magine])) {
            echo 
    $imagen$fin_magine;
            }  else {
            }
         } 
         
    imagen();
             
    ?>
          !--  <img src="captcha2.php"  / --
             /td

            /td
            td
                form action"captcha.php" method"post"
                
                input     name="cambiar" type="submit" value="Cambiar"/
            /td
            tr
                    tdEscriba el codigo de la imagen que ve arriba/td
                    /tr
                    tr
                        td <input id="captcha" name="captcha" type="text"/
    /td
                    /tr
    as you can see i commented out <img src="captcha2.php" which I innitially tried but y idea is to place it as a function so I can validate that with the imput value. Please let me know if there's any solution.

    Most appreciated!
    Carlos
    Last edited by Inigoesdr; 09-11-2011 at 08:39 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,539
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Once you've set the content-type for images, you can no
    longer put anything else in the output. Keep the image processing
    all to itself, and reference it only from <img src> tags in other scripts.

    If doing this gets too hard ... try using recaptcha (it's free and easy to use).

    http://www.google.com/recaptcha


    .

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I'd stick the PHP code to create the Captcha image into a seperate file, e.g. captcha.php, then call it using <img src="captcha.php">. If you need to pass values through to it, why not reference them as: <img src="captcha.php?value1=value&value2=value"> ?

    Gerald.

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    If you're doing this for a commercial reason, I would suggest just using recapcha. It's a pretty decent, free library that requires minimal effort.

    But, if it's just a project for your own learning, then it's not a bad one to do. Defining a header content-type makes apache parse the output as that content-type. Meaning, the output of the .php file in your case, is the same as the browser looking at a .jpg. This means that echo's etc. are unsuitable for your file, as your file IS an image being created by the php, not a file that is displaying an image. Like gerald said, if this file was called image.php, you would display the image by using the filepath+image.php in an img tag in html.
    Also, like gerald said, you can insert parameters using the GET URI statements you would normally use, but for a capcha script I doubt they'd be needed.

    As for actually validating the captcha, you could use session variables to set the answer required (setting the session variables in the actual image.php), then on the form processing, compare the form input with the session variables you set.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by catania View Post
    Hi,
    http://........./captcha3.php” cannot be displayed because it contains errors.
    Yes of course it will.

    In your html you always use the <img> tag for displaying images don't you. Captcha images are no different. They must still be placed into the html by an <img> tag.

    If you think about it, when was the last time you looked at html source and saw an actual image in the source code? Never. You'll see a URL to it instead of an image.

    The reason your browser is saying the image contains errors is because outputting it in the middle of the html corrupts the file stream for the image. The image is no longer just jpeg data its also got bits of html inside it which corrupts the file information contained within and thus your browser detects the corruption and doesn't display the image. The same thing happens if you have a seperate script for captcha images but have whitespace before or after the <? and ?> tags.
    I can't really think of anything to write here now...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •