Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking the user back to the initial page after login?

Tags:

html

php

get

I'm using PHP, I wanted to know if a user lands on a certain page, and they have to login or signup to be able to do actions like commenting or rating etc. I wanted to able to take the user back to that same page after they login.

I was thinking to use the PHP get function and pass the URL, I'm not sure that's the best way to do it.

like image 769
getaway Avatar asked Dec 29 '25 11:12

getaway


2 Answers

Presumably your login fields are in an html form. When you're constructing the form, put the current URI in a hidden input field, then you have the information available to you when you want to perform the redirect after the POST data has been submitted.

I don't typically use PHP without a framework, but here are some resources that will help you get this information:

http://www.webcheatsheet.com/PHP/get_current_page_url.php

How can I get the current page's full URL on a Windows/IIS server?

Basically, it goes like this:

$current_url = "http://" .$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];

But if you are using https, you have to change that string accordingly.

like image 163
treeface Avatar answered Jan 01 '26 02:01

treeface


An alternative is to redirect them back to $_SERVER['HTTP_REFERER']. One thing to take into consideration is that the browser can choose not to send the Referer URL.

like image 34
Kshitij Parajuli Avatar answered Jan 01 '26 03:01

Kshitij Parajuli