How to know if there is a visitor on my website and also when the visitor left the website (i am creating a chat application)

Hi,
I am creating an application for live chat with the visitors on my website. I want to know that how would i know that there is a visitor on my website and when the visitor has left the website. I am developing this application in php with mysql database , i also using jquery and ajax.
Thank you
hung3r4moreAsked:
Who is Participating?
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.

mankowitzCommented:
It's fairly straightforward to know when a user gets to your web site. As soon as they authenticate (enter login or guest or whatever), you should update their status in your database.

Knowing when a user has left is a bit more tricky. From easiest to hardest:
1. When the user logs out
2. When the user closes the window or navigates away
3. When the user is inactive for a period of time

for 1, just update the DB the same way as when they arrived.
For 2, you have to hook into the onunload function
For 3, you have to set a timer, ideally on the server, and if there is no interaction for a fixed period of time, the user is set to absent.
0

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
COBOLdinosaurCommented:
When the user enters the site you set a session and set a flag on either a file or your database.  Use a CRON job to test for whether there is an active file or DB entry.

There is no way to determine that someon has left, but you can set the CRON job to delete entries that are more than 5 minutes old because the user is no longer active.  You could also use AJAX to refresh the entry, but all that indicates is that the browser still has the page displayed.

Cd&
0
Ray PaseurCommented:
Knowing when someone has arrived is easy.  They present an HTTP request.  A web page gets loaded, things like that.

Knowing when someone has "left" is essentially impossible, because of the nature of the client/sever protocol, explained here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html

You can use a jQuery timer to make AJAX requests to your server, and you can update the session as long as the requests keep coming in.  But you don't really have a way to know whether the client is still at the computer or has gone to lunch.  You might be able to have some kind of a window.close() action that would trigger a separate AJAX request.  And, of course, if the next client request is to another page of your web site, it's reasonable to think that the client is not on the other page anymore.
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

hung3r4moreAuthor Commented:
As above comments if there is no way to find out that whether the visitor has left the website or not , then how the applications like zopim or other real time chat applications which provide real time assistance to the visitors when they visit the website know if the visitor is still present on the website or has left or is idle.
Please help me with this problem , it would be very helpful. Thank you.
0
COBOLdinosaurCommented:
They don't know.  They can do the kinds of things we have already outlined and depending on how frequently they check, or the duration of the life of the session, they can keep track withing an acceptable margin of error.  

Whether or not someone is still on the site is irrelevant.  If they send a request, then they are there;  if they do not send a request why should it matter if they are still there.  if something is being pushed to them; then it will be apparent that they are not there when there is no response.

Cd&
0
COBOLdinosaurCommented:
Why do you think is is necessary to know if they are there?  

Cd&
0
Ray PaseurCommented:
I'm with Cd& on this one - please describe the application from a "50,000 foot view" in non-technical terms.  We may recognize a design pattern that can be helpful to you.

Thanks, ~Ray
0
hung3r4moreAuthor Commented:
I am agree with the Cd& that
Whether or not someone is still on the site is irrelevant when that person is not interacting with the website.
Other applications like zopim are able to tell exactly when a user is idle(means not interacting with the website), or left the website, i wonder how they are able to tell the difference.
It is necessary for me because i am creating a similar application as zopim or other applications that provide real time chat assistance to the visitors on the website. As an admin i will be sending the chat requests to the visitors. So when the visitors leave the website i should know it by that,
* i wont be sending the chat requests to nobody or
* i wont be sending the requests to the visitors who already have left but only showing in my database list.
As i have noticed that the other application tells us exactly when a user is idle or actually left the website. When user is not sending any request it either means that they are on the website but idle or they actually left the website. Applications like zopim are able to tell the admin both of these situations.
0
Ray PaseurCommented:
It looks like a JavaScript widget.
https://www.zopim.com/

The other "chat" apps I have seen use JavaScript and timers.  When there is a gap of more than a number of seconds, they send a question like "are you still there?"  No answer means "nope."
0
COBOLdinosaurCommented:
IMO a waste of resources.  What difference does it make if they are not there anymore? What is the business case for knowing in seconds that they are not there as opposed to minutes.

Cd&
0
hung3r4moreAuthor Commented:
thanks for helping out..
0
Ray PaseurCommented:
I think you owe us an explanation of the bad grade.  Please read the grading guidelines, then post back here, thanks, ~Ray
http://support.experts-exchange.com/customer/portal/articles/481419
0
hung3r4moreAuthor Commented:
Hi,
Here is my problem.
I have a db table that has following columns:

ip_from | ip_to | country

and the values present in these columns are:
1.72.0.0 | 1.79.255.255 | japan

now i have some value in a variable say $ip_to_look that contains some value of ip address, now how will i check that the ip in variable $ip_to_look is present between the range of values present in the table in columns ip_from and ip_to.
To do this what would be my database structure.

Please help me to solve this problem, i will be very thankful.
Thank you.
0
Ray PaseurCommented:
This question has already been closed with a bad grade.  I recommend you do two things.

(1) Learn the EE grading guidelines.
http://support.experts-exchange.com/customer/portal/articles/481419

(2) Post a new question in the PHP Zone to ask about the IP-to-Country concepts.
0
hung3r4moreAuthor Commented:
@Ray
i just read the EE grading guidelines. I am really sorry for the bad grades in previous question. I am new to this website.

I have posted this question , you can check it out here :
http://www.experts-exchange.com/Database/Miscellaneous/Q_28273554.html
0
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
Chat / IM

From novice to tech pro — start learning today.