2

Using php, how am I able to accomplish this? Ex:

1.) There are a total of let's say 5 pages (A, B, C, D, E) any user can view (either logged in or out)

2.) User X is viewing page (C) and is not logged in

3.) User X logs in on page (C) and is logged into that same page (C) that he/she is viewing rather than being redirected to the home page

I know how to make it where a user is redirected to the home page when any user logs in on any page, but I do not desire this and is something my users do not want - I would like it where any user can log in on whatever page they are viewing and it stays on that page for them rather than being redirected.

This is what I have for a user to login and be redirected with, but have no idea how to make it work for users being able to stay on whatever page the user is viewing when they login.

PHP

header("Location: index.php");

Is there more that is needed to allow users to stay on same page they are viewing when they login? Or is there a simple solution to this that I just can't seem to find using only that 1 line of code above?

I've read tons of tutorials/blogs/forums, etc...but couldn't find a definite answer anywhere. I did come across 'PHP_SELF' but have no idea how to implement that at all and php.net doesn't really have a clear solution to how to use it - (or at least from what I read)...Any help would be much appreciated.

SOLVED: --> Thank you for your help Thinkswan

$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
header("Location: $current_url");

UPDATE: --> A question that stems from the initial question

From what you provided above as the correct format, could we possibly take that a bit further? For example:

Same rules apply as previously mentioned.

1.) If User X is currently not logged in on page (C), and let's say page (C's) URL reads http://www.example.com/members.php, and there is a div container he/she clicks on that page, another bigger div container opens.

2.) Of Which the URL now reads http://www.example.com/members.php#registered_members

3.) While that is open, the User X decides to now log in. Is there a way to make it where once the User X logs in it stays on that same page with that bigger div container open? As of right now, the div container collapses once the User X logs in, but IS logged in on the same page.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
user2732875
  • 269
  • 5
  • 15

2 Answers2

3

You can make use of $_SERVER['REQUEST_URI'].

Example:

$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
header("Location: $current_url");

Answer to revised question:

The #registered_members part of the URL, known as the fragment identifier, is never sent to the server—it is client-side only. So PHP is never aware that even exists.

You can, however, check for it with JavaScript when the page reloads and then update the page based on whether it's present or not.

<script>alert(window.location.hash);</script>
Graham Swan
  • 4,818
  • 2
  • 30
  • 39
  • Brilliant - is that cross-browser compatible? And do I necessarily need the `http://` at the beginning there? – user2732875 Oct 06 '13 at 18:00
  • Answered my own questions above, Thank you `thinkswan` for you help! – user2732875 Oct 06 '13 at 18:11
  • For people who are learning, this may help: http://stackoverflow.com/questions/13772934/php-server-variables-serverhttp-host-vs-serverserver-name , http://stackoverflow.com/questions/4730798/what-is-the-difference-between-serverrequest-uri-and-getq and http://php.net/manual/en/function.header.php – pablofiumara Oct 06 '13 at 18:14
  • To expand a bit further on this, I'd like to keep what answer you provided `thinkswan`, however, is there a way to, lets say for example, a user to be viewing the page - not logged in, and has clicked on something that opened up - then decides to log in, instead of the container the user was viewing when clicked before he/she logged in collapsing, is there a way to keep that open when he/she logs in? As an example, if that user clicked on a container before logging in, the url looks like this: `http://www.example.com/profiles.php#registered_members`, is there a way to make that container stay – user2732875 Oct 07 '13 at 03:42
  • @user2732875 I don't fully understand what you're asking. Can you provide a pointform workflow? – Graham Swan Oct 07 '13 at 14:20
  • Morning `thinkswan`, I have made an update to my initial post in a pointform workflow. – user2732875 Oct 07 '13 at 15:00
  • @user2732875 I updated my answer to reflect your revised question. – Graham Swan Oct 07 '13 at 22:18
  • Would javascript or ajax work best in this type of scenario? Also, would this be better to use: `$(window).on('hashchange', function() { });` – user2732875 Oct 08 '13 at 01:34
  • @user2732875 Please read this: http://benalman.com/code/projects/jquery-bbq/examples/fragment-basic/. It sounds like you should spend some time learning about redirects and hashchanges to fully grasp how to update a view based on them. – Graham Swan Oct 08 '13 at 14:24
1

If you are including or calling the same login page from all of your form you can do one thing . Just take the current page url in a session variable then redirect it to that page.

Ashoka Mondal
  • 1,161
  • 4
  • 12
  • 28