I have a game site, in which i need to display timer until the match time. Once the timer reaches the limit, i need to load the games.
For this, i have used the jquery countdown. Its working fine in general.
My problem is, while the timer running if the user changes their system time(local time), the remaining time limit changed and game started.
How to prevent the Countdown Timer from local time? Please help me to fix the problem.
My code,
$('#gamewindow').countdown({
until : 100,
layout:'{mn} : {s<}{snn} {s>}',
onExpiry: function () {
$('#gamewindow').load('game.php');
},
});
});
<div id="gamewindow"></div>
Instead of initializing timer via plain Javascript (which will return the client-time, i.e., the time on your client's computer), you should instead rely on server-time, i.e., the time on your server. This will standardize the timing for all your players.
So, upon visiting the countdown page, your Javascript counter will be initialized with the server's time. You could use AJAX or some hack-y PHP to do this. For this task, start-up is the only time JS needs something from the server; afterwards, the "countdown" effect will be managed by JS. Of course, this will not be uber-synced but that error is pretty forgivable. Even if the user refreshes with a different local timezone setting, the JS will just re-initialize from server time--no problems!
Since you can't prevent the user from changing their system time, the solution must be serverside.
In game.php, check if the match_start_time has been reached, if not, redirect to the waiting page (which should display the countdown timer again).
Related
Hi I need help regarding the following issue:
My website has a timer countdown running. Actually in my admin panel I set a date and time for a process to begin and when i hit on save the timer countdown starts in my website.
Example : Say now my current time is 1pm. Now for example in my admin panel I am creating an event now. The start of the event is tomorrow 1pm. So now in my admin panel I select the start time of event to tomorrow 1 pm. Now the time remaining for the event is 24 hours. So now in my website the countdown timer starts for the event like :Time left for the event : 24h:0m:0s and this timer goes in decreasing order. Now the issue is that the timer is taking the time from my computer.
If I change the time on my computer, the count down timer on my website also changes.
Please help me out regarding this on how to use the server time for my website's timer so the time is same for all the users irrespective of their computer's date and time.
I also heard a concept of stop watch?
Please help me out
If your code is on server the only reason for showing local time is you are using Javascript for time because Javascript fetch time from local system.
just set these at top of your php code and calculate or do whatever you want with it afterwards
date_default_timezone_set('Europe/London'); //or Asia/India or any other
$login_date=date("Y-m-d H:i:s");
JavaScript traditionally runs client-side, meaning in the user's browser whereas PHP runs server side, ie. it will fetch time from your server. In your case, you need the time to be same for all users of your website which means you need a server side code
In short:
JavaScript Timer
Runs client side
You are using this
Example code
PHP Timer
Runs server side
You need this
Example code
I've recently finished my application and I've got a huge problem. I need to allow only 1 user to access it at a time. There is an index page accessible for every user everytime and "start" button. When user clicks start, the application locks and other ppl need to wait until the user finishes. When the user closes tab/browser, the application has to unlock automatically. Each user has 5 minutes to use my app.
I partially solved my problem, but it still doesn't work properly - on every site I set the jquery script that every 5 seconds triggers "extend.php" file on the server ($.get() function). The php file modifies time.txt file (it changs it to time()+5) and the script on the intex site checks whether (time()>time.txt content). So that when the uses closes tab/browser, the app is accessible. Obviously my app is also based on sessions (when the user closes browser, he loses access).
On some computers it simply doesn't work (it seems jquery doesnt trigger extend.php file and it makes my app accessible all the time).
So my question is: do you see any other ways to solve my problem?
The descr might be messy but I wanted to describe everything strightforward ;)
Regards.
Try using an a jQuery unload function so that when they click the close button your web browser executes one last line of script before the user exits. Example:
$(window).unload(function(){
"your php function to unlock the app here"
});
Hope this helps.
Your method is OK, it should work. Yes, node.js, or any other server side javascript can be used to do the same, but having a script triggered is by far the easiest solution. You really should focus your time to investigate further on what machines it is not working.
If it is restrained to 5 minutes, then set it to expire in 5 minutes. You can use a counter in jquery to show how much time is available. When it hits the expiration then notify the user time is up. Once time has expired or the user is finished with the app update the time.txt to time() or however you normally handle it when the app is accessible. No polling and 1 update.
You can put a LOCK on a mysql table when a user is online and unlock it when they are offline.
The only issue is if your code forgets to unlock.
im making some statistic codes for my website (im a php developper). I want to calculate how many seconds/minutes the web user stay on any page (like google analytics do) but i have no idea of how to make this. Thanks for any help or scripts!
How are you gathering the data? The common options would be instrumenting the page using javascript, looking at webserver log files, in the server-side request handler or sniffing the TCP/IP traffic.
Doing it "like Google Analytics" implies the former. In which case the way to do it would be to grab a timestamp as soon as possible when the page loads (rather than waiting for page ready / onload event) and compare that value with the previous tiestamp (so you'd probably store that in a cookie). Then you need some way to send this back serverside, and a way of recording and reporting on the data.
Note that trying to fire an ajax call as the user leaves the page, e.g. via onunload, will not work reliably (the page launching the request is at the end of its lifecycle). The important thing here is the ASYNCHRONOUS part. And making a synchronous call will just have the effect of slowing down the website.
You might want to have a look at Yahoo Boomerang - although it doesn't support dwell time measurements out of the box, it's easy to extend. For a backend, you could do a lot worse than Graphite
You can fire an unload event in javascript when the user leaves the page, which sends an Ajax request to your server. Since this may not work in all browsers, especially if the network latency is high, also have a ping script (also with Ajax) which calls your statistics system once in a while as long as the user stays on the page (for example, every 10-60 seconds depending on the resolution you want).
If you want to do it in serverside i.e in php then probably you would need a table allocated for this. say "analytic"
First you need to add this script in every pages. that inserts these data into the table analytic which is $_SERVER['http_referer'] , current timestamp, remote address and current page URL.
Now the calculation part.
basically when a user first lands in your page $_SERVER['http_referer'] wouldnt be from your domain. Then keep the timestamp as the start time.
Now check the next time stamp. If the http_referer is same as previous records page URL then find the difference in the time stamp to know how much the user has stayed in a page.
More or less what am trying to say is find the time between each request from the user.
Disadvantage of this method: When user lands in a page closes it. its impossible to find the time on your site.
A quick and easy method I came up with is pretty useful.
On every page of a site where I want to track time on page, I include a tracker script.
I grab as much info as I can, and make a database entry, including the referrer, the requested/loaded page, user-agent, ip, timestamp, etc.
These timestamps, in conjunction with the user's ip, can be used to determine the time the user was on the previous page (including load time of current page).
The only drawback is that I can't determine time on the last page they visit (which isn't always a bad thing, I can reduce tracking idle time).
Bounces are identified by single entries by a given ip within a specified time period (an hour would probably be sufficient).
At page load create a date object, then when the page unloads create another and substract them. After that you can do an AJAX request to your tracking server, sending the elapsed time.
var startTime = new Date();
var endTime;
window.onunload = function()
{
endTime = new Date();
var elapsedSeconds = endTime.getTime() - startTime.getTime();
//Do the ajax request, sending elapsedSeconds
}
Status as in online/offline..
When a user enters onto a page, it records the current time and puts it in the database.. if 5 minutes passes since their last action, it shows them as offline?
You can use JavaScript to time the five minutes, then use AJAX to call a PHP script that updates the database.
window.onLoad = function(){
setTimeout(UpdateDB,1000*60*5);
}
function UpdateDB(){
// AJAX call...
}
This would be a good way to start. You can easily check the period of inactivity.
It would be relatively simple for you to set up an AJAX ping to your server on a long interval~ so that as long as the user's browser has your site loaded, it's still pinging it's status as online.
I'm writing a web application that autorefreshes data with an AJAX call at set intervals.
Because it's doing that, server side user sessions never time out, since the last activity is refreshed with every ajax call.
Are there good client side rules I could implement to time out the user? I.e. should I track mouse movements in the browser, etc., or should I point the AJAX calls to URLs that don't refresh the session?
I like that my AJAX calls hit a session-enabled URL, because I can also validate that the user is logged in, etc.
Any thoughts in terms of whether I should even bother timing out the users?
One technique I've used: increase the interval between AJAX calls every time a call is made. So you make your first AJAX call after 10 seconds, then you wait 11 seconds, then 13, then 16, 20, 25, etc... (or some similar pattern). Every time there's page activity (found by registering some JS event), you reset the interval back to your starting value (e.g. 10 seconds).
This technique will cause users who don't touch the browser for a while to time out eventually, when the AJAX interval becomes longer than the timeout period. As an added bonus, you'll r educe your server loads -- if a user leaves the browser window open for a long time, they'll make fewer and fewer requests before timing out.
I prompt the user to verify they're still active via JavaScript after a period of inactivity. Inactivity is defined as "no mouse or key messages sent to the window". If they fail to respond to the prompt after a certain amount of time, I redirect to a sign-out page.
My jQuery UI-based implementation can be found here.
I've done this by maintaining a "last action" timestamp in the browser and sending this back to the server with the heartbeat. On the server I then check for a timeout based on the difference between this value and the current time, calling the logout routines if the user has been idle too long. If the session is timed out then heartbeat result will trigger the browser to reload the page which, as the session is now logged out on the server, will clear any user specific information.
The two main problems I had to solve with this approach were differing interpretations of timezones in the server and client date function implementations and keeping track of the most recent action if the user had several tabs open in the same browser sending different "last action" timestamps back to the server.