how do I track the last visited page?

Loganathan Natarajan
Loganathan Natarajan used Ask the Experts™
I have a web site (php & mysql) with user authentication and rights in it. I want to keep track the user last visiting page.

if he log-off from page 5, i need to keep track and when he logs in , to be redirected the page.

what are the ways to do this task and so that user will be taken to the last page visited in it.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
have a link to logout, user clicks on this link and is taken to the page where you have written code to log user out,, where you have unset sessions and cookie. here get the last page from $_Server['HTTP_REFERER'], save this url in db and when user logs in again,, get this value from db and redirect user to this page after successful login.
If your database can handle it, you might decide to update user's data at each visited page: as soon as s/he fires the page, launch your  usual session_start() then write the url of the visited page into the db.

BEWARE: although this is doing what you require... it might create a problem with data in your URL ($_GET type) or in a form ($_POST or $_GET type)


Is there anyway to track if user don't click logout button and closes the window ?
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

I'm afraid not.
You have 2 ways to record your tracking result:
- when the user does something special, like clicking some buttons -- problem is that if they don't click, you do not track / record
- every time they load a page -- problem is that this creates some heavy load on the sql server if you record each page -- this can be lightened however if you do it only for some key pages (this solution is not very different of the usual web log, whether the classical Apache log on your server or what you get from Google Analytics).

So when coming back, the user would be placed at the last "key page" s/he visited.

These solutions assume that users are changing machines from which they call your site.
If each visitor was visiting always from the same machine, you could place on this machine a cookie which would keep the information updated.
This might be a complement: if your site fonds back the cookie, then it replaces user exactly at the same place that was visited FROM THIS MACHINE, otherwise it uses the "key page info" recorder into the database


@ fibo

thanks for your explanation,

actually, the user whereever goes, i need to show the last visited page to him.. it is about like quiz pages .. if he is in 3rd step and log off, he has to be redirect to the page...

i am thinking to record this with database, so that next time user comes, he will be redirect to the step 3 page.. etc.,

my problem is, how do I find if he did not click "logout" link...
first:  record every page visit to db,
second: you can write a cron job,, that will check the db for each user' last page visits,, if more than specified time, lets say 20 min has past after the user's last visit, then you can consider that user has closed the window without properly logging out
If you record the page address when s/he loads it, you do not have to bother for a logout (although that might be an interesting info): the last page s/he has browsed to is the one that is recorded in the db for this user.

There are in fact 2 different problems:
- the one you stated: what was the last page visited by user xx --> this can be handled, ONE THE USER HAS LOGGED IN, by recovering the corresponding info from the db (and from now on, it will be updated each time this user navigates to a tracked page.
- handling the session: how do we know that the user must log in? This can be handled by the session parameters: you can consider that if the last page was recorded more than xx minutes ago, then the session has ended (even if from the same IP)

If I was to implement this, I would probably record for the "last connection info" the IP address of the client station, and the session ID as delivered by php, as well as the the page and a timestamp (DO NOT record the password in the session or a token; if you need to re-check it, store a MD5 of the password or, preferably, a combination of IP address and password

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial