首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Codeigniter :登录时使用Captcha,始终存储captcha映像

Codeigniter :登录时使用Captcha,始终存储captcha映像
EN

Stack Overflow用户
提问于 2018-06-14 05:46:14
回答 1查看 531关注 0票数 0

我在登录代码点火器时使用captcha。然而,它在逻辑上工作得很完美,但是每次刷新时,它都会将一个captcha图像存储到文件夹中。有人能建议我如何修改下面的代码,这样它就不会将图像存储在文件夹中。

将每个captcha图像存储在文件夹中将增加多个用户的响应时间。我在谷歌上搜索过,但没有找到任何解决方案。请建议一下。谢谢,下面是我的代码

代码语言:javascript
复制
/*--- CONTROLLER CODE --*/

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Captcha extends CI_Controller
{
    function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('captcha');
        $this->load->helper('url');
        $this->load->database();
    }

    public function auth()
    {
        $u=$this->input->post('username');
        $p=$this->input->post('password');

        $this->db->where('username', $u);
        $obj=$this->db->get('user_tbl');

        if($obj->num_rows())
        {
            $data=$obj->row_array();
            if($data['password']==$p)
            {
                $inputCaptcha = $this->input->post('captcha');
                $sessCaptcha = $this->session->userdata('captchaCode');
                if($inputCaptcha === $sessCaptcha)
                {
                   // unlink($captcha['filename']); 
                   $this->load->view('afterLoginView');
                }
                else
                {
                    $this->session->set_flashdata('msg', 'Captcha code does not match, please try again. !');
                redirect('captcha'); 
                }
            }
            else
            {
                $this->session->set_flashdata('msg', 'Incorrect Password !');
                redirect('captcha');      
            }
        }
        else
        {
            $this->session->set_flashdata('msg', 'Incorrect Username !');
            redirect('captcha');
        }
    }

    public function index(){

        // Captcha configuration
        $config = array(
            'img_path'      => 'captcha_images/',
            'img_url'       => base_url().'/captcha_images/',
            'font_path'     => 'system/fonts/texb.ttf',
            'img_width'     => '140',
            'img_height'    => 50,
            'word_length'   => 5,
            'font_size'     => 18
        );
        // $captcha = create_captcha($config);
        print_r($captcha = create_captcha($config));


        // Unset previous captcha and set new captcha word
        $this->session->unset_userdata('captchaCode');
        $this->session->set_userdata('captchaCode', $captcha['word']);

        // Pass captcha image to view
        $data['captchaImg'] = $captcha['image'];

        // Load the view
        $this->load->view('captchaview', $data);
    }

    public function refresh(){
        // Captcha configuration
        $config = array(
            'img_path'      => 'captcha_images/',
            'img_url'       => base_url().'/captcha_images/',
            'font_path'     => 'system/fonts/texb.ttf',
            'img_width'     => '140',
            'img_height'    => 50,
            'word_length'   => 5,
            'font_size'     => 18
        );
        $captcha = create_captcha($config);

        // Unset previous captcha and set new captcha word
        $this->session->unset_userdata('captchaCode');
        $this->session->set_userdata('captchaCode',$captcha['word']);

        // Display captcha image
        echo $captcha['image'];
    }
}


/*--- VIEW ----/*

<!DOCTYPE html>
<html>
<head>
    <title>Login with Captcha</title>
    <script type="text/javascript" src="<?php echo base_url('js/jquery.js'); ?>"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <!-- captcha refresh code -->
<script>
$(document).ready(function(){
    $('.refreshCaptcha').on('click', function(){
        $.get('<?php echo site_url().'/captcha/refresh'; ?>', 
        function(data)
        {
            $('#captImg').html(data);
        });
    });
});
</script>
</head>
<body>

<h4>Login Here</h4>
<form action="<?php echo site_url('captcha/auth'); ?>" method="post">
    <table>
        <tr>
            <td>Username</td>
            <td><input type="text" name="username" class="input" placeholder="Email"></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" name="password" class="input" placeholder="Password"></td>
        </tr>
        <tr> 
            <td>Enter the code :</td>
            <td> <input type="text" name="captcha" value="" placeholder="Captcha"/></td>
        </tr>
        <tr>
            <td><p id="captImg"><?php  if(isset($captchaImg)){echo $captchaImg; }  ?></p></td>
            <td><p>Can't read the image? click <a href="javascript:void(0);" class="refreshCaptcha">here</a> to refresh.</p></td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="submit" name="submit" value="SUBMIT" />
            </td>
        </tr>
    </table>    
</form>
<?php echo $this->session->flashdata("msg"); ?>

</body>
</html>

It is always storing images at (captcha_images/) folder.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-14 05:55:57

将此'expiration' => 7200添加到$config数组中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50850395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档