how can a page redirect users back to previous pag

2019-04-08 21:22发布

I have a login page which won't be accessible if user is already logged in. So login page tries to redirect logged in users back to the page where they came from.

Redirect works if users click a link to go to a page. Problem is if users are in About page try to access login page via url then the referrer agent won't be set so login page is not redirecting users back to About page, instead it is redirecting back to the base url( i'm using codeigniter and ion auth library). login page's redirect code as below:

if($this->ion_auth->logged_in())
{
  redirect($this->agent->referrer(), 'refresh');
}

Is it possible to run this code and redirect properly instead of always redirecting to base url? When users are logged in i am not showing the link of the login page. So logged in users can only go to login page using url typing, and what i want is if they do they will be redirected back to the page where they came from.

4条回答
霸刀☆藐视天下
2楼-- · 2019-04-08 22:09

I'm doing this way.

redirect($_SERVER['HTTP_REFERER']);

查看更多
ゆ 、 Hurt°
3楼-- · 2019-04-08 22:18

I don't know if there is any better way, but i always do this:

When user is on About page and clicks on login, take the about page url address, do maybe base64_encode on it and then send it to login page via GET as a parameter. On login page, if credentials are valid, you take that parameter from GET, base64_decode and redirect there.

If you are doing that only for main pages, you can get only the controller from the url, but if you want to apply it on every page (/controller/method/var1/var2) then take the entire URL or entire url minus base url.

Let me know if this tip helped.

查看更多
ら.Afraid
4楼-- · 2019-04-08 22:21

Try this :

$this->load->library('user_agent'); redirect($this->agent->referrer());

else

Use SESSION for login and logout. if session exist block login page else allow login page using if statement.

查看更多
贪生不怕死
5楼-- · 2019-04-08 22:22

In the page that you want to go back to you can do:

$this->session->set_userdata('referred_from', current_url());

Then redirect back to that page

$referred_from = $this->session->userdata('referred_from');
redirect($referred_from, 'refresh');
查看更多
登录 后发表回答