-6

I want to do on a script I`m working on that every month a part of the site resets.

Or a better example, Something like a Subscription, when you want to buy something and you need to renew it every month. How can I know its been a month?

Roy
  • 1
  • 4
  • 1
    @mstruebing I dont know if its even possible in PHP, i thought about doing something like saving the unix date and add to it 30 days in seconds and when the user login check if the date is bigger or equal than saved one. But i want it to be without the user login. – Roy Mar 18 '16 at 12:51
  • 1
    @Roy everything is possible – JimL Mar 18 '16 at 12:53
  • Possible duplicate of [How can I make my PHP script run at a certain time everyday?](http://stackoverflow.com/questions/4558601/how-can-i-make-my-php-script-run-at-a-certain-time-everyday) – JimL Mar 18 '16 at 12:55
  • If they aren't logging in you wont know they are the same user.. – chris85 Mar 18 '16 at 12:57
  • @chris85 Thats for my first example not the second one. for the second example I think il do what I wanted to do. – Roy Mar 18 '16 at 13:08

1 Answers1

1

In PHP you can't do regular cron jobs, and I will discourage you seriously from doing it with real cron jobs if you don't know what you're doing.

You can only register when you last executed that event and then check if it's been a month since then. This is a really simple sample cron:

<?php

$lastexecution = /*logic to know when you last executed.*/;

/* It's either a database or a file or something similar.
 * I usually use a database table that contains the records when I
 * last executed a cron
 */

if (time() > $lastexecution + (30 * 24 * 3600)) {
  /*CRON LOGIC*/
}

You also should look into flock() or some similar locking mechanism to prevent the cron being triggered by two different users simultaneously.

Note: In your case, with a subscription, you could add a expires field to your database that would contain the date and time when the user's subscription needs to be renewed. If that date is in the past, you tell them

César
  • 370
  • 1
  • 9
  • `and I will discourage you seriously from doing it with real cron jobs if you don't know what you're doing` Why? Setting up a cron job isn't especially difficult to get your head around – JimL Mar 18 '16 at 13:04
  • Yeah. Until you get to work at a place where people without experience were allowed to create cron jobs, the crontab has several dozen records and moving an application from one server to another is a pain. Cron jobs lie outside of your application, and therefore reduce it's portability greatly. – César Mar 18 '16 at 13:07
  • It's just my opinion, but the pseudo-crons like this one may not be as reliable, but they're inside the repo. They are carried along with the application when you transfer it, and they are user agnostic (meaning you won't have files owned by root anywhere nor cron in several different users). – César Mar 18 '16 at 13:08
  • 1
    The code you wrote will only work if a user enters the site right? How can I setup a cron job that will do this? – Roy Mar 18 '16 at 13:10
  • Well, we can't dismiss technical solutions on the basis that they can (and will) be misused. There are usually multiple factors outside of the repo (env variables, db credentials, etc) which has to be set up on each install. Adding a line or two using `crontab -u www-user -e` should be within scope of a normal application install. – JimL Mar 18 '16 at 13:12
  • @JimL That's the exact same reason why we all dismiss ```eval```. Not comparing it though. But a misplaced cron job can be extremely hard to debug and because there is a way to do it with cron jobs, doesn't mean it requires cron jobs and whenever possible I would discourage the use of cron, not because it's a bad tool but because abusing it is simple. – César Mar 18 '16 at 13:16
  • Reminding a user that he has to renew a subscription does usually only involve showing a message. That shouldn't require a cron. – César Mar 18 '16 at 13:19