Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can a page redirect users back to previous page if they try to access the page via 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.

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.

like image 847
user1906399 Avatar asked Oct 02 '14 19:10

user1906399


People also ask

How does a URL redirect work?

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.

How do I redirect from one page to another?

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.

How do I redirect a previous page after login?

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.

How do you redirect a URL?

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.


3 Answers

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');
like image 141
westcoast Avatar answered Nov 15 '22 12:11

westcoast


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.

like image 34
151291 Avatar answered Nov 15 '22 11:11

151291


I'm doing this way.

redirect($_SERVER['HTTP_REFERER']);

like image 39
Mitul Lakhani Avatar answered Nov 15 '22 12:11

Mitul Lakhani