Codeigniter 表单发布数据为空
Posted
技术标签:
【中文标题】Codeigniter 表单发布数据为空【英文标题】:Codeigniter Forms Post Data is empty 【发布时间】:2021-06-26 16:09:52 【问题描述】:我一直在尝试在 xampp 中本地设置现有的 CI 3.1 项目。数据库已连接,网站已加载,但登录/注册/表单无法正常工作。
详细检查后发现表单未在发布数据中发送。帖子对象为空。
下面是代码。
登录查看
<!-- language-all: lang-php-->
<form id="app-form" class="card auth_form" method="post" action="<?php echo site_url('auth/login/') ?>">
<div class="header">
<img class="logo" src="<?php echo base_url() ?>assets/images/logo.svg" >
<h5>Log in</h5>
</div>
<div class="body">
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Email" name="email" value="" required>
<div class="input-group-append">
<span class="input-group-text"><i class="zmdi zmdi-account-circle"></i></span>
</div>
</div>
<div class="input-group mb-3">
<input type="password" class="form-control" placeholder="Password" name="password" value="" required>
<div class="input-group-append">
<span class="input-group-text"><a href="<?php echo site_url('/auth/forgotpassword') ?>" class="forgot" title="Forgot Password"><i class="zmdi zmdi-lock"></i></a></span>
</div>
</div>
<!--<div class="checkbox">
<input id="remember_me" type="checkbox">
<label for="remember_me">Remember Me</label>
</div>-->
<input type="hidden" name="<?=$this->security->get_csrf_token_name()?>" value="<?=$this->security->get_csrf_hash()?>" >
<button class="btn btn-primary btn-block waves-effect waves-light ladda-button" data-style="expand-right">SIGN IN</button>
<div class="signin_with mt-3">
<p class="mb-0"><a href="<?php echo site_url('/auth/signup') ?>" title="Sign Up">Sign Up</a></p>
<p class="mb-0">or <a href="<?php echo site_url('/auth/forgotpassword') ?>" title="Sign Up">Forgot Password?</a></p>
<!--<button class="btn btn-primary btn-icon btn-icon-mini btn-round facebook"><i class="zmdi zmdi-facebook"></i></button>
<button class="btn btn-primary btn-icon btn-icon-mini btn-round twitter"><i class="zmdi zmdi-twitter"></i></button>
<button class="btn btn-primary btn-icon btn-icon-mini btn-round google"><i class="zmdi zmdi-google-plus"></i></button>-->
</div>
</div>
</form>
身份验证控制器
public function login()
$response = array('type'=>'', 'page'=>'', 'message'=>'');
// Load the model
$this->load->model('AuthModel');
// Validate the user can login
$result = $this->AuthModel->validate();
var_dump($_POST);
// Now we verify the result
if(!$result)
$response['type'] = 'error';
$response['message'] = 'Invalid username and/or password.';
else
$response['type'] = 'redirect';
$response['page'] = site_url().'RecordVerify/lists';
$response['message'] = 'Login successful';
echo json_encode($response);
身份验证
public function validate()
// grab user input
$email = $this->security->xss_clean($this->input->post('email'));
$password = $this->security->xss_clean($this->input->post('password'));
// Prep the query
$this->db->select("users.*,user_roles.role_name");
$this->db->from('users');
$this->db->join('user_roles', 'users.role = user_roles.URID');
$this->db->where('users.email', $email);
//$this->db->where('users.user_password', password_verify($password,'user_password'));
$this->db->where('users.status', 1);
$query = $this->db->get();
// Let's check if there are any results
if ($query->num_rows() != 0)
// If there is a user, then create session data
$row = $query->row();
$thehashvalue= hash('sha256', $row->salt . hash('sha256', $password) );
if($row->password == $thehashvalue)
$data = array(
'user_id' => $row->id,
'first_name' => $row->first_name,
'last_name' => $row->last_name,
'full_name' => $row->first_name.' '.$row->last_name,
'email' => $row->email,
'user_role_id' => $row->role,
'user_role_name' => $row->role_name,
'user_report' => $row->report,
'validated' => true
);
$this->session->set_userdata($data);
return true;
else
return false;
else
// If the previous process did not validate
// then return false.
return false;
【问题讨论】:
您的Sign up
函数似乎是一个锚标记,因此它只会在单击时导航(不提交表单数据)。 SIGN IN
按钮应该正在发送数据。也许尝试将 var_dump($_POST); die();
放在 Auth 控制器的顶部以验证您是否正在接收表单数据?
@dnapierata 我试过调试,帖子返回空。
查看这个问题:***.com/questions/35756622/…
【参考方案1】:
尝试先获取输入,然后将其传递给模型... 在您的控制器 Auth/login 中输入此代码
$email = $this->security->xss_clean($this->input->post('email')); $password = $this->security->xss_clean($this->input->post('password'));
然后通过调用将其传递给您的模型验证/验证
$result = $this->AuthModel->validate($email, $password);
代码应该是这样的...
授权控制器
public function login()
$email = $this->security->xss_clean($this->input->post('email'));
$password = $this->security->xss_clean($this->input->post('password'));
$response = array('type'=>'', 'page'=>'', 'message'=>'');
// Load the model
$this->load->model('AuthModel');
// Validate the user can login
$result = $this->AuthModel->validate($email, $password);
var_dump($_POST);
// Now we verify the result
if(!$result)
$response['type'] = 'error';
$response['message'] = 'Invalid username and/or password.';
else
$response['type'] = 'redirect';
$response['page'] = site_url().'RecordVerify/lists';
$response['message'] = 'Login successful';
echo json_encode($response);
授权模型
public function validate($email, $password)
// Prep the query
$this->db->select("users.*,user_roles.role_name");
$this->db->from('users');
$this->db->join('user_roles', 'users.role = user_roles.URID');
$this->db->where('users.email', $email);
//$this->db->where('users.user_password', password_verify($password,'user_password'));
$this->db->where('users.status', 1);
$query = $this->db->get();
// Let's check if there are any results
if ($query->num_rows() != 0)
// If there is a user, then create session data
$row = $query->row();
$thehashvalue= hash('sha256', $row->salt . hash('sha256', $password) );
if($row->password == $thehashvalue)
$data = array(
'user_id' => $row->id,
'first_name' => $row->first_name,
'last_name' => $row->last_name,
'full_name' => $row->first_name.' '.$row->last_name,
'email' => $row->email,
'user_role_id' => $row->role,
'user_role_name' => $row->role_name,
'user_report' => $row->report,
'validated' => true
);
$this->session->set_userdata($data);
return true;
else
return false;
else
// If the previous process did not validate
// then return false.
return false;
【讨论】:
以上是关于Codeigniter 表单发布数据为空的主要内容,如果未能解决你的问题,请参考以下文章