Avatar of badwolfff
badwolfff
Flag 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
AJAXMySQL ServerPHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
arnold

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.
badwolfff

ASKER
Hi,

thanks. Any code suggestions on achieving this? I would love to learn.
arnold

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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
badwolfff

ASKER
Access is anonymous. I would like to use this feature on all products pages on my site.
SOLUTION
arnold

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
badwolfff

ASKER
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
Rob

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.