CodeIgniter Forums

Full Version: CAPTCHA without IP address usage
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]bradyrose[/eluser]

what I am trying to do here is , basically follow the examples in the user guide, for using the captcha while using the database table provided. However, I see in the provided code that this makes use of the client ip address's and, given that my site will operate on an anonymity network, there will be no ip address to retrieve and store. can anyone suggest a manner in which the code below can be modified to suit my need?


Code:
$this->load->helper('captcha');
$vals = array(
    'img_path' => './captcha/',
    'img_url' => 'http://example.com/captcha/'
    );

$cap = create_captcha($vals);

$data = array(
    'captcha_time' => $cap['time'],
    'ip_address' => $this->input->ip_address(),
    'word' => $cap['word']
    );

$query = $this->db->insert_string('captcha', $data);
$this->db->query($query);

echo 'Submit the word you see below:';
echo $cap['image'];
echo '<input type="text" name="captcha" value="" />';


Code:
// First, delete old captchas
$expiration = time()-7200; // Two hour limit
$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);

// Then see if a captcha exists:
$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
$binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
$query = $this->db->query($sql, $binds);
$row = $query->row();

if ($row->count == 0)
{
    echo "You must submit the word that appears in the image";
}

El Forum

[eluser]bradyrose[/eluser]
ok here is my best attempt to swap out the session_id , rather than ip_address, testing this momentarily.


Code:
$this->load->helper('captcha');
$vals = array(
    'img_path' => './captcha/',
    'img_url' => 'http://example.com/captcha/'
    );


$cap = create_captcha($vals);

$session_id = $this->session->userdata('session_id');

$data = array(
    'captcha_time' => $cap['time'],
    'session_id' = $session_id
    'word' => $cap['word']
    );

$query = $this->db->insert_string('captcha', $data);
$this->db->query($query);

echo 'Submit the word you see below:';
echo $cap['image'];
echo '&lt;input type="text" name="captcha" value="" /&gt;';




Code:
// First, delete old captchas
$expiration = time()-7200; // Two hour limit
$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);

// Then see if a captcha exists:
$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND session_id = ? AND captcha_time > ?";
$binds = array($_POST['captcha'], $session_id, $expiration);
$query = $this->db->query($sql, $binds);
$row = $query->row();

if ($row->count == 0)
{
    echo "You must submit the word that appears in the image";
}