与codeigniter的表单验证问题

时间:2015-01-18 10:16:58

标签: php codeigniter

我正在尝试开发一个简单的登录系统,但我在数据验证过程中遇到了问题。

如果输入有效数据,一切正常。

但是当我输入无效数据时,它就像空用户或密码,它给我验证erorr,但之后如果我输入正确的用户数据。它不会工作,它只会重新加载相同的页面。 我注意到的奇怪的事情是地址栏上的网址越来越长。

我第一次点击提交按钮后的登录网址。

http://localhost/ci/login/validate

我第二次点击提交按钮后的登录网址。

http://localhost/ci/login/login/validate

每次点击提交按钮,它都会不断向网址添加额外的“/ login”。

知道问题是什么吗?

我的控制器

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

    class Login extends CI_Controller {
        public function index()
        {
            $this->load->view('login/header');
            $this->load->view('login/login_view');
            $this->load->view('login/footer');
        }
            public function validate()
            {
            $this->load->model('login_model');
            $this->load->library('form_validation');

            $this->form_validation->set_rules('username','Username', 'trim|required|min_length[6]|max_length[50]|valid_email|xss_clean');
            $this->form_validation->set_rules('password','Password', 'trim|required|xss_clean');

            //validation check
            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('login/header');
                $this->load->view('login/login_view');
                $this->load->view('login/footer');
            }
            else
            {
                $userdata['username'] = $_POST['username'];
                $userdata['password'] = md5($_POST['password']);

               //check whether user exist int the database or not 
                $result = $this->login_model->login_validation($userdata);

               //in case user exists                
               if($result == 1)
                {
                    $this->set_sessions_details();
                }
               //in case user doesnt exist
                else
                {
                    $data['flag'] = "Incorrect Username or Password!";
                    $this->load->view('login/header');
                    $this->load->view('login/login_view', $data);
                    $this->load->view('login/footer');
                }
            }
        }
//load a view in case user exists in the db.
        public function set_sessions_details()
        {
            $this->load->view('test');
        }
    }

我的模型用于检查数据库中的用户数据。

login_model     

 class Login_model extends CI_Model
 {
   function login_validation($userdata)
    {
      $user=$userdata['username'];
      $pass=$userdata['password'];
      $result = $this->db->get_where('users', array('username' => $user, 'password' => $pass));
      return $result->num_rows();
    }
 }
 ?>

我的观点:

login_view

<h3>Please Login</h3>
<form method='post' action='login/validate'>
  <div class="form-group" >
    <label for="username">Username</label>
    <input type="email" class="form-control" name="username" id="username" placeholder="Enter Username">
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control" name="password" id="password" placeholder="Enter Password"> 
  </div>
  <button type="submit" class="btn btn-success">Login</button>

  <?php echo validation_errors('<p class"errors">');?>
</form>
<?php if (isset($flag)){ ?>
  <p><?php echo $flag; ?> </p>
<?php } ?>

标题

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>WFD</title>


    <link href="<?php echo base_url();?>assets/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>

<div class='container'>
   <div class="header">
    <center><img src="<?php echo base_url();?>assets/images/logo.png" alt="logo" style="margin-top: 10px; padding-bottom: 20px; opacity: 1;">
      <h2>
         Welfare Foundation Database
      </h2>
    </center>
   </div>

页脚

    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="/ci/assets/js/bootstrap.min.js"></script>
      </body>
</html>

3 个答案:

答案 0 :(得分:0)

尝试使用表单助手链接中的表单开放标记:https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

echo form_open('email/send');

在后端将创建

看看这是否解决了您的问题。

答案 1 :(得分:0)

我相信,除了@Abhijit的回答,你的问题就是逻辑本身。你没有重定向到任何地方,你应该是。此外,您不必要地重复调用代码,例如:

$this->load->view('login/header');
$this->load->view('login/login_view');
$this->load->view('login/footer');

只应在一个函数中调用

public function index(){
   $this->load->view('login/header');
   $this->load->view('login/login_view');
   $this->load->view('login/footer');
}

public function validate(){
   // your validation logic

   if ($this->form_validation->run() == FALSE){

       $this->session->set_flashdata('error', array('msg' => 'Upps, something went wrong..', 'anotherParam' => 'anotherText'));

      // this will load again the views
      redirect('login');
      return;
   }
}

您现在可以按原样维护表单:

<form method="POST" action="login/validate">
</form>

<?php if($this->session->flashdata('error')){ ?>
<p><?php echo $this->session->flashdata('error')->msg; ?></p>
<?php } ?>

答案 2 :(得分:-1)

在控制器中使用url库:

$this->load->helper('url');

在视图中使用

<form action="<?php echo base_url('login/validate'); ?>" method="POST">
    <!-- your input --> 
</form>