1

i just created a user login system with php session and now users can register and login to site and do another things...

now i want to create online.php which will fetch all online users.i almost search everything in google and stackoverflow for this with no success.

ok now i want to describe the system which i want to create with high performance.. when a user logged in we just update table user.lastlogin which is a timestamp and then in online.php we SELECT * every user where time interval is < 5 minutes. for this purpose i can update this timestamp lastlogin field in database when user load each page,and this cost many mysql query to do the job... then in each page load i have to update

UPDATE user set last=now()

that will cost me many mysql query.now i am looking for some another way like using sessions or something that i found in this link

"The normal solution is to store a timestamp in the table which you update every time the user does something. Users with a recent timestamp (say, five minutes) are shown as logged in, everybody else are logged out.

It doesn't even have to be updated on every page load. Keep a timestamp in the session with the last update time as well, and only update the table when the database flag are about to expire."

but unfortunately the answer wasnt quite helpful and i need an example or more describe on this.

Community
  • 1
  • 1
HiDd3N
  • 494
  • 6
  • 23
  • or if there is better way out there.please direct me to doing this. – HiDd3N Nov 02 '12 at 12:48
  • are your bound to mysql or is it possible to add other datastore which fits better in this scenario? – Jan Prieser Nov 02 '12 at 13:00
  • how are your sessions stored? – Jan Prieser Nov 02 '12 at 13:00
  • Isn't there a way to just count all the open sessions server-side in PHP on that website? Like a system variable? BTW, You're not alone: http://stackoverflow.com/questions/679657/find-number-of-open-sessions – Ariaan Nov 02 '12 at 13:01
  • "We `select *`". If you're worried about performance, you might want to `select count(1)` instead. Also, yes, every pageview would lead to a query, but then, how many queries do you actually expect? One query (especially an optimised one) shouldn't be a bottleneck. You're probably stepping into the realm of **premature optimisation**. That's a bad place to be. – Berry Langerak Nov 02 '12 at 13:29
  • @Ariaani i dont want to count all users i want to select some sort of information from users then counting is not a solution in this i think – HiDd3N Nov 02 '12 at 15:14

1 Answers1

1

1.) If you need more speed in sessions there is for example memcache. A simple key - value store to save your sessions or keys. You can configure Apache that all sessions will be automaticly stored in memcache.

2.) Another solution is to make a MEMORY Table which MySQL holds in RAM. Problem is when you restart your server the data in this table is lost. But i think in your case for last logon there is no problem.

3.) PHP Shared Memory: http://php.net/manual/de/ref.shmop.php for small data.

When you have implemented a solution i would prefer the MEMORY Table or the SHM solution.

Edit: The last paragraph with the searching on HDD was related to the comment on top: Find Number of Open Sessions

Community
  • 1
  • 1
René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • minor language suggestions, because it might be unclear (so no offense meant). In the last paragraph, should it be ``Then searching``->``Because searching`` and ``When you have implemented``->``If I had to implement``? – Jonas Schäfer Nov 02 '12 at 13:35
  • I have deleted the sentense it was related to the answer on top. – René Höhle Nov 02 '12 at 13:52
  • realy tnx for answer...then using memory table and update in each page change will be the best way for doing this??i just looking some way that i dont have to use some query for each page load because i think updating in each page load will damage performance of my site.if we have 100000 user online for example then 100000 query in one second would be the best solution?? and server can handle it?? – HiDd3N Nov 02 '12 at 15:08
  • i liked your number 2 solution because i update lastlogin in each login then there will be no problem when the server crash or restarted but i have another question..in this memory table how many rows i have to make??should i delete this timestamp row that i created before in table `user` and add this timestamp row to another table in memory format which will update on each page load???..i didnt get this well please explain this second method more.realy tnx :) – HiDd3N Nov 02 '12 at 16:02