Other PHP- using cronjobs

Hi, how can I use PHP to create cron jobs? I need to pass variables to a PHP script and execute the script at a specific point in time. I assume I need to use the exec() function. I would have to first type in the time and then the path to the PHP binary file and then the path to my script. My question is how can I pass variables to my PHP script and how can I delete the cron job? Or at least modify it via my PHP script.

I need to make a cronjob that is dynamic and specific to each user. If a user registers on my website today and makes payment today. It's November 2 2014. I want the person to pay me every month. So, I need to set the cronjob to execute my PHP file at December 2 2014. The PHP file will verify if the person made a payment and if not it will make changes in my database to set a boolean value to 0. If the person didn't make a payment I will send out an e-mail via my PHP script to give a reminder to make a payment. I will set another cronjob at the same time to delete the account. This will be set to one year from the date. So, in this case if the guy made a payment for only one month, then by next December of 2015, the person's account will be fully deleted. I want to set another cron job to run a PHP script just to e-mail the person 14 days from the expiration date as a reminder.

However, I need to either delete or modify the cronjob each month such that my PHP script will check if a payment was made. If this is true then I would need to modify the cronjob to set it for a month after.

For example:

November 2 2014, we as a customer paid for one month for our account that we created. A cronjob will set my special PHP file to execute on December 2 2014.

So, now December 2 2014 comes and runs my script. My script looks up in the database that I made payment for the second month. So, now my script needs to change the cronjob to execute the same script again next month. In this case it's January 2 2015.

Now, notice that these dates and times are specific to the user. I want to give the person an actual month of services. So, I cannot set the scripts to go off at either the first of the month or the last day of the month. I track and store payment information and date and time information when they made payment and when it's due.

So, I need to make something that is dynamic that will create cronjobs on the fly for each user. I do need to pass the user's name to the script so the script can make the changes to the right person's account.

I was told I need to use PHP's argv array which stores the file name and arguments passed to it in this array. I am not to use the GET or POST arrays.

I need resources to figure out how to do this.
 
Last edited by a moderator:
This looks like a bad design to begin with. You clearly need to have some sort of database with users and their account validity data. Have a script run once a day (or whatever), which would query it for accounts which need to be expired and to the work.
 
As mentioned, this really isn't the way to do what you want. Trying to dynamically create a separate cron job for each user, to run when their account expires is going to end in a horrible unmanageable mess. That's on top of the fact that there is no safe way of allowing a web application to create/delete the cron jobs in the first place.

When a user registers, create them an entry in an customers database with an account start date, and an expiry date one month in the future. Create one cron job manually, that runs an account management PHP script every morning. This script should be able to handle everything:
  1. Check for accounts that will expire in the next few days (maybe seven days to give people a decent chance to do something about it) and email to notify they need to renew.
  2. Check for any expired accounts and switch them off. If a user has renewed through your web application, the expiry date in the database should have been pushed forward by one month. You should be able to easy disable all expired accounts in one go with a simple SQL command:
    Code:
    update customers set active = 0 where expiry_date < CURDATE()
  3. Look for any accounts that have an expiry date older than (365-notify-period) days. Email them all with a notice that the account will be deleted after notify-period days.
  4. Look for accounts that are over 365 days and delete.
BTW: If you are using MySQL, you can set the register and expiry dates directly in the insert statement, without having to do all the date maths yourself. That's probably possible in other database systems as well although I'm not sure about the syntax for those.
Code:
insert into customers(field1,register_date,expiry_date) values('somevalue1',CURDATE(),DATE_ADD(CURDATE(), INTERVAL 1 MONTH));
 
Back
Top