所以我正在尝试从登录屏幕转到管理员屏幕,并设置闪存数据,以便我可以告诉用户他们是否已登录,并检查是否有多个用户或错误的密码,它将显示抱歉您未登录。这是我的控制器。
public function insertInformation(){
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_check_if_email_exists');
$this->form_validation->set_rules('phone', 'Phone', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password_confirm', 'Password Confirm', 'required|matches[password]');
if ($this->form_validation->run() == FALSE){
$this->load->view('header_view');
$this->load->view('login_view');
$this->load->view('footer_view');
}else{
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone'),
'password' => $this->input->post('password')
);
$this->load->model('main_page');
$user_id = $this->main_page->storeRegisterInfo($data);
if($user_id){
$user_data = array(
'user_id' =>$user_id,
'name'=> $name,
'logged_in' => true
);
$this->session->set_userdata($user_data);
$this->session->set_flashdata('login_sucess','you are now loggedin');
$this->Admin();
}else{
$this->session->set_flashdata('login_failed','you are not loggedin');
$this->login();
}
}
}
这是插入数据的模式,我不确定如何检查用户 ID 是否存在以及检查用户是否有有效的登录信息。
public function storeRegisterInfo($data){
$insert = $this->db->insert('new_users',$data);
$result = $this->db->get('new_users');
if($result ->num_rows()== 1){
return $result->row(0)->id;
}else{
return false;
}
}
最后,对于登录功能中的视图,它是这样的。基本上这是一个注册表单,但我错误地在任何地方都写了登录,所以请不要混淆。
<p class="bg-danger">
<?php if($this->session->flashdata('login_failed')): ?>
<?php echo $this->session->flashdata('login_failed'); ?>
<?php endif; ?>
</p>
管理员也是如此。
<p class="bg-success">
<?php if($this->session->flashdata('login_success')): ?>
<?php echo $this->session->flashdata('login_success'); ?>
<?php endif; ?>
</p>
<h1>hello</h1>
所以我无法让闪存数据工作,也无法识别用户是否已经存在或写了错误的密码。
设置闪存数据后重定向页面
redirect('controllerNmae/Method');
<小时 />编辑 01
在视野中
<?php
if(!empty($this->session->flashdata('login_sucess')))
{
echo $this->session->flashdata('login_sucess');
}
if(!empty($this->session->flashdata('login_failed')))
{
echo $this->session->flashdata('login_failed');
}
?>
或
<?php
if(!empty($this->session->flashdata('login_sucess')))
{
?>
<script>
alert('<?php $this->session->flashdata('login_sucess'); ?>');
</script>
<?
}
if(!empty($this->session->flashdata('login_failed')))
{
?>
<script>
alert('<?php $this->session->flashdata('login_failed'); ?>');
</script>
<?
}
?>
提示: 使用此库提醒 JS。它提供了很好的用户体验
前任<script> alertify.success('<?php $this->session->flashdata('login_sucess'); ?>'); </script>