CodeIgniter在刷新时创建新的验证码图像

时间:2012-08-18 08:49:13

标签: php codeigniter captcha

Goodday Stackoverflow,

我已成功在我的项目中实现了Captcha-helper。虽然图像本身是在每次刷新后一次又一次地创建的,但图像正在显示,保存和不显示。 10次​​刷新后,我的验证码文件夹包含10张图片。

这是正常的吗?

这是我的代码;

...

/**
 * Method that returns the currently
 * active Captcha code of the user.
 *
 * @access public
 * @return string - user's captcha code
 * @since v0.1.0.0
 */
public function get_captcha()
{
    // Check for already existing captcha's.
    if ($this->is_captcha())
    {
        // Get the captcha.
        $data = $this->get_captcha_data();
    }
    else
    {
        // Create a new captcha.
        if ($this->insert_captcha())
            $data = $this->get_captcha_data();
    }

    // Set the values.
    $captcha_vals = array(
        'word' => $data['word'],
        'img_path' => './media/sm_general/captcha/',
        'img_url' => base_url().'media/sm_general/captcha/',
        'img_width' => '175',
        'img_height' => '60'
    );

    // Return the captcha.
    return create_captcha($captcha_vals);
}

/**
 * Method that returns the captcha data
 * of a specific user.
 *
 * @access private
 * @param string $field - (optional) specific to be returned data
 * @return array/string - data of the captcha
 * @since v0.1.0.0
 */
private function get_captcha_data($field='')
{
    // Prepare the query.
    $this->db->select('*');
    $this->db->from('sm_sessions_captcha');
    $this->db->where('ip_address',$this->input->ip_address());
    $this->db->limit(1);

    // Execute the query.
    $query = $this->db->get();

    // Get the result.
    $result = $query->row_array();

    // Return the data.
    if ( ! empty($field))
        return $result[$field];
    else
        return $result;
}

/**
 * Method that returns a random word from
 * the database.
 *
 * @access private
 * @return string - randomly selected word
 * @since v0.1.0.0
 */
private function get_word_random()
{
    // Prepare the query.
    $this->db->select('word');
    $this->db->from('sm_sessions_captcha_words');
    $this->db->order_by('word','RANDOM');
    $this->db->limit(1);

    // Execute the query.
    $query = $this->db->get();

    // Get the result.
    $result = $query->row_array();

    // Return the word.
    return $result['word'];
}

/**
 * Method that creates a new captcha image.
 *
 * @access private
 * @return array
 * @since v0.1.0.0
 */
private function insert_captcha()
{           
    // Prepare the query.
    $data = array(
        'captcha_time' => (time() + 7200),
        'ip_address' => $this->input->ip_address(),
        'word' => $this->get_word_random()
    );

    // Execute the query.
    if ($this->db->insert('sm_sessions_captcha',$data))
        return true;
    else
        return false;
}

/**
 * Check if a captcha already exists.
 *
 * @access private
 * @return boolean - exists or not
 * @since v0.1.0.0
 */
private function is_captcha()
{
    // Set the expiration time.
    $expiration = time() - 7200;

    // Remove all expired captchas.
    $this->db->where('captcha_time <',$expiration);
    $this->db->delete('sm_sessions_captcha');

    // Prepare the query.
    $this->db->select('*');
    $this->db->from('sm_sessions_captcha');
    $this->db->where('captcha_time >=',$expiration);
    $this->db->where('ip_address',$this->input->ip_address());
    $this->db->limit(1);

    // Execute the query.
    $query = $this->db->get();

    // Return the existence.
    if ($query->num_rows() == 1)
        return true;
    else
        return false;
}
...

我不知道这是否按预期工作。如果我误会,请赐教。

干杯。

2 个答案:

答案 0 :(得分:0)

这是正常的,每次刷新生成新的验证码图像,然后CI运行垃圾收集,因此您可以设置旧验证码图像应保留在文件夹中的时间"expiration"

http://ellislab.com/codeigniter/user-guide/helpers/captcha_helper.html

答案 1 :(得分:0)

$cap = create_captcha($vals);
$this->session->set_userdata('time', $cap['time']);

在数据库中存储数据时写入。

高于此:

unlink(realpath('captcha').'/'.$this->session->userdata('time').'.jpg');