我有一个页面,其中包含页面中的验证码,但我无法显示验证码图像。如何在视图中显示验证码图像。这是我的代码
控制器:
public function contact_mail()
{
$this->load->helper('captcha');
$this->load->model('index_model');
$this->load->library('form_validation');
$this->form_validation->set_rules('captcha', 'Captcha', 'required');
if($this->form_validation->run()==FALSE){
if($this->input->post('submit')){
$inputCaptcha = $this->input->post('captcha');
$sessCaptcha = $this->session->userdata('captchaCode');
if($inputCaptcha === $sessCaptcha){
echo 'Captcha code matched.';
}else{
echo 'Captcha code was not match, please try again.';
}
}
// Captcha configuration
$config = array(
'img_path' => 'captcha_images/',
'img_url' => base_url().'captcha_images/',
'img_width' => '150',
'img_height' => 50,
'word_length' => 8,
'font_size' => 16
);
$captcha = create_captcha($config);
// Unset previous captcha and store new captcha word
$this->session->unset_userdata('captchaCode');
$this->session->set_userdata('captchaCode',$captcha['word']);
// Send captcha image to view
$data['captchaImg'] = $captcha['image'];
$data['mainpage'] = "contact";
$this->load->view('templates/template',$data);
}
else{
$result=$this->index_model->send_mail($this->input->post('email'));
if($result)
{
$this->flash->success('<h2 style="color:green">Thank You! Your Message has been sent</h2>');
redirect('contact');
}
else
{
$this->flash->success('<h2 style="color:red">Sorry ! Message sending failed</h2>');
redirect('contact');
}
}
}
查看:
<div class="">
<div class="contactcaptcha">
<input type="text" class="form-control" name="captcha_image" placeholder="captcha_image" style="background-color: #f4f4f4;border: none;" required>
<?php echo form_error('captcha_image', '<div class="error">', '</div>'); ?>
</div>
</div>
更新了我的代码
答案 0 :(得分:0)
你正在恢复会话吗?
session_start() - 开始新会话或恢复现有会话
看看这个:
<强>控制器强>
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Captcha_Controller extends CI_Controller
{
// Load Helper in and Start session.
function __construct()
{
parent::__construct();
$this->load->helper('captcha');
session_start();
}
// This function show values in view page and check captcha value.
public function form()
{
if (empty($_POST)) {
$this->captcha_setting();
} else {
// Case comparing values.
if (strcasecmp($_SESSION['captchaWord'], $_POST['captcha']) == 0) {
echo "<script type='text/javascript'> alert('Your form successfully submitted'); </script>";
$this->captcha_setting();
} else {
echo "<script type='text/javascript'> alert('Try Again'); </script>";
$this->captcha_setting();
}
}
}
// This function generates CAPTCHA image and store in "image folder".
public function captcha_setting()
{
$values = array(
'word' => '',
'word_length' => 8,
'img_path' => './images/',
'img_url' => base_url() . 'images/',
'font_path' => base_url() . 'system/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 50,
'expiration' => 3600
);
$data = create_captcha($values);
$_SESSION['captchaWord'] = $data['word'];
// image will store in "$data['image']" index and its send on view page
$this->load->view('captcha_view', $data);
}
// For new image on click refresh button.
public function captcha_refresh()
{
$values = array(
'word' => '',
'word_length' => 8,
'img_path' => './images/',
'img_url' => base_url() . 'images/',
'font_path' => base_url() . 'system/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 50,
'expiration' => 3600
);
$data = create_captcha($values);
$_SESSION['captchaWord'] = $data['word'];
echo $data['image'];
}
}
?>
查看强>
<html>
<head>
<title>Add captcha using CodeIgniter</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
// Ajax post for refresh captcha image.
$(document).ready(function() {
$("a.refresh").click(function() {
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "index.php/captcha_controller/captcha_refresh",
success: function(res) {
if (res)
{
jQuery("div.image").html(res);
}
}
});
});
});
</script>
</head>
<body>
<div class="main">
<div id="content">
<h2 id="form_head">Captcha Using Codelgniter</h2>
<br/>
<hr>
<div id="form_input">
<?php
// Form Open
echo form_open();
// Name Field
echo form_label('Name');
$data_name = array(
'name' => 'name',
'class' => 'input_box',
'placeholder' => 'Please Enter Name',
'id' => 'name',
'required' => ''
);
echo form_input($data_name);
echo "<br>";
echo "<br>";
// Email Field
echo form_label('Email');
$data_email = array(
'name' => 'email',
'class' => 'input_box',
'placeholder' => 'Please Enter Email',
'id' => 'email',
'required' => ''
);
echo form_input($data_email);
echo "<br>";
echo "<br>";
echo "<div class='image'>";
// $image is the index of $data array. which will send by controller.
echo $image;
echo "</div>";
// Calling for refresh captcha image.
echo "<a href='#' class ='refresh'><img id = 'ref_symbol' src =".base_url()."img/refresh.png></a>";
echo "<br>";
echo "<br>";
// Captcha word field.
echo form_label('Captcha');
$data_captcha = array(
'name' => 'captcha',
'class' => 'input_box',
'color' => 'white',
'placeholder' => '',
'id' => 'captcha'
);
echo form_input($data_captcha);
?>
</div>
<div id="form_button">
<?php echo form_submit('submit', 'Submit', "class='submit'"); ?>
</div>
<?php
// Form Close
echo form_close(); ?>
</div>
</div>
</body>
</html>
<强> CSS 强>
body {
font-family: 'Raleway', sans-serif;
}
.main
{
width: 1015px;
position: absolute;
top: 10%;
left: 20%;
}
#form_head
{
text-align: center;
background-color: #FEFFED;
height: 66px;
margin: 0 0 -29px 0;
padding-top: 35px;
border-radius: 8px 8px 0 0;
color: rgb(97, 94, 94);
}
#content {
position: absolute;
width: 450px;
height: 490px;
border: 2px solid gray;
border-radius: 10px;
}
#content_result{
position: absolute;
width: 450px;
height: 192px;
border: 2px solid gray;
border-radius: 10px;
margin-left: 559px;
margin-top: -262px;
}
#form_input
{
margin-left: 50px;
margin-top: 36px;
}
label
{
margin-right: 6px;
font-weight: bold;
}
#form_button{
padding: 0 21px 15px 15px;
position: absolute;
bottom: 0px;
width: 414px;
background-color: #FEFFED;
border-radius: 0px 0px 8px 8px;
border-top: 1px solid #9A9A9A;
}
.submit{
font-size: 16px;
background: linear-gradient(#ffbc00 5%, #ffdd7f 100%);
border: 1px solid #e5a900;
color: #4E4D4B;
font-weight: bold;
cursor: pointer;
width: 300px;
border-radius: 5px;
padding: 10px 0;
outline: none;
margin-top: 20px;
margin-left: 15%;
}
.submit:hover{
background: linear-gradient(#ffdd7f 5%, #ffbc00 100%);
}
.label_output
{
color:#4A85AB;
margin-left: 10px;
}
#result_id
{
text-align: center;
background-color: #FCD6F4;
height: 47px;
margin: 0 0 -29px 0;
padding-top: 12px;
border-radius: 8px 8px 0 0;
color: rgb(97, 94, 94);
}
#result_show
{
margin-top: 35px;
margin-left: 45px;
}
.input_box{
height:40px;
width:240px;
padding:20px;
border-radius:3px;
background-color:#FEFFED;
margin-left:30px;
}
img {
margin-left: 97px;
}
input#name {
margin-left: 45px;
}
input#email {
margin-left: 50px;
}
img#ref_symbol {
margin-left: 275px;
margin-top: -36px;
}
答案 1 :(得分:0)
在codeigniter中生成验证码
$this->load->helper('captcha');
$this->load->model('index_model');
$this->load->library('form_validation');
if($this->input->post('submit')){
$this->form_validation->set_rules('captcha', 'Captcha', 'required');
if($this->form_validation->run()==FALSE){
echo "please enter captcha";
} else {
$inputCaptcha = $this->input->post('captcha');
$sessCaptcha = $this->session->userdata('captchaCode');
if($inputCaptcha === $sessCaptcha){
echo 'Captcha code matched.';
$result=$this->index_model->send_mail($this->input->post('email'));
//$result = 1;
if($result)
{
$this->flash->success('<h2 style="color:green">Thank You! Your Message has been sent</h2>');
redirect('contact');
}
else
{
$this->flash->success('<h2 style="color:red">Sorry ! Message sending failed</h2>');
redirect('contact');
}
}else{
echo 'Captcha code was not match, please try again.';
}
}
}
// Captcha configuration
$config = array(
'img_path' => 'captcha_images/',
'img_url' => base_url().'captcha_images/',
'img_width' => '150',
'img_height' => 50,
'word_length' => 8,
'font_size' => 16
);
$captcha = create_captcha($config);
// Unset previous captcha and store new captcha word
$this->session->unset_userdata('captchaCode');
$this->session->set_userdata('captchaCode',$captcha['word']);
// Send captcha image to view
$data['captchaImg'] = $captcha['image'];
$data['mainpage'] = "contact";
// print_r($data);
$this->load->view('templates/template',$data);
在视图页面中显示验证码
<p id="captImg"><?php echo $captchaImg; ?></p>
答案 2 :(得分:0)
我成功完成了这段代码:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Captcha_Controller extends CI_Controller {
// Load Helper in and Start session.
function __construct() {
parent::__construct();
$this->load->helper('captcha');
session_start();
}
// This function show values in view page and check capcha value.
public function contact_mail() {
$this->load->helper('captcha');
$this->load->model('index_model');
$this->load->library('form_validation');
$this->form_validation->set_rules('captcha', 'Captcha', 'required');
if($this->form_validation->run()==FALSE){
if($this->input->post('submit')){
$inputCaptcha = $this->input->post('captcha');
$sessCaptcha = $this->session->userdata('captchaCode');
if($inputCaptcha === $sessCaptcha){
echo 'Captcha code matched.';
}else{
echo 'Captcha code was not match, please try again.';
}
}
// Captcha configuration
$config = array(
'word' => 'word',
'word_length' => 8,
'img_path' => 'captcha_images/',
'img_url' => base_url().'captcha_images/',
'font_path' => base_url() . 'system/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 50,
'word_length' => 8,
'font_size' => 16
);
$captcha = create_captcha($config);
// Unset previous captcha and store new captcha word
$this->session->unset_userdata('captchaCode');
$this->session->set_userdata('captchaCode',$captcha['word']);
// Send captcha image to view
$data['captchaImg'] = $captcha['image'];
$data['mainpage'] = "contact";
$this->load->view('captcha_view',$data);
}
else{
$result=$this->index_model->send_mail($this->input->post('email'));
if($result)
{
$this->flash->success('<h2 style="color:green">Thank You! Your Message has been sent</h2>');
redirect('contact');
}
else
{
$this->flash->success('<h2 style="color:red">Sorry ! Message sending failed</h2>');
redirect('contact');
}
}
}
// This function genrate CAPTCHA image and store in "image folder".
public function captcha_setting(){
$values = array(
'word' => 'word',
'word_length' => 8,
'img_path' => './images/',
'img_url' => base_url() .'images/',
'font_path' => base_url() . 'system/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 50,
'expiration' => 3600
);
$data = create_captcha($values);
$_SESSION['captchaWord'] = $data['word'];
// image will store in "$data['image']" index and its send on view page
$this->load->view('templates/template', $data);
}
}
?>