Welcome Guest, Not a member yet? Register   Sign In
Tracking user visits and last activity
#1

[eluser]stevefink[/eluser]
Hi all,

I'm looking for the most robust way of tracking users and their last activity within Code Igniter. Currently, if you use the native session class that CI ships with, there is a last_activity field available. However, I'm not sure if it's possible to embed a user id into this table without breaking the CI session class. I'd ultimately like to be able to query this table at any given time for my own stats collecting of users, their number of visits and last time they logged in.

I was curious if anyone has come up with a clever solution before I go work around looking at the Session class further?

I prefer not keeping last activity and visits in that table anyhow as garbage cleanup removes rows, so it's not the best place to keep long running stats.

Looking forward to ideas!


Thanks,

/sf
#2

[eluser]exodus7[/eluser]
Steve,

I'm sure there are other ways of doing this, but the way I do it goes like this:

In my login function, if the username & password are correct, I do the following:

Code:
$data['query'] = $this->loginmodel->curUser('users');
$newdata = array(
     'isLOGGED'  => 'YES',
     'curUser'     => $data['query']->username,
     'curUserId'     => $data['query']->id
);

$this->session->set_userdata($newdata);

So what that does is - it pulls up the record of the person who logged in and stores the username and ID of the user in a session which I can retrieve (as long as the session has not expired) anytime I need using: $this->session->userdata('curUserId');

I use the 'isLOGGED' value to determine if they are currently logged in to preform a specific task like posting a message or updating a record.

You can easily add a column or two in your users table to include the date of the last login or keep track of any other statistic like user rights and then store that in the session if needed...

It might/might not be the best way of doing it, but it works.

Does anyone know of an easier way?
#3

[eluser]Firestorm ZERO[/eluser]
Here's what I did to do a "Who's Online" feature on my site.

I added two new columns in the "session" table. One is "user_id". I also added others but let's just use this one.

I modified the set and get functions of the sessions so that if the key is "user_id", I set and get to the specific column instead of the "session_data" text column. Because putting like the "user_id" in the serialized text column will be hard to retrieve when there are lots of users (you will have to grab the data, parse through to get just the user_ids and then match them up). So now when I want to see who is online. I just have to do a SELECT DISTINCT on the "user_id" column and do a join.

EDIT:

I just re-read your post.

For "number of visits", I would add a column to your user table and modify the code whenever a new session is made to update the column in the user table. I guess you will have to clear this column everyday if you want daily visits or something.

For "last time they logged in", again put a new column in your user table and modify the code when the session is deleted to update the column in the user table with date.




Theme © iAndrew 2016 - Forum software by © MyBB