Resent mysql values at certain time - php

I have a database with a table and some columns. One of these columns is flags (similar to SO) where users can flag comments. I would like to give each user 5 flags per day. So if a user uses 2 flags in a 24 hour period, the flags should reset to 5 at the end of the 24 hours. I really have no idea how to do this. Is there a special mysql function?
PHP:
$query=mysql_query("UPDATE users SET flags='5' WHERE userID='$user'");
how would i get this to repeat every 24 hours? (if this is the right solution)

The best way is probably to set up an automated task (possibly using cron) that runs a query to do this.

Make a script and add to cron job. It will automatically update all on specified time of day.
Cron is very simply a Linux module that allows you to run commands at predetermined times or intervals. In Windows, it’s called Scheduled Tasks. The name Cron is in fact derived from the same word from which we get the word chronology, which means order of time.
Using Cron, a developer can automate such tasks as mailing ezines that might be better sent during an off-hour, automatically updating stats, or the regeneration of static pages from dynamic sources. Systems administrators and Web hosts might want to generate quota reports on their clients, complete automatic credit card billing, or similar tasks. Cron has something for everyone!

You can use MySQL Event Scheduler and define an event to let it update every interval you want.

Related

Can php launch scripts without user interaction in order to interact with the database?

I've search on the web and apparently there is no way to launch a php script without user interaction.
Few advisors recommend me Cron but I am not sure this is the right way to go.
I am building a website where auctions are possible just like ebay. And after an amount of time the objects are not available anymore and the auction is considered as finished.
I would like to know a way to interact with the database automatically.
When do you need to know if an object is available? -> Only if someone asks.
And then you have the user interaction you are searching for.
It's something different if you want to, let's say, send an email to the winner of an auction. In this case you'd need some timer set to the ending time of the auction. The easiest way to do this would be a cron job...
There are several ways to do this. Cron is a valid one of them and the one I would recommend if its available.
Another is to check before handling each request related to an object whether it is still valid. If it is not, you can delete it from the database on-the-fly (or do whatever you need to) and display a different page.
Also you could store the time at which your time-based script was run last in the database and compare that time with the current time. If the delay is large enough, you can run your time based code. However, this is prone to race conditions if multiple users hit the page at the same time, so the script may run multiple times (maybe this can be avoided using locks or anything though).
To edit cronjobs from the shell: crontab -e
A job to run every 10 minutes: */10 * * * * curl "http://example.com/finished.php"
TheGeekStuff.com cron Examples
Use heartbeat/bot implement
ation
Cron job that runs pretty frequently or a program that starts on boot and runs continuously (maybe sleeping periodically) is the way to go. With a cron job you'll need to make sure that you don't have two running at any given time or write it such that it doesn't matter if you have more than one working at any given time. With "resident" program you'll need to figure out how to handle the case when it crashes unexpectedly.
I wouldn't rely on this mechanism to actually close the auction, though. That should be handled in your database/web site. That is, the auction has a close time and either the database constraints or your code makes it impossible to bid on a closed auction. Notifying the winner and seller, setting up the payment process, etc. are things your service/scheduled task could do.

daemon software to update mysql database in background

I'm writing a realtime wep application, something similar to auction site. The problem is that I need a daemon script, preferrably php, that runs in background and constantly launches queries to mysql db and basing on some of criterias (time and conditions from resultsets) updates other tables. Performance of the daemon is crucial. Sample use case: we have a deal that is going to expire in 2:37 minutes. Even if nobody is watching/bidding it we need to expire it exactly in 2:37 since the time it started.
Can anybody advise a programming technology/software that performs this kind of task the best?
Thanks in advance
UPDATED: need to perform a query when a deal expires, no matter if it has ever been accessed by a user or not.
Why do you need to fire queries at time intervals? Can't you just change how your frontend works?
For example, in the "Deals" page, just show only deals that haven't expired - simplified example:
SELECT * FROM Deal WHERE NOW() <= DateTimeToExpire
Accordingly for the "Orders" page, a deal can become a placed order only if time hasn't expired yet.
Does your daemon need to trigger actions instantaneously? If you need a table containing the expired state as a column you could just compute the expire value on the fly or define a view? You could then use a daemon/cron job querying the view every 10 minutes or so if you have to send out emails or do some cleanup work etc.

Making timers to run code on expired records

I am developing a web application (PHP/MySQL), in which I need to implement timers (record id expires at expiration_date), where expiration involves the record's state being updated and arbitrary code being executed as required.
I basically need a BPMN Timer event.
Options I have considered, none of which I am thrilled with:
Cronjob calling a function in the application that just queries for and updates expired records.
At a commonly called point in the code, call this function at a fixed interval.
How is this commonly done in PHP applications?
The following is an option which you have not considered:
You could use MySQL Event Scheduler to run a stored expiration routine within the database periodically.
Some good things about this approach:
Platform independent, works the same way on UNIX and Windows.
Easy to set-up. No need to explain to the user how to setup cron jobs. You just create the event together with your database schema.
Downsides:
Not enabled by default in MySQL. Need to put event_scheduler=on in my.cnf or otherwise enable it.
Requires MySQL 5.1 or newer if I remember correctly.
Example:
DROP EVENT IF EXISTS expire_event;
DELIMITER //
CREATE EVENT expire_event
ON SCHEDULE EVERY 1 MINUTE
DO BEGIN
DELETE FROM data WHERE time < UNIX_TIMESTAMP(NOW() - INTERVAL 1 HOUR);
END //
DELIMITER ;
The above will every minute delete any rows from data table which have a UNIX time stamp older than 1 hour.
I would also choose the cronjob option.
I would also, however, have logic in my app that knows how to deal (or ignore) expired records. Services like cron can break, so it's nice to NOT have to rely upon it.
Unfortunately, DBMSs don't give us quite this sort of flexibility, and you won't want to go off and do database maintenance in the event you come across one of these "expired" records.
You're best excluding them through queries, the use of views, or simple application logic.
I would choose the cronjob option.

PHP and mySQL Task Scheduler program and database theory?

I need to develop a website that runs jobs at certain times (every 15 minutes) I'll use a cron to run the webpage.
What is the best way to store the jobs information in? Some jobs will be daily, others every 8 hours etc. To complicate matters, I also need to take into consideration the timezone differences. This shouldn't be too difficult as PHP has many timezone functions, but how do I integrate it in the programming of the next job to run?
Another question, is how will the user enter the information for the jobs to run? One option, similar to http://www.webcron.org, is to ask the user to select how often the jobs must run, but how do I store that info in a database?
Any assistance will be greatly appreciated!
I would use the crontab syntax, store it in a DB and run a separate crontab invoking the execution script every minute.
There are GPL classes that do that for you already, like : http://www.phpclasses.org/package/4140-PHP-Database-driven-PHP-job-scheduler-like-cron.html
Create a MySQL Event, that will take care also the storing, recurrence, execution.

how to set cron through php script?

i have 10 users. and i want to set a cron for that 10 users. Now when a perticular user logged in he can set the corn time from his panel and cron will run at that time .Also when new user added a new cron will be set for that user with default time according to application.
how can i achieve this?
I agree with thomasmalt that it is not wise but what you should do instead is have it so your users can set a cron time in a 'users' database table (like mysql) with columns: 'interval (INT)' and 'last_update (DATETIME)'. So every user has their own time set. Then you decide what is the lowest increment you will allow (such as 1 minute) and only let your users set times higher than that.
Then you run a cron every 1 minute which checks the 'users' table. Find any users where interval is less than or equal to the time since 'last_update'. Then run your actions for those users. This effectively does the same thing as setting crons for every user.
This sounds generally like a very unwise thing to do, but if you know what you're doing and understand the security problems involved etc. I would suggest solving this using two scripts
one php part which lets the user view his crontab and manipulate time settings using an html form and storing the wanted changes in a known place on your system.
one shell script part running as a trusted user on your system picking up the changes at a given interval (once a minute maybe) and doing the actual updates to cron.
Adding stuff to cron is potentially a very dangerous thing and only very trusted users should be allowed to do so. Any web solution should be regarded as insecure and you should take extra care to make sure any input is properly audited.
As thomasmalt said this is probably not wise to do in a public system ... you don't want to let users mess with system crons.
I suggest you let the users specify a time of day in their controlpanel and have a cron script run every minute. In that script it's easy enough to fetch the users that match the current time and execute their script. It achieves the same thing, is a little more verbose and more to code but alot more secure.

Categories