Welcome Guest, Not a member yet? Register   Sign In
how to show online members with user_data in ci_sessions
#1

[eluser]lipolipo95[/eluser]
Hi everybody,

first sorry for my bad level in english.

I use a DB to store sessions.
I wanna show on my website a liste of online members (with their pseudo).

I think it could be possible by using the table CI_sessions that recieve sessions of users connected and visitors.

But I'm not skilled inought to manage with the column user_data.

The format that seems like :

a:5:{s:9:"user_data";s:0:"";s:2:"id";s:3:"135";s:6:"pseudo";s:15:"Léonarda";s:4:"sexe";s:1:"F";s:6:"logged";b:1;}

1/ do you think using the table CI_sessions is the best way to do what I need to do?
2/ if yes, could you please show me a example code (model / controller / view) that could help me because I'm not familiar at all with "serialize" "unserialize" "json_encode" and I really wish progress.

Please help Smile
#2

[eluser]CroNiX[/eluser]
I don't think this would be very efficient, as it would require you to:
1) directly query the database and get everybody whose "last_activity" is within the last x minutes.
2) Loop over the results from the query and decode each "user_data" field using the $session::_unserialize() method so that you can get their username in order to display it.

If you just wanted an "online count" of users, you'd just need to do #1 which is pretty simple, but if you wanted to also display their names you'd have to also do #2, which could be a lot to process if you have many users online.

It might be easier to just create a new db table to store the last_activity and the username and directly query that, so that you don't have to _unserialize() every row you get back from the db. So when the user "logs in" and you set their session data, you'd also do it in this simpler table in addition to the regular session data. You'd need some sort of clean up method that periodically removes expired users from that table.
#3

[eluser]lipolipo95[/eluser]
CroNix, thank you for your answer.

I had the process #1 + #2 in mind (even if I was not able to build #2 because of my low skills in serialize / unserialize) and I was also thinking that it's heavy when a lot of users are online.

I'm seduced by the solution you propose. But I have still a problem.

If I build a "last_activity" column in my "users" table.
When the user connects himself, the column will update because I will ask a model to do it.

But after?
How could I update it?

I thought about an update each time he goes on a new page but this solution is not enought because the user can speak for a while with other users in a same page during a lot of time without wishing to be disconnected.

That's why I was seduced by CI_session that update itself every time.

I can imagine an ajax system that update the column when the user click on one or an other button but it's also very heavy to do it for each page.

Do you have an idea?
Do you know how they do on social networks for example?
#4

[eluser]lipolipo95[/eluser]
Is it impossible to add a column "iduser" in CI_cession?
That would be so easy ...
#5

[eluser]Tpojka[/eluser]
[quote author="lipolipo95" date="1401356739"]Is it impossible to add a column "iduser" in CI_cession?
That would be so easy ...[/quote]

I will consider question as "Is it possible...?"

Code:
$this->session->set_userdata('iduser', $iduser);

You can find more on this page.
#6

[eluser]CroNiX[/eluser]
There are existing libraries/code examples to do this if you google for it.
#7

[eluser]lipolipo95[/eluser]
[quote author="Tpojka" date="1401360621"][quote author="lipolipo95" date="1401356739"]Is it impossible to add a column "iduser" in CI_cession?
That would be so easy ...[/quote]

I will consider question as "Is it possible...?"

Code:
$this->session->set_userdata('iduser', $iduser);

You can find more on this page.[/quote]

=> Tpojka, in this way your data are stored in the column "user_data" in this format :
a:6:{s:9:"user_data";s:0:"";s:6:"iduser";i:3;s:2:"id";s:3:"136";s:6:"pseudo";s:10:"christophe";s:4:"sexe";s:1:"M";s:6:"logged";b:1;}
Not in a column iduser into the table CI_sessions as I want.
#8

[eluser]Tpojka[/eluser]
I can see you have some value storred as "id" already there.
But without seeing rest of that part of code I am confused why "iduser" and "id" are different.
You just need to grab rows where ids are not empty.
Code:
$query = $this->db->where_not_in('id', '')->get('ci_sessions')->result();
You have fetched every row that is with id.
Lastly you have to manage users id on login/logout. When user logs out you update data.
Code:
$this->session->set_userdata('id', '');// you can remove my first idea of userid since you are already using id variable
JSON stringified user_data you have there are pretty much made for easy managing.
I don't know what would be adventage of new table column against allready made session object that allows you approach very easy.
But as CroNiX says, there are many libraries that are managing users and can be easily integrated into your application. Try with (ion_auth) for example.




Theme © iAndrew 2016 - Forum software by © MyBB