我是CI的新手,通常会尝试将一些信息传递回如下所示的登录页面…
public function authenticate()
{
$this->load->model('User_model', '', true);
if ($this->User_model->authenticate($this->input->post('username'), $this->input->post('password')))
{
$this->session->set_userdata('loggedin', true);
header('Location: /');
}
else
{
header('Location: /sessions/login/error?='.'1');
}
}
然后在登录页面使用_GET。
那么在CodeIgniter中我该如何做呢?
首先,对电子邮件/密码等敏感数据使用GET通常是不允许的。一直都是坏习惯。
但是你的问题的答案在这里- http://www.codeigniter.com/userguide3/libraries/input.html
$this->input->get('username')
等于$_GET['username']
。您可以使用$this->input->get('username', TRUE)
来转义一些恶意代码。
且$this->input->post('username')
= $_POST['username']
。这是我建议你使用的(它需要修改你的HTML)
根据我的理解,您想为错误代码传递_GET
变量,对吗?如我所见,你正在使用_POST
变量的形式,因为你应该。
当您使用Codeigniter时,您可以使用框架的URI结构,而不是使用_GET
工作和MVC结构。
所以,与其重定向这个:
header('Location: /sessions/login/error?='.'1');
重定向:
header('Location: /sessions/login/error/1');
然后在控制器中为登录,在error方法中:
function error($msgId = 0) {
// here you will get the number you sent in the url as $msgId
}
如果您仍然需要使用_GET
变量,这也是可能的。转到框架/aplication/config/config.php
的主应用程序配置文件,查看Enable Query Strings
部分