php - Codeigniter 不正确的 CAPTCHA 图像 src

标签 php codeigniter codeigniter-3

您好,我正在尝试为我的注册页面使用 CodeIgniter 验证码助手。 我有一个具有如下注册功能的用户 Controller :

public function register(){   
    $this->form_validation->set_rules('user_name' , 'Username' , 'trim|required|max_length[32]'); 
    $this->form_validation->set_rules('captcha' , 'Captcha' , 'trim|required|max_length[32]|callback_check_captcha');
    $this->form_validation->set_rules('password', 'Password','trim|required|min_length[5]|max_length[12]');
    $this->form_validation->set_rules('confirm_password', 'Confirm Password','trim|required|min_length[5]|max_length[12]|matches[password]');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
    if ($this->form_validation->run()==FALSE){
    //Captcha code 
    $this->load->helper('captcha');
    //Captcha Config 
    $vals = array(
        'img_path'      => './captcha/',
        'img_url'       => './captcha/',
        'img_width'     => '150',
        'img_height'    => 30,
        'expiration'    => 7200,
        'word_length'   => 8,
        'font_size'     => 16,
        'img_id'        => 'Imageid',
         'font_path'     => '/fonts/Open.ttf',

        // White background and border, black text and red grid
        'colors'        => array(
                'background' => array(255, 255, 255),
                'border' => array(255, 255, 255),
                'text' => array(0, 0, 0),
                'grid' => array(255, 40, 40)
        )
    );
    //Create the Captcha
    $cap = create_captcha($vals);

    // Store The Created Captcha in DB for Verification  
    $data = array(
    'captcha_time'  => $cap['time'],
    'ip_address'    => $this->input->ip_address(),
    'word'          => $cap['word']
    );
    $this->register_model->store_captcha($data);
    //Load View
    $data['image']=$cap['image'];
    var_dump($data['image']);exit();
    $this->load->view('templates/header');
    $this->load->view('register' , $data);
    $this->load->view('templates/footer');
    }else {
              $option=array('cost'=>12);
              $encrypt_password=password_hash($this->input->post('password'),PASSWORD_BCRYPT,$option);
              $this->register_model->add_user($encrypt_password);
              $this->session->set_flashdata('user_registered','You are Successfully Registered and can Log in ');
              redirect('register/success');
    }
}

如果您看到上面的代码,我将“img_path”设置为“./captcha/”(使用根相关方法)。我在根目录中有这个文件夹验证码。

在我看来我有:

    <div class="form-group">
    <label>Enter UserID</label>
    <input type="text" class="form-control" name="user_name"  placeholder="Enter Your UserId">
  </div>
  <div class="form-group">
    <label>Enter Your Email</label>
    <input type="email" class="form-control" name="email"  placeholder="Enter Your Email">
  </div>
  <div class="form-group">
    <label>Enter Your Password </label>
    <input type="password" class="form-control" name="password"  placeholder="Enter Password">
  </div>
  <div class="form-group">
        <label>Re-enter Password </label>
        <input type="password" class="form-control" name="confirm_password"  placeholder="Renter Password">
  </div>
  <?php echo $image ; ?>
  <div class="form-group">
        <label>Solve Captcha </label>
        <input type="text" class="form-control" name="captcha" >
  </div>
   <button type="submit" class="btn btn-primary">Submit</button>
  <?php echo form_close();?>

问题是我的图像没有显示出来。检查验证码图像时,我发现它是正确的,如下所示:

<img id="Imageid" src="./captcha/1551023447.5228.jpg" style="width: 150; height: 30; border: 0;" alt=" ">

然而,src 实际上被“计算”为:http://[::1]/login/user/captcha/1551023447.5228.jpg .请参见下图。

computed src

谁能告诉我为什么 src 指向 http://[::1]/login/user/captcha/1551023447.5228.jpg尽管它应该是 http://[::1]/login/captcha/1551023447.5228.jpg ?我也很想知道悬停在 chrome 中的 img src 上如何显示与代码中的实际值不同的值。我知道/.path 用于从根目录遍历文件夹,但这是我第一次在 chrome 中看到类似的东西。谢谢 。

最佳答案

路径参数看起来不对。它应该是验证码目录的完整 URL,如

$vals = array(
    'img_path'      => 'captcha/',
    'img_url'       => base_url('captcha')
.....

假设您的项目根目录中有一个 captcha 目录,您还在 config/autoload.php 中启用了 url 帮助程序

关于chrome dev tool path,它计算出显示完整URL的相对路径,这样你就可以直接点击了。

关于php - Codeigniter 不正确的 CAPTCHA 图像 src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54853913/

相关文章:

php - 无法在codeigniter中调用 Controller

php - 这是在 codeigniter 3.1.5 中发布和处理数据的最佳方式

php - ampps 3.8 上的本地主机太慢

php - 开发 Joomla 组件的单元测试

php - 通过唯一ID动态加载MySQL数据到Bootstrap模态-CodeIgniter

javascript - CodeIgniter 中的部分 url

html - 无法加载资源 : the server responded with a status of 403 (Forbidden) including css files and js libraries

php - x 轴值不显示在谷歌折线图中

PHP 扩展在 Ubuntu 中激活

php - 使用 Gmail 进行 SMTP 时,可以设置不同的 "from"地址吗?