tags:

views:

45

answers:

3

im 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, im not sure thats the best way to do it, thanks!!

+3  A: 

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

http://stackoverflow.com/questions/189113/how-do-i-get-current-page-full-url-in-php

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.

treeface
cheers thanks mate!
getaway
Beware with HTTP_HOST, since it's generated from the client request and can be a entry point for XSS atacks http://shiflett.org/blog/2006/mar/server-name-versus-http-host
rmontagud
+2  A: 

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.

Kshitij Parajuli
+1  A: 

I'm probably a little too late but...

$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . 'index.php';

I also forgot you would put this after, this is what would actually take you back to the home page

header('Location: ' . $home_url);
Drewdin
Don't forget to `exit` or equivalent after sending `Location:` headers.
alex
+1 for the good catch
Drewdin