Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    339
    Thanks
    63
    Thanked 11 Times in 11 Posts

    Scheduling a PHP Script

    Hi,

    I'm creating a CMS where I want a user to be able to define a date for a post they're making to be auto-archived (so essentially copied via PHP/SQL from one table to another... active to archive, and delete the original - or justadding an 'active' field Y/N).

    My first thought was just to have a PHP script at the top of the landing page which checks all of the 'expirey dates' in the table againsts todays date and then updates them accordingly. But then that obvioulsy will only work if the user opens their CMS and hence runs the script... which isn't very good if they don't open it that often.

    I then found Cron Jobs which seem the way to go. Does anyone have any experience with these, and is using a free service as opposed to writing the cron job from scratch myself necessarily a bad thing? I'd just need it to excecute my PHP once at midnight, every day.

    Any advice would be much appreciated - I think I could learn how to code a Cron Job but if there free services are OK then maybe that would be best for such a simple task?
    Last edited by paddyfields; 11-05-2013 at 01:11 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,339
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    A cron job is simply a script. Thats it.

    You write it to do what you want and then setup a cron job (a scheduled even - cron is just the linux equivalent of the windows scheduler). With that cron you supply the path to the script you want it to run.

    Inside the script as with all linux scripts, you need to supply the hashbang which is basically the path to the executable that will run the script - in this case php.

    You could just write a normal php script which is called via http using a remote cron service but they tend to limit you to the amount of calls per day before charging you.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    paddyfields (11-05-2013)

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    339
    Thanks
    63
    Thanked 11 Times in 11 Posts
    That's great, thank you.

    I've found a few services that will schedule my script for free as it's only once a day... but it turns out my hosting package with GoDaddy included scheduling, so once i've finised my site locally and then uploaded to the server I think it should be a case of just pointing to my .php - as setting the intervals I need it to run.

    It's such a great tool being able to schedule.. the possibilties!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •