3

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.

getaway
  • 8,792
  • 22
  • 64
  • 94

3 Answers3

3

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.

Community
  • 1
  • 1
treeface
  • 13,270
  • 4
  • 51
  • 57
  • 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 Oct 23 '10 at 00:30
2

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
  • 349
  • 2
  • 12
1

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
  • 1,732
  • 5
  • 23
  • 35