1

早安 Stackoverflow,

我已经成功地在我的项目中实现了验证码助手。图像正在显示、保存等等,尽管图像本身在每次刷新后一次又一次地创建。刷新 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;
}
...

我不知道这是否按预期工作。如果我弄错了,请赐教。

干杯。

4

2 回答 2

0

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

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

于 2013-01-01T16:47:45.907 回答
0
$cap = create_captcha($vals);
$this->session->set_userdata('time', $cap['time']);

数据存入数据库时​​写入。

在此之上:

unlink(realpath('captcha').'/'.$this->session->userdata('time').'.jpg');
于 2013-06-12T13:34:15.237 回答