SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast Dunkel's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP - Captcha Class

    Hi everybody, this thread is about a Captcha Class I'm working on, I just want you to give me some advice to improve this class, I'll paste the code source here.

    This doesn't need any explanation, I just have 3 files, one is the class... other is where a use the class and the last one is just for showing the captcha image generated by the class.

    The directory Tree would be like this.

    |root
    |--lib <dir>
    |----ctCaptcha.lib.php <the class file>
    |--bin <dir>
    |----ctCaptcha.bin.php <the file wich uses the class>
    |Captcha.php <the demo>

    Well, that the dir Tree, maybe is not as clear as I think but I hope it is useful.

    ctCaptcha.lib.php
    PHP Code:
    <?
      
    /***************************************************************/
      /* ctCaptcha - Libreria de generacionde CAPTCHA
           Version: 1.0.22.06.07
         Autor: Jose Antonio Reyna Fuentes
         Empresa: (c)Code Team 
         URI: http://www.codeteam.net
       
         
       
          Ultima Actualizacion:  22 de Junio de 2007                  */
       /***************************************************************/
       
       /************************ Documentacion ************************/
       /*
       
       Proximamente http://www.codeteam.net
       
       */

        /********************* CODIGO DE LA CLASE *********************/
        
        /*INICIO DE LA SESSION PHP, SI TU APLICACION YA INICIA LA SESSION.
        COMENTA LA SIGUIENTE LINEA*/
        
    session_start();
        
        
    //NOMBRE DE LA VARIABLE DE SESION DEL CAPTCHA
        
    define('CAPTCHA_ID','session_captcha');
        
        class 
    ctCaptcha
        
    {
            var 
    $captchaImg//IMAGEN
            
    var $iAncho//ANCHO DEL CUADRO
            
    var $iAlto//ALTO DEL CUADRO
            
    var $cadena;
        
            
    //CONSTRUCTOR DE LA CLASE
            
    function ctCaptcha($ancho=200,$alto=50)
            {
                
    //DEFINIMOS ANCHO Y ALTO
                
    $this -> iAncho $ancho;
                
    $this -> iAlto $alto;
                
    //INICIALIZAMOS NUESTRA IMAGEN
                
    $this -> captchaImg imagecreate($this -> iAncho$this -> iAlto);
                
    //RELLENAMOS NUESTRO CAPTCHA
                
    $this -> setBox();
            }
            
            
    //FUNCION QUE ESTABLECE UN BORDE EN NUESTRA IMAGEN
            
    function setBox()
            {
                
    $background_color imagecolorallocate($this -> captchaImg255255255);
                
    $black_color imagecolorallocate($this -> captchaImg000);
                
    $border imagerectangle $this -> captchaImg00$this -> iAncho-1$this -> iAlto-1$black_color );
                
                
    $this -> cadena $_SESSION[CAPTCHA_ID] ? $_SESSION[CAPTCHA_ID] : $this -> getStr();
                
                
    $txt_size 20;
                
    $txt_font "ARLRDBD.TTF";
                for (
    $i=0;$i<strlen($this -> cadena);$i++)
                {
                    
    $txt_angle 10 $i rand(-50,50);
                    
    $xx = (40 $i) + 20;
                    
    $yy 30;
                    
    $color imagecolorallocate($this -> captchaImgrand(0,190), rand(0,190), rand(0,190));
                    
    imagettftext $this -> captchaImg$txt_size$txt_angle$xx$yy$color$txt_font$this -> cadena[$i] );
                }
                
                
    /*
                $txt_size = 20;
                $txt_angle = 10;
                $txt_font = "ARLRDBD.TTF";
                $texto_array = imagettfbbox ( $txt_size, $txt_angle, $txt_font, $this -> cadena );
                //print_r($texto_array); 
                
                $xx =  ($this -> iAncho / 2) - (($texto_array[4] - $texto_array[6]) / 2);
                $yy =  ($this -> iAlto / 2) - (($texto_array[1] - (-$texto_array[7])) / 2);
                
                imagettftext ( $this -> captchaImg, $txt_size, $txt_angle, $xx, $yy, $black_color, $txt_font, $this -> cadena );
                */

            
    }
            
            function 
    getStr($limit=5)
            {
                
    $haystack "0123456789";
                
    //$haystack .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                
    $cadena "";
                for (
    $i=0;$i<$limit;$i++)
                {
                    
    $cadena .= $haystack[ceil(rand(0,strlen($haystack)-1))];
                }
                
    $_SESSION[CAPTCHA_ID] = $cadena;        
                return 
    $cadena;
            }
            
            function 
    Crear()
            {
                
    header ("Content-type: image/png");
                
    header ("Pragma: no-cache");
                
                
    imagepng $this -> captchaImg ); 
            }
        }
    ?>
    ctCaptcha.bin.php
    PHP Code:
    <?php
        
    include ("../lib/ctCaptcha.lib.php");    
        
        if (empty(
    $_GET['reload']))
        {
            
    $myCaptcha = new ctCaptcha();
            
    $myCaptcha -> Crear();
        }else{
            
    $_SESSION[CAPTCHA_ID] = ""
            
    header("location: ".$_SERVER['HTTP_REFERER']);
        }
    ?>
    Captcha.php
    PHP Code:
    Captcha<br />
    <
    img src="bin/ctCaptcha.bin.php" /><br />
    <
    a href="bin/ctCaptcha.bin.php?reload=ok">Reload</a
    You can see a working example at:
    http://www.codeteam.net/ctclasses/Captcha.php

    I hope you check it and give me tips for improve this class.

    Thanks

  2. #2
    SitePoint Evangelist optl's Avatar
    Join Date
    Oct 2004
    Location
    Washington DC
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You definitely need to have the reload done in Javascript. It would be way to annoying to have to fill out everything all over again. Unless I'm missing something.... Give us a link to the demo if you can.
    For the phrase "Bethesda home architect", my clients
    websites occupy 6 of the first 8 results
    on the 1st page of Google. My Secret SEO Strategy Revealed

  3. #3
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would suggest to pass CAPTCHA_ID as an argument in the constructor, rather than having it defined as a constant.

    Also, I'd recommend to split the class into two: only purely for CAPTCHA image generation, and another (which will use the first one) to deal with sessions.

  4. #4
    SitePoint Enthusiast Dunkel's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I will rewrite those thinks and then show you the class again, the two parts you recommend me should be in two different files?

  5. #5
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dunkel View Post
    Ok, I will rewrite those thinks and then show you the class again, the two parts you recommend me should be in two different files?
    They don't have to, but I'd recommend to use a file per class.

  6. #6
    SitePoint Enthusiast Dunkel's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by earl-grey View Post
    They don't have to, but I'd recommend to use a file per class.
    Ok, I will paste the new code soon I hope you can take a look of it

  7. #7
    SitePoint Enthusiast Dunkel's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by earl-grey View Post
    They don't have to, but I'd recommend to use a file per class.

    Hello again, i just rewrite some parts of the Class, just tell if it is good enough or how to improve it

    Here is the first library of the class:
    ctCaptcha.lib.php
    PHP Code:
    <?
      
        session_start
    ();
        
        require_once(
    'ctCaptchaImg.lib.php');
        
        class 
    ctCaptcha
        
    {
            var 
    $captchaImg//IMAGEN
            
    var $iAncho//ANCHO DEL CUADRO
            
    var $iAlto//ALTO DEL CUADRO
            
    var $cadena;
            var 
    $captcha_id;
        
            
    //CONSTRUCTOR DE LA CLASE
            
    function ctCaptcha($ancho=200,$alto=50,$captcha_id='session_captcha')
            {
                
    //DEFINIMOS ANCHO Y ALTO
                
    $this -> iAncho $ancho;
                
    $this -> iAlto $alto;
                
    $this -> captcha_id $captcha_id;
                
    //INICIALIZAMOS NUESTRA IMAGEN
                //$this -> captchaImg = imagecreate($this -> iAncho, $this -> iAlto);
                //RELLENAMOS NUESTRO CAPTCHA
                //$this -> setBox();
            
    }
            
            function 
    Create()
            {
                 
    $this -> captchaImg = new captchaImg($this -> iAncho$this -> iAlto,$this -> captcha_id);
                 
    //echo "<img src=\"{$this -> captchaImg}\" id=\"imgCaptcha\">";
            
    }
            
            function 
    Refresh()
            {
                
    $_SESSION[$this -> captcha_id] = "";
                
    $this -> Create();
            }
        }
    ?>
    then the class which create the image...
    ctCaptchaImg.lib.php

    PHP Code:
    <?
    class captchaImg
    {
        var 
    $captchaImg;
        var 
    $iAncho//ANCHO DEL CUADRO
        
    var $iAlto//ALTO DEL CUADRO
        
    var $captcha_id;
        var 
    $cadena;
        
        function 
    captchaImg($ancho$alto$captcha_id){
            
    $this -> iAncho $ancho;
            
    $this -> iAlto $alto;
            
    $this -> captcha_id $captcha_id;
            
    $this -> captchaImg imagecreate($this -> iAncho$this -> iAlto);
            
    $this -> setBox();
            
    $this -> show();
        }
        
        function 
    setBox()
        {
            
    $background_color imagecolorallocate($this -> captchaImg255255255);
            
    $black_color imagecolorallocate($this -> captchaImg000);
            
    $border imagerectangle $this -> captchaImg00$this -> iAncho-1$this -> iAlto-1$black_color );
                
            
    $this -> cadena $_SESSION[$this -> captcha_id] ? $_SESSION[$this -> captcha_id] : $this -> getStr();
                
            
    $txt_size 20;
            
    $txt_font "../lib/ARLRDBD.TTF";
            for (
    $i=0;$i<strlen($this -> cadena);$i++)
            {
                
    $txt_angle 10 $i rand(-50,50);
                
    $xx = (40 $i) + 20;
                
    $yy 30;
                
    $color imagecolorallocate($this -> captchaImgrand(0,190), rand(0,190), rand(0,190));
                
    imagettftext $this -> captchaImg$txt_size$txt_angle$xx$yy$color$txt_font$this -> cadena[$i] );
            }

        }
        
        function 
    getStr($limit=5)
        {
            
    $haystack "0123456789";
            
    //$haystack .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            
    $cadena "";
            for (
    $i=0;$i<$limit;$i++)
            {
                
    $cadena .= $haystack[ceil(rand(0,strlen($haystack)-1))];
            }
            
    $_SESSION[$this -> captcha_id] = $cadena;        
            return 
    $cadena;
        }
        
        function 
    show()
        {
            
    header ("Content-type: image/png");
            
    header ("Pragma: no-cache");
            
    imagepng $this -> captchaImg );
        }
    }
    ?>
    and the example that you can see here working:
    http://www.codeteam.net/ctclasses/Captcha.php

    PHP Code:
    <script type="text/javascript">

        function refreshCaptcha(id)
        {
            obj = document.getElementById(id);
            obj.src = 'bin/ctCaptcha.bin.php?reload=true&id=' + Math.random()*100;
        }
        
    </script>
    <?
        
    //require ("lib/ctCaptcha.lib.php");
        
    echo "Testing Captcha<br>";
        
        echo 
    "<img src=\"bin/ctCaptcha.bin.php\" id=\"imgCaptcha\"><br />";
        
        echo 
    "<a href=\"javascript:refreshCaptcha('imgCaptcha')\">Refresh</a>";
    ?>
    and this is the image bin... (the one that is called for the img tag)

    PHP Code:
    <?php
        
    include ("../lib/ctCaptcha.lib.php");    
        
    $myCaptcha = new ctCaptcha();
        
        if (empty(
    $_GET['reload']))
        {
            
    $myCaptcha -> Create();
        }else{
            
    $myCaptcha -> Refresh(); 
            
    //header("location: ".$_SERVER['HTTP_REFERER']);
        
    }
    ?>
    Last edited by Dunkel; Jun 26, 2007 at 04:24. Reason: I Forgot to paste a File content

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi Dunkel and all

    I just modify path in files, but when i run captcha.php on localhost, it's not work.

    Help me with script contain in attach file.
    Attached Files Attached Files


Bookmarks

Posting Permissions

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