Captcha不在CI工作

时间:2016-09-12 18:21:05

标签: php codeigniter

我写了一小段代码,它应该适用于Codeigniter中的验证码。代码应该只是打印创建验证码的时间,第一次尝试。但它似乎甚至没有创建验证码本身。我确定帮助程序已加载,这是在构造函数中完成的。接下来,应该有将图像写入文件夹的正确权限。任何人都知道为什么它不能正常工作?

defined('BASEPATH') OR exit('No direct script access allowed');
class Register extends CI_Controller{

     public function __construct(){
        parent::__construct();  
        $this->load->helper('captcha');
    }

    public function generate_captcha(){
        $vals = array(
            'img_path' => './captcha/',
            'img_url' => base_url().'captcha/',
        );
        echo base_url().'assets/images/captcha/';
        $captcha = create_captcha($vals);

        echo 'cap time: ' . $captcha['time'];

        $captcha_image = $captcha['image'];
        return $captcha_image; 
    }

}

修改 它可以与除此代码之外的其他内容有关吗?我已经为文件夹设置了正确的权限,因此它可以将图像写入目录。

3 个答案:

答案 0 :(得分:2)

在应用程序之外创建一个名为captcha Captcha Helper的文件夹我认为你还需要更多$ vals,而不仅仅是img_path和img_url

  

还要确保文件夹chmod 0777权限或0700

您可能还需要配置一些路线

$route['register/generate_captcha'] = 'register/generate_captcha';

文件名:Register.php

application

assets > images > captcha // Has the correct permissions

assets > images > captcha > fonts // Has the correct permissions

system

index.php

控制器

更新

文件名:file and class style guide

后面的Register.php

设置您的基本网址:$config['base_url'] = 'http://localhost/yourproject/';

<?php

class Register extends CI_Controller {

     public function __construct() {
        parent::__construct();  
        $this->load->helper('captcha');
    }

    public function index(){
        $vals = array(
            'word' => 'Random word',
            'img_path' => './assets/images/captcha/',
            'img_url' => base_url('assets/images/captcha'),
            'font_path' => './assets/images/captcha/fonts/texb.ttf',
            'img_width' => '150',
            'img_height' => 30,
            'expiration' => 7200,
            'word_length' => 8,
            'font_size' => 16,
            'img_id' => 'Imageid',
            'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
        );

        $cap = create_captcha($vals);
        echo $cap['image'];

    }

}

图像示例1

enter image description here

图像示例2

enter image description here

答案 1 :(得分:0)

位置: ./ application / controllers / Captcha.php

<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Captcha extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->driver("session");
        $this->load->helper(array('form', 'url', 'captcha'));
    }

    public function index() {
        $this->form_validation->set_rules('name', "Name", 'required');

        $this->form_validation->set_rules('captcha', "Captcha", 'required');
        $userCaptcha = set_value('captcha');
        $word = $this->session->userdata('captchaWord');

        if ($this->form_validation->run() == TRUE && strcmp(strtoupper($userCaptcha),strtoupper($word)) == 0){

            $this->session->unset_userdata('captchaWord');
            $name = set_value('name');
            $data = array('name' => $name);

            $this->load->view('success-view', $data);

        } else {

            $vals = array('img_path' => 'static/','img_url' => base_url().'static/');
            $captcha = create_captcha($vals);
            $this->session->set_userdata('captchaWord', $captcha['word']);
            $this->load->view('captcha-view', $captcha);
        }
    }
}

位置: *。/ application / views / captcha-view.php /

添加一个Captcha!

<h1>Adding a captcha</h1>

<p>Take a look at <code style="background:rgb(220,220,220);">application/controllers/Captcha.php</code> to look at the controller used to generate the captcha.</p>
<?php echo validation_errors(); ?>
<?php echo form_open( 'captcha'); ?>
</p>

<p>
<label for="name">Name:</label>
<input id="name" name="name" type="text" />
</p>

<?php echo $image; ?>

<p>
<label for="name">Captcha:</label>
<input id="captcha" name="captcha" type="text" />
</p>

<?php echo form_submit( "submit", "Submit"); ?>
<?php echo form_close(); ?>

位置:./ application / views / success-view.php

<html>

<head>
    <title>Success!</title>
</head>

<body>
    <h1>Success!</h1>
    <p>Thanks, <?php echo $name; ?>!</p>
</body>

</html>

答案 2 :(得分:0)

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Mycaptcha extends CI_Controller  {

public function __construct() {

    parent::__construct();  
        $this->load->helper('captcha');
        $this->load->helper('url');
    }

    public function index() { 
        $vals = array(
            'img_path' => './captcha/',
            'img_url' => base_url().'/captcha/',
        );

        $captcha = create_captcha($vals);
        $captcha_image = $captcha['image'];

        print_r($captcha);
    }
}

确保您拥有:

  1. 使用权限777
  2. 在root上创建文件夹验证码
  3. 在config.php $config['base_url'] = 'http://localhost/yourproject/';
相关问题