Welcome Guest, Not a member yet? Register   Sign In
user is online
#1

[eluser]spherop[/eluser]
i am interested in implementing some sort of user is online functionality as best as can be done given the web's architecture of course.

would i be able to query the session table using cl_auth? seems like session_user_id is always 0 tho?

thanks for any insights.
#2

[eluser]marinaccio[/eluser]
I would be interested in working on this with you. Look forward to your reply.
#3

[eluser]tdktank59[/eluser]
[quote author="spherop" date="1218689757"]i am interested in implementing some sort of user is online functionality as best as can be done given the web's architecture of course.

would i be able to query the session table using cl_auth? seems like session_user_id is always 0 tho?

thanks for any insights.[/quote]

Thats because you need to set a user_id...

These are the values CI Session class will populate automatically by using the session control built in with CI
Quote:[array]
(
'session_id' => random hash,
'ip_address' => 'string - user IP address',
'user_agent' => 'string - user agent data',
'last_activity' => timestamp
)

However there is a 5th one called "user_data" that is used to store anything other than those 4 listed above in the database.

You can however assign any session with any data you see fit.
by doing one of these 2 methods

Method 1:
Code:
$this->session->set_userdata('some_name', 'some_value');

Method 2:
Code:
$newdata = array(
                   'username'  => 'johndoe',
                   'email'     => '[email protected]',
                   'logged_in' => TRUE
               );

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

And you can call back any data that you have set by doing this:
Code:
$this->session->userdata('item');

So in theory you could look up any session that has been alive for less than lets say 15 minutes (normally how long it takes a session to die... in my experience) or just use all sessions since there is a cleanup script that runs every once in a while... Where a user_id exists and this will give you a pretty good estimate of how many users are online at a given time.

Post if you still need some help.


Source: http://ellislab.com/codeigniter/user-gui...sions.html
#4

[eluser]marinaccio[/eluser]
If you just wanted to get all users (not just logged in users), you could do a simple query to the ci_sessions table, and pull back distinct records based on ip_address and user_agent.

That would give you a good estimate of how many users are viewing your site at that moment. I put together a simple library to implement this.

It is meant to be a very simple example, not a sophisticated one. As it does not take into consideration users behind a proxy, distinguish users from spiders, logged in vs. not logged in, etc.

Here is a link to it:
http://marinacciofamily.biz/code/ci-users-online/

If anyone has feedback, feel free to post. If there is some constructive criticism, I would be more than happy to implement them into this, and eventually turn it into something sophisticated. If you haven't guessed already, I am a noob, and yes, this is my first contribution to this forum, actually my first piece of 'released' code. I look forward to your feedback.




Theme © iAndrew 2016 - Forum software by © MyBB