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.
In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that start with 3 , and a Location header holding the URL to redirect to. When browsers receive a redirect, they immediately load the new URL provided in the Location header.
Approach: To redirect from an HTML page to another page, you can use the <meta> tag by specifying the particular link in the URL attribute. It is the client-side redirection, the browsers request the server to provide another page.
There are two approaches used to redirect the browser window back. Approach 1: Using history. back() Method: The back() method of the window. history object is used to go back to the previous page in the current session history.
Under the Domain category, choose the Redirects menu. You'll see the Create a Redirect section. Here, you'll need to fill in which URL you want to Redirect and where you want it to Redirect To. Make sure your information is correct and choose the right connection protocol – HTTP or HTTPS.
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');
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.
I'm doing this way.
redirect($_SERVER['HTTP_REFERER']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With