Problem with captcha image not showing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    Problem with captcha image not showing

    Hi,
    I have a captcha script which should pick up a background image and add some random letters to it and re-display

    This is the part of the form that the captcha image is part of:


    Code:
    <span >Verification Image:</span>
    <span ><img src="captcha.php" id="captcha" />
    <a href="#renew" onclick="javascript: document.getElementById('captcha').src = 'captcha.php?' + Math.random();">refresh</a> </span>
    
    <span >Enter 8 character key:</span>
    <span ><input type="text" name="imgver" id="imgver" onkeyup ="this.value=this.value.toUpperCase()"></span>
    
    <input class="button2" type="submit" value="Give Me Free Entry Now"/>
    And here is the captcha script that is called:

    Code:
    #since we are storing our data using Sessions, we need to start a session
    session_start();
    #$bg_image is the image that will be used for the background of our captcha
    #you will have to replace the value with your bg image.
    $bg_image = "images/cap02.png";
    #we're going to put some lines throughout the image to make it a bit harder for bots to crack
    #to color the lines, we need to fill in the color fields using RGB values (0-255 for each color)
    $line_color = array(
    "R" => 150,
    "G" => 150,
    "B" => 150
    );
    #set the number of line to display in our captcha
    $numLines = 5;
    #set the length of the key to display in our captcha
    $keyLength = 6;
    #set the color of the text in our captcha
    $textcolor = array(
    "R" => 0,
    "G" => 255,
    "B" => 0
    );
    
    #get some file attribures of our bg image, all we are going to use is witdth and height.
    list($width, $height, $type, $attr) = getimagesize($bg_image);
    
    #using PHP's GD Library, we're going to create our base captcha, which starts with our BG image.
    $captcha = imagecreatefrompng($bg_image);
    #sets the color for our key, the color was defined above.
    $keycol = imagecolorallocate($captcha, $textcolor["R"],$textcolor["G"],$textcolor["B"]);
    #start a loop to add our lines to our captcha
    for($i = 0; $i < $numLines; $i++)
    {
        $line = imagecolorallocate($captcha,$line_color["R"],$line_color["G"],$line_color["B"]);
        imageline($captcha,rand(0, $width),rand(0,$height),rand(0, $width),rand(0,$height),$line);
    }
    #generate our random key
    $string = GenKey($keyLength);
    #add our random key to our captcha
    imagestring($captcha, 9, rand(1, 30), rand(1, 15), $string, $keycol);
    #encrypt our key and add it to our session data.
    $_SESSION['key'] = md5($string);
    #send HTTP header to tell client we're going to display an image.
    header("Content-type: image/png");
    #dsplay image
    imagepng($captcha);
    
    function GenKey ($length)
    {
        #define the letter / number that will be used in our key.
        $chars = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
        #start a loop to make the key.
        for($i = 0; $i < $length; $i++)
        {
            #pick a random start place in the string
            $rand_start = rand(1, strlen($chars) - 1);
            #add this character to our key
            $key .= substr($chars, $rand_start, 1);
        }
        #return our key
        return $key;
    }
    ?>
    I have a background image that is here:


    image


    As far as I can see, everything should work but I get nothing displayed at all.

    Can anyone spot my errors ?
    Thanks for helping.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    The captcha.php script is probably printing some errors. Try commenting out the header and navigate to the capatcha.php file in your browser. See if there are any errors.

    Comment

    • jeddiki
      Contributor
      • Jan 2009
      • 290

      #3
      Thanks for your reply, but I am not sure what you mean or how to do it :(

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Line #44 in the code you posted. Remove it.
        You can simply put # or // in front of it to comment it out.

        Then open the script in your browser.


        That should tell you why your image isn't showing like it should.

        Comment

        • jeddiki
          Contributor
          • Jan 2009
          • 290

          #5
          OK ,
          thanks for explaining that.

          It shows an error:
          Notice: Undefined variable: key in /home/chosy/public_html/captcha.php on line 64

          That line 64 refers to:

          $key .= substr($chars, $rand_start, 1);

          So its missing the $key variable,
          Am I supposed to suply that variable somehow ?

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by jeddiki
            OK ,
            thanks for explaining that.

            It shows an error:
            Notice: Undefined variable: key in /home/chosy/public_html/captcha.php on line 64

            That line 64 refers to:

            $key .= substr($chars, $rand_start, 1);

            So its missing the $key variable,
            Am I supposed to suply that variable somehow ?
            Because you haven't defined the variabled, you can't add to it using '.='

            Add $key = ""; to your GenKey method (to the top of it).

            Comment

            • jeddiki
              Contributor
              • Jan 2009
              • 290

              #7
              Thanks Marcus,

              I have dropped it in here: (line 14 - 15 )

              Code:
              #since we are storing our data using Sessions, we need to start a session
              session_start();
              #$bg_image is the image that will be used for the background of our captcha
              #you will have to replace the value with your bg image.
              $bg_image = "images/cap02.png";
              #we're going to put some lines throughout the image to make it a bit harder for bots to crack
              #to color the lines, we need to fill in the color fields using RGB values (0-255 for each color)
              $line_color = array(
              "R" => 150,
              "G" => 150,
              "B" => 150
              );
              
              #initialise the key
              $key = "";
              
              #set the number of line to display in our captcha
              $numLines = 5;
              
              #set the length of the key to display in our captcha
              $keyLength = 6;
              
              #set the color of the text in our captcha
              $textcolor = array(
              "R" => 0,
              "G" => 255,
              "B" => 0
              );
              Is that what you meant ?

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Nope, I meant inside your GenKey method, like so:

                Code:
                function GenKey ($length)
                {
                    # key
                    $key = "";
                    #define the letter / number that will be used in our key.
                    $chars = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
                    #start a loop to make the key.
                    for($i = 0; $i < $length; $i++)
                    {
                        #pick a random start place in the string
                        $rand_start = rand(1, strlen($chars) - 1);
                        #add this character to our key
                        $key .= substr($chars, $rand_start, 1);
                    }
                    #return our key
                    return $key;
                }
                Does that work now?

                Comment

                • jeddiki
                  Contributor
                  • Jan 2009
                  • 290

                  #9
                  Ah ha
                  Good job I asked ;)

                  When I run it I get this:

                  �PNG  ��� IHDR���x���� ��h�M����IDATh ���y��U} ����o��{�-��4 M�(��R���N,��� %1�X��lj����2� 3��m�$�)*��Xf *(��ұ��n�_� ~�}w�mg�?^�hh�( Ƅ�n������o�{~� s.:�7xYo�W��ڛs Z�O��j��>��"� �6"�ou��+�ke� �s�s��4�m����S ��H�`n�$���"�2ވ ���k���x�<�d�<( �ה��3�S����Ѧ� �`kk}0�� ��-V���#��S��O�� �̨rd�mq���S �4W5�����!� ��51C�M=�&� �'7c�оC����L{�� 3�

                  so maybe thats working

                  I'll take out the comment out at:
                  #header("Conten t-type: image/png");

                  and run it from the form.

                  Thanks

                  Comment

                  • jeddiki
                    Contributor
                    • Jan 2009
                    • 290

                    #10
                    Yep, That did the job !

                    I guess I must have deleted that variable somehow !

                    Thanks again for your help.

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Glad you got it working.

                      Markus.

                      Comment

                      Working...