Anup Kumar
Anup Kumar

Reputation: 11

how to know how many pages a particular user visited in a particular session

i am working on a e-commerce website, i want to monitor how many pages a users visited in a particular session. there are only registered users of the site. can any one tell me how to record the URL of the user of a particular session, i am using php and mysql

Upvotes: 1

Views: 1329

Answers (4)

Abdul Khaliq
Abdul Khaliq

Reputation: 2285

I made a base class that is inherited by System.Web.UI.Page like

 public class BasePage : System.Web.UI.Page
{
    protected virtual void Page_Load(object sender, EventArgs e)
    {
    }
}

Note: make base class Page load event Virtual and other pages Page load event override and then call base class Page load event in every page Page load event like

base.Page_Load(sender, e);

Every page is inherited through this base class like

protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
    }

I did it like this way,

        string pageName = Path.GetFileName(Request.PhysicalPath);
        string serverName = HttpContext.Current.Server.MachineName;
        string sessionId = HttpContext.Current.Session.SessionID;
        string ipAddress = HttpContext.Current.Request.UserHostAddress;

then insert these values in your database through SQL or LINQ query...

I inserted through LINQ like

VisitorInformation vPanel = new VisitorInformation()
        {
            PageName = Path.GetFileName(HttpContext.Current.Request.PhysicalPath),
            ServerName = HttpContext.Current.Server.MachineName,
            SessionId = HttpContext.Current.Session.SessionID,
            IPAddress = HttpContext.Current.Request.UserHostAddress,
            VisitorDate = Convert.ToDateTime(System.DateTime.Now.ToString()),
        };
        new BaseDAL().AddVisitorInfo(vPanel);

Upvotes: 0

Savid
Savid

Reputation: 311

Have a mysql table

UserID    SessionID     URI
1         XYZ           /
1         XYZ           /contact
1         ZYX           /contact

Each page that loads insert this into the table

$query = "INSERT IGNORE INTO visits  (`UserID`, `SessionID`, `Uri`)
  VALUES ($userID, " . session_id() . ", " . mysql_real_escape_string(['REQUEST_URI']) . ")";

later you can check a session of a user and count up all the different pages with

SELECT COUNT(URI) FROM visits WHERE UserID = $userID GROUP BY SessionID

you could also associate a datetime with the session

Upvotes: 0

Dan Lugg
Dan Lugg

Reputation: 20592

At some point in the application you could simply perform:

$_SESSION['history'][] = $_SERVER['REQUEST_URI'];

Thus appending the current URI to the session's history array. You could get more intuitive, such as checking whether the URI exists already, calculating the time difference between pages for reporting purposes, etc.

Upvotes: 2

Starx
Starx

Reputation: 78971

There are more than one way to do this. Here is one quick walkthrough:

  1. Create a table with two fields ('session_id', 'url');

  2. Whenever the site loads, read the sessionid using session_id(), and use this value to store the url of the page in a table for every pages the people visit.

  3. Once a page loads, read the URL, through $_SERVER['REQUEST_URI']; and then use it the value of session_id() and store the records.

After you implement this simple technique. you can see how many people see the pages, using a simple query

SELECT count(*) FROM <YOURSESSIONTABLE> GROUP BY `<session_id_field>`

P.S. This is one of the basic example, and should be changed to fit your requirements

Upvotes: 1

Related Questions