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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
badwolfffAuthor Commented:

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?
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

badwolfffAuthor Commented:
Access is anonymous. I would like to use this feature on all products pages on my site.
Presumably each page will have a counter that will either increment or decrement as user view or leave the page.
You will use a common include JavaScript file? That will use a loop to call an process that updates while returning the current count.

I.e. Check_status (page,cookie_identifier) )the ip of the request is determinable.

The check_status will also have the cleanup duties.removing entries that have not been updated within the specified time I.e. Tge interval your client side scripts use to check, I.e. If the check is triggered every five seconds your delete interval will be that last update was more than 5 seconds ago.
badwolfffAuthor Commented:
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.
RobOwner (Aidellio)Commented:
There will be several ways to do this, with varying levels of accuracy.  Given this is anonymous, the only information you've have to identify a "user" is an IP address, and a session (that utilises a cookie on the client).

You will need to keep track of the Session ID, IP Address, page viewed, time viewed.

Initially you could "assume" that a user has stopped viewing that page after X minutes.

On the client side, your ajax script would run every minute? to just get the updated value.  There are more complicated plugins you can use for "push" notifications e.g. that you could use also.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Here's the problem with the question "how many people are viewing my site"  There is no notification that can tell you when a client leaves.  Your server will know when a client enters, because there will be an HTTP request, but there may be only one request (or a few) at the initial entry to the site.  For a better understanding of the client/server relationship, this article may be helpful:

To illustrate the issue, ask yourself whether I am looking at this page now.  Obviously I looked at it once, at least, because I posted some information.  But am I still here?  Is my browser window still open?  Or did I close my browser?  Or did I leave and go get coffee?  Or did I leave my browser window open, but switched to another page in another browser tab?  None of these "leave" events triggers any kind of HTTP request to the server.  I am like Schroedinger's Cat as far as you can tell.  As a result, my counsel is, "have fun developing the app, but do not rely on it for accurate information."
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.