Welcome Guest, Not a member yet? Register   Sign In
CI 2.0 - Image Show Problem
#2

[eluser]mariuskmc[/eluser]
I use a model
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Captcha model
*
* Interacts with the captcha table and creates and validates
* the captcha using the captcha plugin
*
* @package        openviz
* @subpackage          admin
* @author        Robert Tooker
* @since        Version 1.0
*/

class Captcha_model extends CI_Model {

    /**
     * Defines the table for the model, used for active record functions
     * @access private
     * @var string
     */
    private $_model_table = 'captcha';

    /**
     * How long images and database records will be retained
     * @access private
     * @var integer
     */
    private $_expiration = 3600;

    /**
     * Auto increment field, primary key
     * @access public
     * @var integer
     */
    public $id = null;

    /**
     * The time the captcha was created (unix timestamp)
     * @access public
     * @var integer
     */
    public $captcha_time = 0;

    /**
     * The IP address of the user when the captcha was created
     * @access public
     * @var text
     */
    public $ip_address = '';

    /**
     * The word shown in the captcha image
     * @access public
     * @var text
     */
    public $word = '';

    /**
     * Constructor
     */
    function __construct() {
        //parent::Model();
        parent::__construct();
    }

    /**
     * Makes a captcha image
     * @param text $ip_address the IP address of the user
     * @return text a link to the image or an error message if image could not be created
     */
    function make($ip_address) {
        //$this->load->plugin( 'captcha' );
        $this->load->helper('captcha');
        
        $parameters = array(
            'img_path' => './captcha/',
            'img_url' => base_url() . 'captcha/',
            'img_width' => 150,
            'img_height' => 30,
      
            'expiration' => $this->_expiration
        );

        $captcha = create_captcha( $parameters );
        if ( $captcha ) {
            $this->id = null;
            $this->captcha_time = $captcha['time'];
            $this->ip_address = $ip_address;
            $this->word = $captcha['word'];

            $this->db->insert($this->_model_table, $this->model_to_array());
            $this->id = $this->db->insert_id();
        } else {
            return "Could not make captcha." ;
        }
        return $captcha['image'] ;
    }

    /**
     * Checks the response to a captcha image
     * @param text $ip_address the ip address of the response
     * @param text $response the response which should match the word
     * @return bool if the response is correct or not
     */
    function check($ip_address, $response) {
        $this->db->where('captcha_time <', time() - $this->_expiration);
        $this->db->delete($this->_model_table);

        //checking input
        $this->db->where('ip_address', $ip_address);
        $this->db->where('word', $response);
        $query = $this->db->get($this->_model_table);

        if ( $query->num_rows() > 0 ) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Assigns the values of a captcha array to the captcha model
     *
     * Receives an array and sets matching properties of the model, leaving
     * unmatched properties intact. Useful for receiving a subset of fields
     * from a page post and applying an update using active record.
     *
     * @access public
     * @param mixed $data an associative array of datafield properties with the key representing the property name
     */
    function array_to_model($data) {
        if (isset($data['id'])) $this->id = $data['id'];
        if (isset($data['captcha_time'])) $this->captcha_time = $data['captcha_time'];
        if (isset($data['ip_address'])) $this->id = $data['ip_address'];
        if (isset($data['word'])) $this->id = $data['word'];
    }

    /**
     * Returns the model's database fields as an associative array
     *
     * @access public
     * @return mixed an associative array of datafield properties with the key representing the property name
     */
    function model_to_array() {
        $data['id'] = $this->id;
        $data['captcha_time'] = $this->captcha_time;
        $data['ip_address'] = $this->ip_address;
        $data['word'] = $this->word;
        return $data;
    }

}

in controler i have
Code:
// load captcha model with default database connection
        $this->load->model('Captcha_model', '', 'default');
        // add the captcha image to the view data
        $data['captcha_img']=$this->Captcha_model->make($this->input->ip_address());


// and for validation
$this->form_validation->set_rules('captcha_text', 'Cod Security code', 'required|callback_validate_captcha');

in view
Code:
&lt;?
$form_captcha_text = form_input(array(
    'name'=>'captcha_text'
    , 'id'=>'captcha_text'
    , 'value'=>''
    , 'maxlength'=>8
    , 'size'=>14
    , 'class'=>'texta')
);
?&gt;

&lt;?php echo $captcha_img; ?&gt; = &lt;?php echo $form_captcha_text; ?&gt;


database
Code:
CREATE TABLE IF NOT EXISTS `captcha` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `captcha_time` int(10) unsigned NOT NULL,
  `ip_address` varchar(16) NOT NULL DEFAULT '0',
  `word` varchar(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `ip_address` (`ip_address`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


Messages In This Thread
CI 2.0 - Image Show Problem - by El Forum - 03-02-2011, 03:53 AM
CI 2.0 - Image Show Problem - by El Forum - 03-02-2011, 05:34 AM
CI 2.0 - Image Show Problem - by El Forum - 03-02-2011, 05:44 AM
CI 2.0 - Image Show Problem - by El Forum - 03-02-2011, 05:53 AM



Theme © iAndrew 2016 - Forum software by © MyBB