www.webdeveloper.com
Results 1 to 3 of 3

Thread: CAPTCHA: problem incorporating the script into the webpage.

  1. #1
    Join Date
    Jan 2007
    Location
    Springfield, MO
    Posts
    68

    Unhappy CAPTCHA: problem incorporating the script into the webpage.

    This fully working script does not work when incorporated into a page. I have tried the following: a) put it into a function (function captcha()) and then called that function; b) created a separate page just for the 'captcha' script, and the included that page with the 'include' function. The result is always the same: The image (.... path ...) cannot be displayed, because it contains errors. This is so frustrating! As I said, the script DOES work on its own. Take a look:
    PHP Code:
    ob_start(); 
    $image imagecreate(80,30);
    $bg_color imagecolorallocate($image,102,102,102);
         
    // ----------------- text -------------------------
    $letters = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
    $numbers = array(1,2,3,4,5,6,7,8,9,0);
    $joined array_merge($letters$numbers);
    $word "";
     for(
    $i=0;$i<5;$i++) {
        
    $ran_pos array_rand($joined); 
        
    $word .= $joined[$ran_pos];
     }

    $textColor imagecolorallocate($image255,255,255);
    imagestring($image5155$word$textColor);
         
    header('Content-type: image/png');
    imagepng($image);
    ob_flush(); 
    If I put the script into a function
    PHP Code:
    function captcha() {
    ....
    ....

    and then call it, it does work [U]as long as I do NOT call it from within any HTML tags, like this:
    HTML Code:
    <body>
       <?php captcha(); ?>
    </body>

    I don't know what to do. Please help. (I wasn't been able to find a solution in Google or anywhere else).

  2. #2
    Join Date
    Aug 2006
    Location
    Michigan
    Posts
    1,046
    Try this:


    file name: captcha.php

    PHP Code:
    class Captcha
    {
        function 
    getCaptcha()
        {
            
    ob_start();
            
            
    $image imagecreate(80,30);
            
    $bg_color imagecolorallocate($image,102,102,102);
        
            
    // ----------------- text -------------------------
            
    $letters = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
            
    $numbers = array(1,2,3,4,5,6,7,8,9,0);
            
    $joined array_merge($letters$numbers);
            
    $word "";
            
            for(
    $i=0;$i<5;$i++) 
            {
                
    $ran_pos array_rand($joined);
                
    $word .= $joined[$ran_pos];
            }

            
    $textColor imagecolorallocate($image255,255,255);
            
    imagestring($image5155$word$textColor);
        
            
    header('Content-type: image/png');
            
    imagepng($image);
            
            
    ob_flush();    
        }
    };

    $captcha = new Captcha;
    $captcha->getCaptcha(); 
    web page name: page.php
    HTML Code:
    <?php
    
    include('captcha.php');
    
    ?>
    
    <img src="<?php $captcha ?>">

  3. #3
    Join Date
    Jan 2007
    Location
    Springfield, MO
    Posts
    68
    Thanks for your reply!!! Unfortunately, I got the very same error I had getting earlier. BUT, your script gave me an idea that solved the problem.

    Instead of doing this:
    HTML Code:
    <img src="<?php include(.....); ?>" />
    I did it with no PHP tags:
    HTML Code:
    <img src="captcha.php" />
    CAPTCHA works now. A minor problem is, though, for some reason I can't set the SESSION key initially, when the page first loads. Of course, after I refresh the page, the SESSION key is there, but not the first time around.

    Like this :
    PHP Code:
    ....
    ....
     
    $textColor imagecolorallocate($image255,255,255);
     
    imagestring($image5155$word$textColor);
     
    $_SESSION['captcha_key'] = sha1($word);
    ....
    ..... 
    When the page loads, the following script 'complains' that such SESSION does not exist.
    PHP Code:
    echo $_SESSION['captcha_key'
    NOTE: session_start() exist on both pages (captcha.php and login.php).

    It should work, but it doesn't. Would you be willing to help me with this? Thanks.
    Last edited by andkhl; 06-03-2008 at 03:40 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles