Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter creating new captcha images at refresh
    text
    copied!<p>Goodday Stackoverflow,</p> <p>I've implented the Captcha-helper in my project successfully. The image is being displayed, saved and what not, although the image itself is created again and again and again after every refresh. After 10 refreshes, my captcha folder contains 10 images.</p> <p>Is this normal?</p> <p>This is my code;</p> <pre><code>... /** * 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-&gt;is_captcha()) { // Get the captcha. $data = $this-&gt;get_captcha_data(); } else { // Create a new captcha. if ($this-&gt;insert_captcha()) $data = $this-&gt;get_captcha_data(); } // Set the values. $captcha_vals = array( 'word' =&gt; $data['word'], 'img_path' =&gt; './media/sm_general/captcha/', 'img_url' =&gt; base_url().'media/sm_general/captcha/', 'img_width' =&gt; '175', 'img_height' =&gt; '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-&gt;db-&gt;select('*'); $this-&gt;db-&gt;from('sm_sessions_captcha'); $this-&gt;db-&gt;where('ip_address',$this-&gt;input-&gt;ip_address()); $this-&gt;db-&gt;limit(1); // Execute the query. $query = $this-&gt;db-&gt;get(); // Get the result. $result = $query-&gt;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-&gt;db-&gt;select('word'); $this-&gt;db-&gt;from('sm_sessions_captcha_words'); $this-&gt;db-&gt;order_by('word','RANDOM'); $this-&gt;db-&gt;limit(1); // Execute the query. $query = $this-&gt;db-&gt;get(); // Get the result. $result = $query-&gt;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' =&gt; (time() + 7200), 'ip_address' =&gt; $this-&gt;input-&gt;ip_address(), 'word' =&gt; $this-&gt;get_word_random() ); // Execute the query. if ($this-&gt;db-&gt;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-&gt;db-&gt;where('captcha_time &lt;',$expiration); $this-&gt;db-&gt;delete('sm_sessions_captcha'); // Prepare the query. $this-&gt;db-&gt;select('*'); $this-&gt;db-&gt;from('sm_sessions_captcha'); $this-&gt;db-&gt;where('captcha_time &gt;=',$expiration); $this-&gt;db-&gt;where('ip_address',$this-&gt;input-&gt;ip_address()); $this-&gt;db-&gt;limit(1); // Execute the query. $query = $this-&gt;db-&gt;get(); // Return the existence. if ($query-&gt;num_rows() == 1) return true; else return false; } ... </code></pre> <p>I have no idea if this is working as intended. Please enlighten me if I am mistaking.</p> <p>Cheers.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload