如果用户尝试通过url访问页面,页面如何将用户重定向回上一页? [英] how can a page redirect users back to previous page if they try to access the page via url?

查看:121
本文介绍了如果用户尝试通过url访问页面,页面如何将用户重定向回上一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录页面,如果用户已经登录,将无法访问该登录页面.因此,登录页面尝试将登录用户重定向到他们来自的页面.

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.

重定向有效.问题是,如果用户在About页面中,尝试通过url访问登录页面,则不会设置referrer agent,因此登录页面不会将用户重定向到About页面,而是将其重定向回到(我正在使用codeigniter和ion auth库). 登录页面的重定向代码如下:

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');
}

是否可以运行此代码并正确重定向,而不是始终重定向到基本url? 当用户登录时,我没有显示登录页面的链接.因此,登录用户只能使用url输入进入登录页面,而我想要的是,如果他们这样做,他们将被重定向回他们来自的页面.

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.

推荐答案

在您要返回的页面中,您可以执行以下操作:

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

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

然后重定向回该页面

$referred_from = $this->session->userdata('referred_from');
redirect($referred_from, 'refresh');

这篇关于如果用户尝试通过url访问页面,页面如何将用户重定向回上一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆