簡體   English   中英

Codeigniter中的驗證碼驗證錯誤

[英]Captcha validation error in codeigniter

我發現了有關驗證碼驗證表單的問題,有關詳細信息,請在此處顯示我的代碼:

驗證碼功能

private function gbr_captcha()
    {
        $vals = array(
        'img_path' => './captcha/',
        'img_url' => base_url().'captcha/',
        'font_path' => './system/fonts/impact.ttf',
        'img_width' => '150',
        'img_height' => 40
        );
        $cap = create_captcha($vals);
        $datamasuk = array(
            'captcha_time' => $cap['time'],
            'word' => $cap['word']
            );
        $expiration = time()-3600;
        $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
        $query = $this->db->insert_string('captcha', $datamasuk);
        $this->db->query($query);
        return $cap['image'];
    }

驗證碼驗證表

    if(empty($cek))
    {
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('captcha', 'Captcha', 'trim|required');

        if ($this->form_validation->run() == FALSE)
        {
            $frm['gbr_captcha'] = $this->gbr_captcha();
            $this->load->view("app_admin/login/index",$frm);
        }
        else
        {
            $u = $this->input->post('username');
            $p = $this->input->post('password');
            $this->app_model->getLoginData($u,$p);
        }
    }

可以在我的網站上顯示驗證碼,但是要驗證用戶輸入的驗證碼正確與否,這是行不通的,我認為我的問題來自驗證,如果有任何建議可以修復我的代碼,請分享,謝謝。

您正在將以下數據存儲到數據庫中

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

恕我直言,沒有什么可以識別用戶的身份(例如ip地址)。 您將無法獲取已經生成並存儲的文本進行比較。 因為您沒有指向用戶的任何內容。

選項1:存儲更多信息,例如ip地址,以及當您嘗試驗證驗證碼時,詢問數據庫該ip地址是否存在任何記錄。

選項2:將驗證碼存儲在類似的會話中

$this->session->set_userdata("captcha", ["expires"=> time()+3600, "data" => captcha_string]) 

這種方式更容易驗證(至少對我而言)。

我希望它足夠清楚。

我認為您在驗證碼中應該有一個回調函數來驗證它是否正確。

 $this->form_validation->set_rules('captcha', 'Captcha', 'trim|required|callback_checkCaptcha');

function checkCaptcha($word){
$ip = $this->session->get_userdata("ip");
    //check db/query db if the captcha word is correct
    $sql = "SELECT id FROM captcha WHERE word = {$word} and ip={$ip}"
//return true or false if word exists or not
    }

其次,您如何確定它是正確的驗證碼? 您可以存儲用戶的IP地址,或設置Cookie /會話

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM