I'm trying to guess how much time a user expend on my website in php. To do that, i was thinking that for every new action on my web, i could get the difference between the $_SERVER['REQUEST_TIME'] and the current.
I would also be planning to get stats of my current hosting but i don't know if i can get these values via PHP request.
I would like to get your point of view about using this first and second method, but in advice I should say i'm not really happy with the first option, cause server could be collapsed if the amount of petitions are really high..., otherwise I don't know any other way so if you could help me, it would be wonderful.
On my website i'm using HTML5, CSS3, PHP, JavaScript, JQuery, AJAX and MySQL database.
It is supposed i'd like to store and count all the time users stay logged in my webpage.
Thanks in advice.
There are a few ways of doing this, but only one way to get fairly accurate readings.
PHP Time Difference
This method is what you were talking about with checking the time difference. You would mark the time the user visited the page. Then when the visitor navigates to another page, you subtract the two times and you have the duration the user was looking at the previous page.
To hopefully make myself a little more clear, below is a basic layout of the actions you need to perform to make the PHP time difference method work.
Log the current page URL and time(). Store this as a cookie / session / entry in the database.
When the user visits a page, check the HTTP_REFERER variable and if it matches the page they were previously on, you can calculate the time they were on the last page. The referral check is needed to make sure they are not just opening a new tab and visiting your site again. You will also want to add a time limit so if the value is over 30 minutes it doesn't mark them as visiting the page for over 30 minutes.
Keep repeating those actions and you will have a somewhat okay tracking method. This won't be that specific and in some cases not work if they don't send the referral header.
Javascript Tracking
You can send a "heatbeat" or a "ping" every few minutes to tell your server that the user is still on a specific page. You would want to send the URL the user is currently on as a parameter, don't rely on the HTTP Referral header to be sent. Using this method you can get very accurate readings. You could even track if the user is focused on the web browser window or not to check if the page is being read or just in the background.
Related
I need to track users across pages without using a cookie. I do not have access to the web server, so my solution has to run from some JS each page can load.
Currently I am using a horrible solution of IP + timestamp.
So his navigation might look like this:
13:21:11 /index.php
13:21:15 /shop.php
13:21:35 /blue-shirt-1.php
Currently to track his session I have some logic like unique IP + navigated around in the past 10 minutes.
This is fine if every user visits the site once, and only for 10 minutes, or visits very infrequently, also for 10 minutes. But I need to be able to understand these two scenarios:
If he sits at /blue-shirt-1.php for half an hour, and then clicks another link, that should be considered a part of the same session.
If he closes his browser or navigates to a different website, and then comes back 5 minutes later, that should be considered a different session.
Is there any way to do this?
Note I can't use cookies because of a GDPR compliance issue.
Any ideas greatly appreciated.
An alternative to cookie-based sessions could be an URL-based session.
You could generate your own sessionids and use them as parameters like index.php?id=XXX and if ther is no id given you could generate a new one.
You should also take a look at PHPs URL based session management: http://www.php.net/manual/en/session.configuration.php#ini.session.use-trans-sid
Note that in this case the user could fake a new sessionid or use the id of an other user. For this
you could track the ids with the users ip and hold them after leaving
one page for a moment (for example by using onbeforeunload) and if the
given sessionid does not exist you could generate a new one, too...
How would I approach this.
My idea is to limit posting with a timer. So for example,
if a user posts a question, the user will have to wait 'X' ammount of time before the user can submit another question.
I am using Meteor as my framework. I am not asking for anyone to write up the code FOR me.
I am studying by myself, and I couldn't find any tutorials/tips on what I approach I should take to accomplish this.
Any references, or pointers would be greatly appreciated!! Thank you!!!
(As of now, I am using a Meteor Method to postInsert into the mogoDB)
In the browser, when the user posts a question, you can disable the controls for posting another question and then use setTimeout(fn, x) to set a timer for x milliseconds from now at which time your fn function can then re-enable the controls to allow them to post again. Ideally, you'd probably also provide some advice on screen about how much remaining time before they can post again.
To prevent a simple browser refresh from re-enabling the controls, you could also store the time when the next post is allowed in a cookie and read that cookie whenever the page is loaded to decide if the posting controls should be enbled or not.
But, since client protections are not entirely secure (they can be bypassed, sometimes even by just opening another browser), you may also want to record the time of last post in the user session on the server whenever a user posts. If a new request to post arrives, you can check the time of the last post and decide if enough time has elapsed to allow the post or not. This assumes you have some sort of user-identifying information which you can use to know when this user last posted. If not, you would need to add such information.
I am building a WebApp (ERP) and I need to display the people currently logged in and active on the page. I managed to get something pretty accurate by listening on the mouse/keyboard events and periodically reporting to the DB.
I don't know how to mark people offline when they close the page. I tried using onbeforeunload, but it obviously fires when the user simply changes pages (click a link inside the ERP, that point to another page in the ERP).
I then tried to use WebSockets, but the problem is the same : everytime the page is realoded, the WebSockets connection is closed.
So I can think of two ways:
Use WebSockets indeed, and replace all links by a call to a javascript function that would somehow tell the server that the user is going to change page (so that the server doesn't mark it as offline). But that doesn't feel right, semantically speaking, links should be links, it simply points to another location.
Use either WebSockets or AJAX and never actually change page: links are replaces by a function that will call for the content, and display it on screen (updating the DOM with Javascript). But again, it doesn't feel right either, because semantically speaking the page would have no meaning and the URL would never change, so the user can't "copy paste" the link of the page to refer to it, right ?
So, is there a proper, clean way of doing this? Thanks for your help.
If each of your pages has a webSocket connection to your server, then on the server you can see when any given page is closed by seeing that the webSocket gets closed.
To avoid thinking that a user has left the site when they are just navigating from one page in your site to another, you simply need to add a delay server-side so that you only report that the user has left your site if there has been no webSocket connection from this user for some time period (probably at least a few seconds).
So, on your server when you detect that the last webSocket connection for this user has been closed, you set a timer for some number of seconds. If the user opens up another page on your site (either via navigation or just opens another page) before the timer goes off, you cancel the timer and count the user as still connected. If the timer goes off, then you now know that the user has been away from your site for whatever time period you picked (say 10 seconds) and to you, this will signify that they have left the site. You can control how long you want that time period to be before you decide that, yes they are gone.
All attempts at trying to "see" the user leaving your page IN the browser with Javascript are going to have holes in them because there are always ways for a web page to get closed without your client-side javascript having a chance to tell your server. The beauty of the webSocket solution is that the browser automatically and reliably tells your server when the page is now gone because it closes the webSocket and your server receives the notification that the socket has been closed.
As I understand you want to compute users active on website/pages.
Identify the user (99% unique id computed):
http://valve.github.io/blog/2013/07/14/anonymous-browser-fingerprinting/ you can use another library, there are few.
On each page send from time to time at page load meaning user is navigating or (60sec you can chose lower time frame meaning user is staing on the page) computed id (fingerprint js) to server (web-socket/ajax)
On server you need to have list of id's with expiration date (60s) increment when new user log's in (stored in database or session).
Retrieve on your website the count (60sec ajax/websocket) of id's having timestamp <= server time - let say 120sec.
Knowing if user is logged, and specify the page:
use an object to be sent at server {fingerprint: 123123124234, logged : true, page: home}
Clear your list if you are not storing in Database the users:
Separate thread (server only) access the object and destroy all nodes older then 10 min or whatever your page session is set.
js timer: http://www.w3schools.com/jsref/met_win_setinterval.asp
Let hope it's helpful, id did something similar using the timer at 5 min to sent to server if user is still on the page, or signal at page load.
Getting the cont of users in frame of 60 sec. And even the users with names present on page :)
Somebody already post this kind of question.
Hope this could help you .
Detect if user has closed ALL windows for a website?
I'm looking for an event that will return when close page whether the user is connected to my site.
onbeforeunload does not help me because it is also activated in efresh, submit, browse from page to page on the same site.
There is no client side event that triggers when a user leaves your site for another site specifically.
If you want to know if users are online you can make repeated ajax calls on a timer, and use a server side session that updates a timestamp of the last request. Using this you can query the timestamps for example, get sessions where the timestamp is in the last 2 minutes.
If polling is an option and you have a server-side backend, you could make a keep-alive script polling the server every x seconds and add it to all pages on your site. If you do not receive enough calls (i suggest at least 3 or so), consider it as a signal the user has logged out. Of course this won't provide an immediate feedback, but should be reliable enough.
I have an html5/javascript application in which multiple users can be viewing the same set of data of any given time. For the sake of a real world example, lets say its a calendar type page.
So user1 is looking has the browser open and looking at the calendar page and user2 is also on the calendar page. User2 makes a change to the calendar and i'd like (as quickly as possible) for those changes the be recognized and refreshed on user1's screen. What is the best way to do this?
I'm thinking about have a mysql table for active users that stores the page they are currently on and a timestamp for its last update, then use ajax calls to ping the server every few seconds and check for an updated timestamp, if its newer than what they have client side, the new data gets sent and the page "reloaded." I am putting reloaded in quotes because the actual browser window will not be refreshed, but a function will be called via javascript that will reload the page. Sort of the way stack overflow performs its update checks, but instead of telling the user the page has changed and providing a button for reload, it should happen automatically. If user1 is working away on the calendar, it seems it might be quite annoying for user2's screen to constantly be refreshing...
Is this a horrible idea? Is pinging the server with an ajax request every few seconds going to cause major slow downs? Is there a better way to do this? I would like the views on either users side to be real time because its important that user1 not be able to update an element on the calendar page that user2 has already changed.
Update: based on some web sockets research it doesnt seem like a proper solution. First its not compatible with older browsers and i support ie8+ and second i dont need real time updstes for all users on the site. The site is an account based applicatiin and an account can have multiple users. The data needs to sync between those users only. Any other recommendations would be great.
You need realtime app for this. You should have a look at socketio. Everytime a user log in, you make him listen for changes on the server. Then when something changed on the server, every users listening are notified.
you can find examples on the official website : http://socket.io/