flashdata不显示form_validation错误

时间:2018-02-13 04:59:51

标签: codeigniter session

以前我使用OpenServer并使用CodeIgiter开发了一个小型登录应用程序,一切都运行良好。但后来由于某些原因我将服务器更改为Xampp并将所有控制器,视图,模型和数据库复制到xammp服务器。我在配置文件和自动加载文件中进行了所有必要的更改。但是当从那里跑时我会遇到很多致命的错误。此外,不显示我通过flash-data()设置的form_validation错误。

你能否在这方面帮助我?

家庭控制器

<?php
 class Home extends CI_Controller 
  {

   public function index ()
    {
        $data['main_view']= "home_view";
        $this->load->view('Layouts/main',$data);
    }
   }
?>

用户控制器      

类用户扩展了CI_Controller  {

    public function show()
    {

        $this->load->model('user_model');           
        $data['reslt']= $this->user_model->get_users(3);
    //  $this->load->view('user_view',$data);   
        foreach( $data['reslt'] as $objec)
        {
            echo $objec->Username;

        }
    }

    public function addUser()
    {

        $uname="Mursal";
        $upass="Not good for you";
        $this->load->model('user_model');    
        $this->user_model->create_user(['username'=>$uname,
                                        'UserPassword'=>$upass],$id);
    }


    public function updateUser()
    {

        $id=3;
        $uname="Mursal";
        $upass="Not good for you";
        $this->load->model('user_model');    
        $this->user_model->update_users(['Username'=>$uname,
                                        'UserPassword'=>$upass],$id);
    }

    public function deleteUsers($id)
    {

        $this->load->model('user_model');   
        $this->user_model->delete_user($id);
    }

    public function login()
    {
            //echo $_POST["username"];

        //echo $this->input->post('username');
        $this->load->model('user_model');   

        $this->form_validation->set_rules('username','User Name', 'trim|required|min_length[3]');
        $this->form_validation->set_rules('password','Password','trim|required|min_length[3]');
        $this->form_validation->set_rules('confrm_pass','Confirm Password','trim|required|matches[password]');

        if($this->form_validation->run()==false)
        {
            $data=array(
                'errors'=>validation_errors()
            );

            $this->session->set_flashdata($data);
            redirect('home');
        }
        else
        {

            $name=$this->input->post('username');
            $pass=$this->input->post('password');

            $userid=$this->user_model->log_in($name,$pass);

            if($userid)
            {
                $usrdata=array(

                    'user_name'=>$name,
                    'user_pass'=>$pass,
                    'loged_in'=>true
                    );  

                $this->session->set_userdata($usrdata);
                $this->session->set_flashdata('login_success','You are now logged in');

                $data['main_view']="admin_view";
                $this->load->view('Layouts/main',$data);

                //redirect('home/index');
            }
            else
            {
                $this->session->set_flashdata('login_failed','Sorry');
                redirect('home/index');

            }

        }

    }


    public function log_out()
    {

        $this->session->sess_destroy();
        redirect('home/index');
    }   

 }
?>

用户模型     

类User_model扩展了CI_Model {

    public function log_in($name,$pass)
    {
            $this->db->where('Username',$pass);
            $this->db->where('UserPassword',$pass);

            $reslts=$this->db->get('users');

            if($reslts->num_rows()==1)
            {
                return $reslts->row(0)->ID;
            }

            else
            {
                return false;

            }
    }
 }
?>    

主视图

&#13;
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Document</title>
	<link rel="stylesheet" href="<?php echo base_url();?>assets/css/bootstrap.min.css"> 
	<script src="<?php echo base_url();?>assets/js/jquery.js"></script>
	<script src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script>

</head>
<body>
	
	<div class="container">
	
	<div class="col-xs-3">
		<?php $this->load->view('users/Login_view');?>
	</div>

	<div class="col-xs-9">
		<?php $this->load->view($main_view); ?>			
	</div>
	

</div>
</body>
</html>
&#13;
&#13;
&#13;

home_view

&#13;
&#13;
<h1>Hello, from home_view</h1>

<p class="bg-success">
	<?php if($this->session->flashdata('login_success')):?>

		<?php echo $this->session->flashdata('login_success'); ?>

	<?endif;?>	
</p>


<p class="bg-danger">
	<?php if($this->session->flashdata('login_failed')):?>
		<?php echo $this->session->flashdata('login_failed'); ?>

	<?php endif;?>


</p>
&#13;
&#13;
&#13;

管理员视图

&#13;
&#13;
<h1>Admin View </h1>
&#13;
&#13;
&#13;

登录视图

如果用户紫罗兰形成验证规则,则应在此处显示Flash数据错误,但不起作用。

&#13;
&#13;
<?php if($this->session->userdata('loged_in')): ?>

	<h2>Log Out</h2>
	<?php echo form_open('users/log_out');?>
	
	<p>
	<?php if($this->session->userdata('user_name')):?>
	<?php echo "You are logged in as " . $this->session->userdata('user_name');?>
	<?php endif;?>
	</p>
	
	<div class="form-group">
		<?php $data=array('name'=>'btnsbmt',
					   	'class'=>'btn btn-primary',
					   	'value'=>'Log out'
						);?>
		<?php echo form_submit($data);?>
	</div>


	<?php echo form_close(); ?>
<?php else:?>

<h1>Log In form</h1>
<?php $attributes=array('ID'=>'login_form', 'class'=>'form_horizontal'); ?>

<?php if($this->session->flashdata('errors')):?>

<?php echo $this->session->flashdata('errors'); ?>


<?php endif;?>

<?php echo form_open('users/login',$attributes); ?>

<div class="form-group">
<?php echo form_label('User Name:');?>

<?php  $data=array(

		'class'=>'form-control',
		'Name'=>'username',
		'placeholder'=>'Enter Your Name');?>

<?php echo form_input($data);?>
</div>

<div class="form-group">
	<?php echo form_label('Password:'); ?>

	<?php $data=array(
			'class'=>'form-control',
			'Name'=>'password',
			'placeholder'=>'Enter Your Password'
		);

	 ?> 
<?php echo form_password($data); ?>	 
</div>

<div class="form-group">
	<?php echo form_label('Confirm Password');?>

	<?php $data=array(
		'class'=>'form-control',
		'name'=>'confrm_pass',
		'placeholder'=>'Confirm Your Password',
		);?>

	<?php echo form_password($data);?>



</div>
<div class="form-group">

	<?php $data=array(
		'value'=>'Log In',
		'name'=>'btnSb',
		'class'=>'btn btn-primary'

		); ?>
	<?php echo form_submit($data);?>
</div>

<?php echo form_close();?>
<?php endif;?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

将其置于控制器中

if ($this->form_validation->run() == FALSE) {       
$errors = validation_errors();
           $this->session->set_flashdata('form_error', $errors);
           $this->load->view('Your View page');
}

将其放置在您的视图页面

<?php if($this->session->flashdata('form_error')): ?>
    <?php echo $this->session->flashdata('form_error');?>

<?php endif; ?>

我希望这会对你有所帮助..!