简体   繁体   中英

How to detect the “length of visit” by IP Address?

I know it is possible to detect the length of visit of a website visitor using their IP Address. But how do I know how many hours or even minutes they are online on my site using PHP script?

i don't think you can gather accurate data, but the close call can be made like this.

a) determine when the user logs in with reference to IP address.

b) detect when a user leaves a website.

first of you can create a record of the ip addresses that is browsing your website by storing them in database or anything you prefer.

to determine who is browsing your website, you can try fetching the ip address of the user as soon as he/she enters the website and store it somewhere along with login time. for example you can store it in the database with the following records.

ipAddress |  LoginTime  |  LogoutTime

as soon as someone enters your website you store the ipAddress in your record with the login time. but before storing you need to make sure the duplicate doesn't exist. ie before storing check if that ipAddress already exist. if it exist then it means the user haven't closed your website yet.

Now you need to determine when the user leaves your website, you can do that through javascript by using window.onunload that should tell you when user leaves your website. you can make an ajax call to know when the user leaves the website.

$(window).bind('beforeunload', function() {
    // ajax call
    // write to db..
});

this way you could know when a user comes in and leave your website. i don't say that this is it. but this should help you understand build your own logic. hope this helps you.

No, it's not possible to detect the accurate “time stay in your site" because of you never know when the visitor close the last page.

But you can get a approximate time buy calculating the time between the first visit and the last visit in one day, or something else. You can save each visit's time and "from ip", then do some calculate.

Besides, the best solution maybe using some third-party analytic service, eg Google analytics . It's free and it's very powerful.

One easy way to do this is via Ajax.

Open or start a session for the user and have Ajax ping the server once a minute.

In your logs or db or a flat you can record the start time of that sessions for that ip and record the last ping time.

If you keep recording the last ping time then you should be close to within. Minute of the visit length for your users.

UPDATE:

You can do the same with a hidden iframe with a meta refresh every 60 seconds. The point of the answer was to suggest an alternative technique to help the poster by giving the example with AJAX. There are other ways that this can be accomplished of course.

A completely different way to do this is to access your session store.

If you can access the folder in which your sessions are stored, you can get the list of the most recently updated or oldest sessions. If something is inactive for a period of time, you'll be able to discern how long it's been inactive by how long the session file has been untouched.

It'll then be up to your application/website to determine how to keep the session file updated while the user's on the site. If you store their IP in their session file, you can read this out again from the file.

This will also depend on how often your server takes to reset session files, but if these things are in your control, this is another technique worth considering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM