Anuj Hari
Anuj Hari

Reputation: 543

Check last visited between two php pages

Is there some sort of PHP code that allows me figure out which of the two pages was last visited.

Here is why i need it.

I have 3 pages called:

user-management.php, manage-membership.php and manage-user.php

There are two ways of getting to manage-user.php. One is to click on the name of the user in user-management.php and the other is to click on the membership account holder in membership-management. Both user-management and membership-management are completely different pages so please don't tell me to merge them to make it easier, because it won't get easier.

What i want to do is track where i'm coming from.

For example, if i'm going to manage-user.php from user-management.php, when all the editing is done, i want it to redirect back to user-management.php, and the same for membership-management.

How do i check to see which of the two pages I came from and redirect back to those pages accordingly?

Upvotes: 1

Views: 690

Answers (4)

developerwjk
developerwjk

Reputation: 8659

You could use $_SERVER['HTTP_REFERER']; but the user can also set their browser to not send the referer header. You could also do something like this:

At beginning of page:

session_start();
$lastVisited = $_SESSION['last_visited'];

At end:

$_SESSION['last_visited'] = $thisPagename;

Upvotes: 0

Marc B
Marc B

Reputation: 360862

Have each of your scripts record their name in the $_SESSION, so you're keeping track of where you came from:

user-management.php:

$_SESSION['came_from'] = 'user-management.php';

and then in your manage-user.php script:

<a href="<?php echo $_SESSION['came_from'] ?>">Back</a>

This is more reliable than using the HTTP referer, because not everyone sends refers, or sends the ACTUAL referer.

Upvotes: 2

moreasd
moreasd

Reputation: 21

Tried this? -> $_SERVER["HTTP_REFERER"]

You can read more from this: http://www.electrictoolbox.com/php-http-referer-variable/

Upvotes: 0

Philip
Philip

Reputation: 1078

you could use $_SERVER['HTTP_REFERER']; But this is not so safe, better store the page in a session and check it then.

session_start();
...
$_SESSION['log'][] = $_SERVER['PHP_SELF'];
...
if ($_SESSION['log'][count($_SESSION['log'])-1] == "xxx") {
  do code...
}

Upvotes: 1

Related Questions