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']
Related
so first of all i checked and researched this question in stackoverflow and google but there no similar question.
i checked Count number of consecutive visits and SQL Query: Visited the site each day for 30 consecutive days but it doesn't help.
so my question is where to implement this action of testing if a user has visited the site for a consecutive x time, in the links that i put there they check in server side, but i think it's not very optimised because each time the user visits the website a query is triggered to check if todays date is equal or higher than LastVisit date.
in my application i already do query every time the user loads a page to retrieve his information but if i add an
update userInfo
set DaysConsecutivelyVisited=DaysConsecutivelyVisited+1
when DATEDIFF(CURDATE(),LastVisit)=1
then i'll have two queries every time the user loads the page, so i tought maybe it's better if i do this in client side rather than in server side. I'll put an attribute or hidden span in the document
<body data-lastvisit="2012-03-25" >
and in the javascript i'll do the analysis and if the $("data-lastvisit") is equal to yesterday i'll send an ajax reqeuest to update the DaysConsecutivelyVisited
what do you think, is it a good idea ?
You'll need a parameter to compare to.. which is - unfortunately - always the data inside your database table. The only thing you can do is to retrieve the userinfo on pageload, which most likely includes a field last_visited. If the date is not equal to todays date, registered it.
Furthermore, I'd say create a field consecutive_visits and reset to 0 in case the date's difference is larger than a day and increment it in case there's a day or less difference. This way you'd always have the amount of consecutive visits in the userobject after fetching it on pageload.
I absolutely do not see any need for an ajax request in this case. It's just a compare of the current user data with a Date() instance or timestamp. All calculating can be done serverside and there's only need for an extra MySQL query in case the values are different.
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.
I am using php and mysql and have no java knowledge. After searching I have found many countdown timers for javascript but they are client-side. I am looking for something that is server-side based.
I have a table in mysql with a column called TimeRemaining. That column stores future time records. I need to load the record with the least time remaining so that when it hits zero that record is removed from the table and a new countdown timer begins with the next record with the least amount of time remaining. Also, if a new record is inserted with less time remaining than the previous record with the least time remaining, the timer needs to be updated with the new record.
Is this possible and/or recommended through using triggers to update an event in mysql or can I create a countdown timer in php.
Thank you all for any help.
Edit:
I mentioned Java/Javasript because I would like to avoid it as I have no knowledge of it and through research I have seen most posts using java.
For a better understanding of what I want to do, multiple users are banned from creating posts in a forum for 7 days. I have a table in mysql that stores that ban and the time it expires. I need to automatically remove that record from the table when the 7 days expires. The user should have no control over this timer. I do not need to display the time remaining on a webpage. I need this to happen in the background with no user intervention. Is that clearer ?
You want to do this client-side. On pageload, you can pull the countdown time from the SQL table and client-side countdown THAT time. Once the time is complete, you can do a call (synchronous or AJAX) to the server (PHP) again to remove that record.
Considering you said you have little knowledge of javascript I would make a little iframe in your page that refresh automatically and show the last time checked accordingly.
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.
What is the best way to calculate the total time spent by a registered user on the site? ...under these conditions
1) User logs out normally.
2) User can simply close browser.
3) User can auto-login next time he comes back.
I think the best way to do this would be to find the time spent by the user on each page and keep adding them to his total time instead of checking for the whole site. But I don't know how to implement that....please help
You can't find the exact time he leaves the system, unless he logs out. Even then, he might be browsing the site while logged out.
The approximate way to do this would be to set the start time in the session and keep incrementing the time everytime he visits a page.
So the first time the user comes to your site at time T, you will
Create a session and put the start time there
Add the total time as 0
For all subsequent requests you would
Check the start time and compare that with the time now and get the difference
Add that time to the total time
This method will not give you the time the user spent on the last page. But it will give you something to work with.
You can do this with JavaScript and a separate PHP script.
The javascript code reacts to events that mean that an user is active (such as mouse/keyboard/resize events) and invokes the php script.
The php script compares the time when it last received a request to the current time and checks if the difference is over a certain threshold (i suggest something like 10-30 minutes to prevent single-click sessions from adding up) nothing happens.
If the threshold is not reached then the difference between the two timestamps is added to the total sum in the database.
Afterwards (in both cases) the last request time is set to the current time and the script ends.
If you also want to know when the user closes your website pages you can subscribe to unload events and/or implement an heartbeat script that calls a PHP script every X seconds.
you have 2 approaches, either to create a log table in your DB to track each user (by ID) logins and logouts and then calculate the time difference between the two in each record for the specific user and then sum all of that. OR you go more complex and make 4 columns in your DB->usertable (logintime 'timestamp' - logouttime 'timestamp' - lastactive 'timestamp' - onlinetime 'int') and update each column as their names say by code according to user activities. then alter the Session.php script in the System/libraries directory at line 105 exactly after if ( ! $this->sess_read()) before the system creates a new session and write a code to check if the 'logouttime' is not the same as 'lastactive' time (to avoid session timeout expiry misunderstood in the next code) if both fields not the same, update your DB to make 'logouttime' equals 'lastactive' then at line 107 exactly after: $this->sess_update(); write a code to check if the 'logouttime' equals the 'lastactive' (and you will make that happen earlier in your logout.php script) write a code to calculate the online time by the difference between the 'logintime' and the now time 'time()' and add the result to the 'onlinetime' field. but if the 'logoutime' is not the same as 'lastactive' (that meanse the user is online and making activities in your site because you are tracking him and updating the 'lastactive' field frequently) then write a code to calculate the online time by the difference between the 'lastactive' and now time 'time()' and add the result to the 'onlinetime' field. that way you have the exactly online time logged forever in the 'onlinetime' field! I hope you got me right because the examples will be a lot long of scripts (although I don't mind to share upon request). good luck.
Use the Session ID to keep track of individual sessions.