Countdown timer with random number generator - php

I am running a sweepstakes like thing and want my users to all be able to load the page and have the exact same countdown and number generator running. I tried this in AS3 but each user caches their own swf file and gets a different result from the random number generator, and the as3 countdown is a few seconds off from each user. How would I go about making a countdown that is the exact same for each user looking at it at the same time and then a random number generator where every user will see the same result it gives? Is it even possible?
Sorry I wasnt clear on this. I would like to have it where the viewers could see the number being generated when the timer runs out. Kind of like watching the lotto on TV. Again, not sure if this is possible.
I have looked around, I know as3, some php, and some javascript. I have given up on doing this in flash.

Assuming I understand correctly what you want, store a random value (RV) every time someone accesses the page associated with a UNIX timestamp in seconds. Make time column unique, then if another request is made the same second, the random number is going to be taken out of the database.

Store the timer result in a table, show users the stored random number. Then when needed simply create another random number, store again, show user. Repeat.

Every user that come to that page , first print the time from the server so every user that will come and if your server time is 12:00 am for example , you will show him 12:00 am.
And then with Ajax refresh this time every X seconds ajax will send a request to the server and the server will tell you the time to display.
Keep the random logic on your server and when your application logic will say to change to a different number the server will return a different number and your clients will get it on the next pull ( ajax ) .
I hope that answering your question.

I'm not too familiar with flash, but what I would do is make the countdown happen client side using javascript's getUTCSeconds(), getUTCMinutes(), getUTCHours() etc to figure out current time verses end time of the counter (end time in universal time code).
Then you could use php to generate the random numbers (and a corresponding remaining time associated with it? how often do you want these?) and store it in a location for later retrieval (database, or file or some such). You could use ajax to grab the random number at the specified times
for more about js date/time functions w3schools has a pretty good resource:
http://www.w3schools.com/jsref/jsref_obj_date.asp

This is possible.
All logic should be stored on the server side. Use Flash only to show results.
Countdown: create it with php, store on the server side in the storage (database, memory, files, whatever). All clients (written on Flash) request counter value and display the counter on the client side starting from the value taken from server.
Lotto results are also generated on the server and passed to the clients. On the first hand you may generate intermediate results on the server and read them one by one by the clients, but I'd generate all results at once and pass them to the client.
Intermediate results can then be synchronized with the counter.

Related

saving time when user finishes quiz

Im creating a quiz for a client and I need a push in the right direction.
When the user starts the quiz a timer(count up) stars counting in milliseconds, seconds, minutes.
When all questions are correct the result is stored in a table with the number of attempts and time.
How can I ensure that the user does not just go into the DOM tree and edit time manually? Or just do a simple
$('#foo').disabled = false;
$('#foo').val('0.0.1');
An idea that crossed my mined was to compare two different timestamps using php.
One when the quiz starts and when a user submits the results, is this a bad practice? Or is it another way around?
If you want to prevent the user from manipulating the result you'll want to do the calculation on the server side, in PHP.
For example store the start time in a session variable $_SESSION['quizStart'] = time() (so it can be accessed on different pages) and when the form is completed PHP can save the end time: $_SESSION['quizEnd'] = time().
The difference between these two variables is your duration: $quizDuration = $_SESSION['quizStart'] - $_SESSION['quizEnd']

Is there a standard way to randomly search my database items with a cron until complete, then reset in PHP?

I already have a sceen scraper built using PHP cURL, tied to a mySQL database. I have stored products that need to be updated weekly rather than what I have now (a form that I input the url/product and hit go).
My first thought would be to use standard cron every 30 minutes on a PHP file like so.
I would like to randomize two things, the delay on the PHP script actually accessing the source site (i.e. 0 - 20 minutes) so the process timing is random. Second, I want to access my target items/pages randomly, but be sure to get all of them weekly and/or consistently, before cycling through the list again.
The timer is fairly strait forward and needs no storage of data, but how should I keep track of my items/uri's in this fashion? I was thinking a second cron to clear data, while the first just increments. But still I have to set flags as to what was updated already and I am just not familiar enough for choice of where and how to store this data.
I am using mySQL, with HTML5 options and is on Codeigniter, so can also hold data in SQLite as options..along with cookies if that makes sense. I couple questions on this part, do I query my database (mySQL) for what I need every-time, or do I store on a jSON file once a week, and run that? This obviously depends and/or determines on where I flag what was already processed.
You have a list of items to scrape in your MySQL database. Ensure that there is field that holds the last time the item was scraped.
Set a cron job to run every minute with this workflow:
Ensure that the previous run of the script has completed (see step #4). If not, end.
Check last time you scraped any item.
Ensure enough time has passed (see step #9). If not, end.
Set a value somewhere to show that you are processing (so step #1 of subsequent runs is aware).
Select an item to scrape at random. (from those that haven't been scraped in n time.)
Delay random interval of seconds to ensure all requests aren't always on the minute.
Scrape it.
Update time last scraped for that item.
Set a random time to wait before next operation (so step #3 of subsequent runs is aware).
Set a value to show that you are not processing (so step #1 of subsequent runs is aware).
End.
Once all items have been scraped, you can set a variable to hold the time the batch was completed and use it for n in step #5.

Do you post old data back when doing a long poll?

I think I understand the concept of polling fairly well. You basically just request data from the server, but only once the data has changed, does the server return it. Straight forward stuff. My problem comes with this example.
Let's say I have auctions with data that changes constantly. Among this data are things like
Closing time of the auction
Number of current bidders on the auction
When I start the long poll, I basically have something like this:
while($counter < $MESSAGE_TIMEOUT_SECONDS) {
$newData = getNewData();
$hasDataChanged = hasDataChanged($newData, $oldData);
if ( $hasDataChanged ) {
return $newData;
}
usleep($MESSAGE_POLL_MICROSECONDS);
}
Where do I get the old data from? I mean, when doing the request, I can either post the current state as it was last given to me, or I can store the data in Session. Am I allowed to store stuff in session when doing a long poll, or should I do a POST from the Javascript with the current state of that page?
Also, how would I stop someone opening 50 pages from killing the database? I mean, getNewData() effectively goes to the database. With a polling interval of about half a second, this could mean 50 requests every half a second, which could mean 50 x 2 x 30 = 3000 requests to the database in 30 seconds by just one user, if he decided to open 50 tabs?
Any ideas?
I would cache all ajax response data in memory along with last date that each auction had any change so you don't have to compare old and new data but just datetime. Invalidate cache on some change (closed, new bid, etc.) for auction.
Then from client side send time from last known data (last ajax call or when user opened page) and compare dates to see if something changed, if it didn't just return status:nochange (now client side knows there is nothing to update) and if it did return all necessary data from cache and update users page.
This model should protect database from overloading.

Random word on webpage every 1/10th of a second

I'm developing a small web-based (javascript) 'application' for an art project. The thing is called 'Poetry Generator', and it's a script that generates random poems based on user input.
The script has to display a random word to the user every 1/10th of a second. The wordlist used, counts 109.582 words.
I've already tried different solutions:
put all the words in a text file, and get a random line of the textfile -> too slow (and the user has to download a 3MB text-file before being able to use the application)
put all the words in an array in the Javascript. -> javascript arrays apparently can't handle 109.585 items
pull the words from a database using jQuery's Ajax function with a Javascript interval function -> this solution worked perfectly when testing on my localhost, but once uploaded to a real web-environment, this method proved to be too slow. (And I could imagine that my hosting provider wouldn't be so happy if I executed 10 query's to their server every second.)
So.. Does anybody knows a different approach that I could use to show a random word on a webpage every 1/10th of a second? It doesn't necessarily has to use php or javascript, as long as it runs in a browses, I'm happy!
Thanks in advance
Teis
There's no reason you have to pull the entire dataset every tenth of a second. Pull a reasonable amount from a database every minute (which would be about 600 words), load it into a local javascript object, and iterate through it.
When either the array index becomes high enough or the timer hits one minute, poll for another set of 600.
When dealing with times as low as a tenth of a second, you don't want to have to invoke the server EVERY single time! You could even load the entire data set into memcached and poll for random words, thus skipping costly database calls, as the entire data set is loaded into memory.
You could try to load only a subset of your words into your JS array.
Maybe you could try to load only 1000 (random) words from your database and show them.
As long as you don't need to generate insanely long text, you cold divide the randomization into two steps:
First preselect some of the words server-side (let's say -- 5000?)
Then, client-side, use JS to pick some more at random, from the preselected words.
Pros: No additional requests necessary; JS should handle array that big

Few issues with creating a web based chat system

I've decided to create a web based chat system for the experience. I'm using a mixture of AJAX(jQuery), PHP, and JSON to transfer the data. Now that I've started thinking about certain things, I've come to a mind block.
Right now, I use javascript to post the last loaded message id to a php file that queries the data and echoes new posts in json and then displays those posts in order on the page. However, the dates don't reflect the current time for the user. Since I use php to get the current time, I have no idea how to display the correct time to the user which takes into account of their time zone. Second, how would I incorporate a who's online list with this method? I could create a separate table and update it when a user creates a session and delete their name when they end the session; but what if they don't close it properly? Should I just add their last sent message into the the table and if it's been about 5 minutes since their last message consider the user disconnected? Lastly, is the method I'm using to collect new posts efficient? This there a better way to go about this? I appreciate any input.
This seems related: Determine a User's Timezone
I'm going to make you go there for the code snip so you give proper credit with your upvotes.
I get the impression that Javascript is the best/easiest way to get that data.
What I would probably do is use GMT or some other fixed time zone for all your server stuff and then just adjust that with js once it hits the browser depending on their time zone. Either that or just collect it once at the start of the conversation and adjust your output accordingly. There might be advantages to either way.
Edit:
Oh yeah, about the "who's online" I think you're headed in the right direction. I might suggest 2 lists. "Who's active" and "Who was active recently"
That way you can put people inactive after 5 mins and consider them disconnected after 10 or something. I guess it's about the same but it seems more accurate to me somehow.
The other option would be to set an ajax request to automatically fire of a request every minute or so. When they stop then you know the user is gone.

Categories