Link to home
Start Free TrialLog in
Avatar of badwolfff
badwolfffFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How could I achieve this using with MySQL + ajax and or php?

I need to show a counter on some pages on my website in this format:

"X people are currently viewing this page."

My thoughts were:
Since I have access to phpmyadmin I could manually create a table in my DB.
When a person visit a page on my site I would like to add the number 1 in a mysql cell for that page in the table I created above.
When he leaves I would like to do a -1 on the same cell.
Naturally this would have to take in account all visitors on the site so, if, when visitor A enters, there are already 30 visitors, it should show 31 as the number "x" above.

It would be great if this were in AJAX and could update in real time.
Alternatively at the very least I could make do with a pure jquery or php solution to start with but I will ideally want to do it in AJAX.

Any thoughts, links or code help would be greatly appreciated.
I need
Avatar of arnold
arnold
Flag of United States of America image

The increment part is the easy part, the leave is the difficulty.
The process should be to use an event/transactional type of a table, including an entry for when the visit began, while updating a column that keeps time when the last update occurred. The combination will give you the duration a person was on each page.
Initial/updated within a 5 second interval or the entry is seen as invalid.

Where your output is the count/group by the URL.
Avatar of badwolfff

ASKER

Hi,

thanks. Any code suggestions on achieving this? I would love to learn.
No, usually, seen those pages on an interactive site that uses something like a chat, etc. that has to maintain a connection as data flows back to the user.
Are you looking to include this on any page just as a way to indicate the number of users logged in.

You would likely have to use a JavaScript/client side scripting ...

On the backend side you would use something similar to insert into tablename () values () on duplicate key keepalive=now0(

If the page is interactive, you will have that when your client side checks in for an update.

It has to be fully worked out

Is access anonymous or some will have to login and those or all will be counted?
Access is anonymous. I would like to use this feature on all products pages on my site.
SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all the help arnold. The thing is, I do understand the theory and most of what you have said I already knew more or less. I was really hoping for code help or at least pointing towards any public domain websites or even commercial pieces of code that did just that. No offence, but I need to get this to work so I have to decide if I need to start from scratch or reuse something that already exists out there.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial