-2

I have to maintain a count of the number of users that has been logged in. Is there any way in PHP to do it without using database or creating a file to store details ?

No framework used, just simple plain PHP.

Hrqls
  • 2,944
  • 4
  • 34
  • 54
Tejas
  • 585
  • 3
  • 15
  • 1
    @matiit session is user dependent. Cannot track site metrics – shatheesh Jun 04 '14 at 10:04
  • @matiit wanted to say the same but sessions also use either file- or database-based storage mechanisms ... – SaschaM78 Jun 04 '14 at 10:04
  • You can't. The only way to store something out of a database is through a session. And those are user-side. A user can not know which(and how many) other users are on your website without connecting to the server. Thus you need a database or something to store a value which the user can pull out. – CaptainCarl Jun 04 '14 at 10:04
  • If you are free to use `redis`, try this [http://redis.io/commands/incr](http://redis.io/commands/incr) – shatheesh Jun 04 '14 at 10:05
  • @shatheesh - okay. but not using redis – Tejas Jun 04 '14 at 10:07

2 Answers2

2

Your options are literally, a data store or a file. You could alternatively offload them to some remote server which does something or find some sort of "remember some data" third party API, but realistically it needs to be a data store or a file.

Files would be kinda slow at this and you would get race conditions, so you'd be right to avoid those.

A MySQL Database or the like is possibly also overkill if you are not currently using one.

Another option (as @shatheesh pointed out) is Redis.

http://redis.io/commands/incr

I would suggest you do that. :)

Phil Sturgeon
  • 30,637
  • 12
  • 78
  • 117
0

You can achieve this using a session variable and storing count in same.

For further understanding you can visit this link :

How do I expire a PHP session after 30 minutes?

Community
  • 1
  • 1
Chhavi Gangwal
  • 1,166
  • 9
  • 13