Button with a limit of one click per hour - php

i have a question With This simple system, i can make a button with a limit of one click per hour in php / sql or other? no limit per ip. but for all
It would be like a reservation system in real time, anyone can help me?
But only the script of the button with 1 click limit per hour for all.

You can store the number of clicks in a mySQL column and increment it every time a user clicks the button and then check if the click falls in the past 1 hour interval and if so, tell them they have to wait.
Something like this:
select count(*) as clicks_in_the_past_hour
from table
where click_time >= now() - interval 1 hour

store click time to database then each click check click time with stored time if less than 1 hour then return false else return true and save then new time to database and so on.

Yes In java script you can do it. execute the following code.This code is for one minute,if you want time change ,change your require time in javascript code
<script>
function doSomething() {
document.getElementById("btn").disabled=true;
setTimeout('document.getElementById("btn").disabled=false;',60000);//enter your require time in place of 60000
}
</script>
<label for="btn">This button will activate in one hour</label>

Related

PHP/MySQL update mysql field after 10 minutes has elapsed.

As the title reads, I am looking for a way to update a mysql field after 10 minutes has elapsed of a query being run.
Something like below is an example without the time restraint:
mysql_query("UPDATE `players` SET `playcoins`=TRUNCATE(ROUND((`playcoins`+$amount),9),8) WHERE `id`=$player[id] LIMIT 1");
Any ideas?
MySQL databases have a class of object called an EVENT. It's basically a hunk of SQL code that runs at particular time, or on a particular interval.
You could use code like this to create an event to do what you require at the right time in history. This particular code will create an event that runs just once, ten minutes in the future.
DELIMITER $$
DROP EVENT IF EXISTS coins_user12345$$
CREATE EVENT coins_user12345
ON SCHEDULE
AT NOW() + INTERVAL 10 MINUTE
ON COMPLETION NOT PRESERVE
ENABLE
DO BEGIN
UPDATE players
SET playcoins=TRUNCATE(ROUND((playcoins+123),9),8)
WHERE id=12345
LIMIT 1;
END$$
DELIMITER ;
To use these EVENT objects, you have to configure the event scheduler correctly. Read this. http://dev.mysql.com/doc/refman/5.6/en/events-configuration.html Some cheap shared hosting providers don't allow the use of events, so this is not guaranteed to work.
You go it the wrong way. Sure you can do it. And you can do it with PHP. But you shouldn't. PHP is not the right language to do such a task. Before I starting talk about shell_execute and sleep, which would be the core elements, you need to do this, I offer you another solution.
If I see right, you want to give a player every 10 minutes, some coins.
The right approach would´basicly be:
Save the last time the player has get coins in the database. If you get the player coins, you first want to check, the last time you give the player coins. Now calculate, how much he has earned in this time difference. Finaly add this to his balance and update the field, where you save the last time, the player has earned coins.
An alternative would be a Cronjob/Scheduled Task to a PHP file, which is called every 10 minutes, to give each player the coins, he should get.

PHP Script to Add One Every Hour

I'm new to PHP and I'm hoping to make a script where a user will get a "coin" every hour that they go on the page. For instance, if a user logs in twice during the same hour, they will only get one coin. But if they refresh the page during the next hour, they get another coin. However, they do not get coins when they do not refresh the page, even if many hours go by.
How would I even start going about doing this? Any help would be extremely appreciated.
Easy... :) If you are using MySQL or something to store the coins, get the time too, when the coin was credited. And each time the page is called, check the time. A pseudo code would be like this:
load(coins);
timeDiff = timeNow - timeLastCredited;
if (timeDiff > 1 hour)
coins++;
save(coins);
In case of PHP, I guess you may do like this:
$coins = getCoins(); // Assuming this function will load the current coins count from DB.
$lastCredit = getLastCoinCreditedTime(); // Should return a DateTime integer.
$timeDiff = microtime() - strtotime($lastCredit);
if ($timeDiff > 60*60*60*1000)
saveCoins($coins+1); // Assuming this function saves the new number of coins.
I'd do it this way:
On each page load (refresh, login, whatever), check to see if the user has already received a coin for the current hour. To do this, you need to know what the current hour is:
$hour = (new DateTime())->format("Y-m-d-h");
This will give a value like "2013-03-25-11" during the 11:00 hour. I'm including the date, since we don't to want skip giving a coin in, say, the 11 o'clock hour just because they were online yesterday at 11:05.
Then you can either:
Add one to their coin total whenever you have a new $hour value (ie. it's not recorded in your database) and save the $hour value, or
Save the $hour value in the database and count the total number of coins earned with a query like SELECT COUNT(DISTINCT hour) FROM table;
The first approach is useful if they're spending the coins on something (ie. you can add/subtract from their "coin account"); the second is useful if you just want a grand total of the number of coins (ie. the total number earned, ever).
I'm going to assume that you have a users table in a database.
If that is the case, you can use a column in the database to update the latest time that a user has gone on the page with php and a query on that page. Then you can set up a cron job that runs every hour that will query the users table and see if the user has viewed the page within the last hour. If the user has viewed the page within the last hour, increment the amount of coins associated with that user.

Countdown until mysql query is executed

I'm redeveloping my online text based MMO, And I'm building a "Travel" Script Where a user clicks where they want to go and it takes X minutes to get there.
The only problem is - Due to limitations of my host, I cannot run 1 cronjob every minute to take away time spent travelling.
Can someone tell me how i could achieve this without using crons? Delaying a query from being run for multiples of 1 minute or so?
Once they set off, A countdown from that moment of X minutes begins, And once their travel time reaches 0 their "location" is set to "destination"
Thanks!
Use PHP sleep() method.
Like sleep(60); will result in the script to wait execution for 1 minute.
Do NOT set a script timeout as it wont make any sense.
I suggest you add a destination time column, set it to when the animals should arrive, and then in the client display a countdown until that time. You could use a query such as
select * from arrivals where arrival_time < now();
To get a list of animals that have not yet arrived, and of course the arrival_time is when they will arrive.

Countdown Timer interact with php mysql

I want to construct a countdown timer so the customer gets discount on price if he buys the product before countdown ends.
I've search internet and found a lot about countdown timer using Jquery, Javascript...
But how can i say in PHP that if the countdown is running then the product has 50% discount
Else if countdown ends(00:00:00) delete discount, return product's price to normal.
The price is stored in the database.
No need for timer. Jut store discount date time in database with product.
and if purchase time is within that time apply 50% discount else no discount. You can do this by any means either apply if/ else condition using PHP or handle if/else and datetime comperisions in mysql query.
You just need to store in database the end time of countdown with something like this
INSERT INTO user_offers SET countdown_end = DATE_ADD(NOW(),INTERVAL 5 MINUTE), user = ...
This will insert row with current time + 5 minutes. Assuming that you have logged in user, you will insert item id and user id, otherwise you need to develop your own way to recognize user (cookie for example), so that refreshing page would not refresh countdown.
Then you will need some jQuery (I like jQuery, but you can use whatever you want) magic for countdown, like this plugin http://keith-wood.name/countdown.html
When user clicks on ORDER NOW button (or smthing like that) you just check user_offers table like this
SELECT countdown_end FROM user_offers WHERE countdown_end > NOW() AND user = ...
If it returns something than you know, that he clicked in limit :)

Online or offline function PHP

I'm working on a "community". And of course I would like to be able to tell if a user is online or offline.
I've created so that when you log in a row in my table UPDATE's to 1 (default is 0) and then they're online. And when they log out they're offline. But if they don't press the Log out button, they will be online until they press that button.
So what I would like to create is:
After 5 minutes of inactivity the row in my database should UPDATE to 0.
What I'm looking for is how to do this the easiest way. Should I make an mysql_query which UPDATE's the row to 1 every time a page is loaded. Or is there another way to do it?
Instead of using a boolean "Online" field, use a DateTime. When a user makes a request to the page, update the DateTime to NOW(). When you are gathering your list of current users online, your WHERE clause would be something like WHERE lastSeen > DATE_SUB(NOW(), INTERVAL 5 Minutes)
Update: To retrieve individual online status.
select if(lastSeen > date_sub(now(), interval 15 minutes), 1, 0) as status from table where userid=$userid
This tutorial is quite handy: Who Is Online Widget With PHP, MySQL & jQuery
Well, if you don't want to set up a cron job, that would execute some code every 5 minutes, you have no options. But, actually, I think the following approach would be much more efficient:
Change your 1/0 column to timestamp
On each user request update that timestamp to current DateTime.
When checking for active users, check if that timestamp is less than 5 minutes from now
This way you'll be having actual data on users and no recurring queries - just one additional update per request
If you will update the row only on page load, then some of information would be incorrect.
Let's assume that user have opened page and is writing really long text or something. He is doing it for half an hour now. And your database ny now is already updated and he is counted as offline user.
I would write javascript that pings you back each 5 minutes, if opened tab is active.
This ping updates database field 'last_activity' to NOW(). And to count online users, or check if user is online you'll need to compare 'last_activity' to NOW() minus five minutes.
Simpliest ways (IMHO):
You can count sessions in session_save_path() dir.
you can store last visit timestamp in DB, and count rows with (timestamp > current_timestamp - somedelay).

Categories