简体   繁体   English

Code Igniter-登录后如何重定向用户?

[英]Code Igniter - how to redirect user after login?

I've a Code Igniter project using database backed sessions. 我有一个使用数据库支持的会话的Code Igniter项目。 The web application is password protected, meaning that I have an abstract controller checking if the user is logged in before I allow him to see any pages, apart from the login form. 该Web应用程序受密码保护,这意味着我有一个抽象控制器来检查用户是否登录,然后再允许他查看除登录表单之外的任何页面。

While I had no problems implementing this, I'm having some difficulty understanding how to make the application redirect the user to the page he wanted to see if he need to login first. 尽管实现这一点没有问题,但我在理解如何使应用程序将用户重定向到他想查看是否需要首先登录的页面时遇到了一些困难。

How it goes: the user is logged out and types in a URL. 进行方式:用户已注销并输入URL。 The application detects he's not logged in so send him to the login page and creates a row in the ci_session table. 该应用程序检测到他尚未登录,因此将他发送到登录页面并在ci_session表中创建一行。 At the same time I store the url the user entered in the session object using either flashdata or userdata. 同时,我使用flashdata或userdata存储用户在会话对象中输入的URL。 My problem is that once the user logs in, the application will create a new row in the database, meaning a new session, completely ignoring the values I stored previously. 我的问题是,一旦用户登录,应用程序将在数据库中创建一个新行,这意味着一个新会话,而完全忽略了我之前存储的值。

Shouldn't it be one row per session? 每个会话不应该排成一行吗?

The CI URL Helper has a redirect function that you can use. CI URL Helper具有可以使用的重定向功能。 http://codeigniter.com/user_guide/helpers/url_helper.html http://codeigniter.com/user_guide/helpers/url_helper.html

Does a "header redirect" to the local URI specified. 是否将“标头重定向”到指定的本地URI。 Just like other functions in this helper, this one is designed to redirect to a local URL within your site. 就像该帮助器中的其他功能一样,该功能旨在重定向到您网站中的本地URL。 You will not specify the full site URL, but rather simply the URI segments to the controller you want to direct to. 您将不会指定完整的站点URL,而只是指定要定向到的控制器的URI段。 The function will build the URL based on your config file values. 该函数将根据您的配置文件值构建URL。

The optional second parameter allows you to choose between the "location" method (default) or the "refresh" method. 可选的第二个参数允许您在“位置”方法(默认)或“刷新”方法之间进行选择。 Location is faster, but on Windows servers it can sometimes be a problem. 位置更快,但在Windows服务器上有时可能会出现问题。 The optional third parameter allows you to send a specific HTTP Response Code - this could be used for example to create 301 redirects for search engine purposes. 可选的第三个参数允许您发送特定的HTTP响应代码-例如,可以将其用于创建301重定向以用于搜索引擎。 The default Response Code is 302. The third parameter is only available with 'location' redirects, and not 'refresh'. 默认响应代码为302。第三个参数仅在“位置”重定向中可用,而在“刷新”中不可用。 Examples: 例子:

if ($logged_in == FALSE)
{
     redirect('/login/form/', 'refresh');
}

// with 301 redirect
redirect('/article/13', 'location', 301);

I think you're misunderstanding how sessions works between a browser and your web application. 我认为您误会了会话在浏览器和Web应用程序之间的工作方式。 When a user opens your login page, they are assigned a unique session ID which codeigniter keeps track of. 当用户打开您的登录页面时,将为他们分配一个唯一的会话ID,codeigniter会对其进行跟踪。 Unless your session gets expired, either forcefully by logging out or due to your own session expire settings, codeigniter should only be writing 1 row per unique session in your database. 除非您的会话过期(通过注销强制退出或由于您自己的会话过期设置),否则codeigniter仅应在数据库中的每个唯一会话写入1行。 Make sure you have your sess_expiration variable in config.php set to something realistic. 确保将config.php中的sess_expiration变量设置为实际值。

I don't see how removing the underscore from your cookie name could have fixed this, as the name has nothing to do with how sessions work in general. 我看不出如何从Cookie名称中删除下划线可以解决此问题,因为该名称与会话的一般工作方式无关。

You can user something like this. 您可以使用类似这样的东西。

When the user tries to access a page like 当用户尝试访问类似的页面时

http://test.com/userpage.php

If he is not logged in, redirect him to 如果他未登录,请将其重定向到

http://test.com/login.php?redirectpage=userpage.php

(This redirect will be done by userpage.php after checking the login status from the cookie or the session.) (此重定向将在检查cookie或会话的登录状态后由userpage.php完成。)

The login page has the value "redirectpage" and once the user logs in at the login page, redirect him to the page he was previously trying to visit. 登录页面的值为“ redirectpage”,一旦用户在登录页面上登录,就将其重定向到他先前尝试访问的页面。

You will have to check the user login status in all the pages that you need the user to be logged in. 您将必须在需要用户登录的所有页面中检查用户登录状态。

Solved it. 解决了。

My problem was not how to redirect or how to store data. 我的问题不是如何重定向或如何存储数据。 My problem was the application creating two sessions per request. 我的问题是应用程序为每个请求创建两个会话。

I changed the name of my cookie to something that didn't include underscores and voila, fixed. 我将Cookie的名称更改为不包含下划线和“瞧”的固定名称。 One session per request and everything works as it should. 每个请求一个会话,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM