CodeIgniter登录所有页面 [英] CodeIgniter login across all pages
问题描述
我对CI的MVC模型很新。
我有一个简单的页面,由3个视图组成。标题,内容和页脚。
在我的标题中,我有一个登录 >形式。因此,整个网站都会有一个登录表单。因为它出现在每个具有差异模型的页面上:
我将如何编写脚本以记录用户,因为我不喜欢写作
也许是另一个例子:
$ b
如果还不清楚,我试试另一个例子。假设我有一个网站。在我加载一个视图,在我的所有页面的页脚。并且页脚包含一个表单。现在,当我使用该表单发布数据时,应该将数据放在哪里?
显示它去处理表单中的所有帖子的专用PHP文件,它去到当前页面的控制器?
我希望我很清楚。再次,我刚开始使用CI和MVC一天前。
当我使用该表单发布数据时,
您不应在每个页面(控制器)上写入逻辑。您应该做的是创建一个用于管理用户日志记录的不同的控制器(如 Accounts
)。
登录表单将数据发布到 accounts / login
URL。但要返回上一页,我们应该将当前网址存储在 referer
。
,只需在每个需要的页面上的会话中存储 uri_string();
。
$ this-> session-> set_userdata(array(
'referer'=> $ this-> uri-> uri_string()
));
如果您不需要将用户重定向到上一页,请忽略它。
下面的示例说明如何实现目标:
{
public function login()
{
$ config = array(
//这里写你的验证规则
);
$ this-> load-> library('form_validation');
$ this-> form_validation-> set_rules($ config);
//检查表单是否提交
if(isset($ _ POST ['submit'])){
if($ this-> form_validation- > run()== FALSE){
$ data ['login_errors'] = validation_errors();
//在会话中存储验证错误
//在每个需要的页面上显示
$ this-> session-> set_userdata($ data);
} else {
//登录过程
//在会话中设置用户详细信息
//登录后重定向到上一页
if($ referer = $ this-> session-> userdata('referer')){
$ this-> session-> unset_userdata('referer');
} else {
$ referer ='';
}
redirect(base_url()。$ referer);
}
}
}
public function logout()
{
//销毁会话
$ this-> session-> sess_destroy();
redirect(base_url());
}
}
I'm am very new to CI the MVC model.
I have a simple page which is made of up 3 views. Header, Content and Footer. The content will be unique across the site but header and footer will be the same, no matter what page.
In my header I have a login form. So there will be a login form across the whole site. since it appears on every page which has diff models:
how will or where will I write the script for logging in a user as I don't fancy writing a login script on every model the header is used on...
Maybe another example:
If that's not clear, I try another example. Suppose I have a site. In that I load a view which is a footer in all my pages. and the footer contains a form. Now when I post data using that form, where should that data it go?
Show it go to a dedicated PHP file which handles all the posts from that form, or should it go to the current page' controller?
I hope I was clear. Again I Just started using CI and MVC a day ago. It would be nice if you could guide me.
When i post data using that form, where should that data it go?
You shouldn't write the logic on every page (controller). What you should do is creating a distinct controller (like Accounts
) for managing user logging.
The login form should post the data to accounts/login
URL. but to get back to the previous page, we should store current URL in session as referer
.
To do that, just store uri_string();
in the session on each page you need.
$this->session->set_userdata(array(
'referer' => $this->uri->uri_string()
));
If you don't need to redirect the user to the previous page, ignore it.
The example below indicates how to achieve the goal:
class Accounts extends CI_Controller {
public function login()
{
$config = array(
// Write your validation rules here
);
$this->load->library('form_validation');
$this->form_validation->set_rules($config);
// Check whether form is submitted
if (isset($_POST['submit'])) {
if ($this->form_validation->run() == FALSE) {
$data['login_errors'] = validation_errors();
// Store validation errors in session
// to display on every page needed
$this->session->set_userdata($data);
} else {
// Login process
// Set user detail in session
// Redirect to previous page after login
if ($referer = $this->session->userdata('referer')) {
$this->session->unset_userdata('referer');
} else {
$referer='';
}
redirect(base_url().$referer);
}
}
}
public function logout()
{
// Destroy the session
$this->session->sess_destroy();
redirect(base_url());
}
}
这篇关于CodeIgniter登录所有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!