Jump to content

Recommended Posts

Hi there,

 

I want to make script which will execute via crontab every Sunday at 0:01h and will stop at 23:59h. My goal is to put(show) some tekst on the site. The problem is how to put this tekst in the site? I mean I know how to configure Cron Jobs but how to include the tekst in index.php for example?

crontab will be.

1 0 * * 0     /usr/bin/php Start.php
59 23 * * 0   /usr/bin/php Stop.php
  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/292362-cronjob-and-insert-content-in-site/
Share on other sites

Your script is going to have to write to the index.php file.

Easy way is to make a template file with a searchable tag you can do a find and replace on like: {{replace_text}}

You open the file, search and replace the tags you want.

Will need to set the permissions on that file to make it writeable.

Then use functions like:

fopen()

preg_match() or str_replace()

fclose()

Your script is going to have to write to the index.php file.

Easy way is to make a template file with a searchable tag you can do a find and replace on like: {{replace_text}}

You open the file, search and replace the tags you want.

Will need to set the permissions on that file to make it writeable.

Then use functions like:

fopen()

preg_match() or str_replace()

fclose()

Why would you edit the index.php file? PHP is a programming language, you can use it to dynamically display data from different data sources. Having cron dynamically alter the PHP "script" is ridiculous.

 

Instead, have your cron job put the data somewhere PHP can easily access it (like a database) and then write some logic into your php script to retrieve this data and display it.

  • Like 1

 

I want to make script which will execute via crontab every Sunday at 0:01h and will stop at 23:59h

Then you don't have to use two cron jobs. In my example below  my regular user (jazzman) sets up a command to be run by cron every Sunday (day 7) at 00:01 AM.

1 0 * * 7 /usr/bin/php -f Start.php

The cron task will be finished at Sunday 23:59. Or you want to run the script every minute?

 

Anyways... you don't need to use cronjob here if I understand correctly your wishes. Just create your start.php file with the following data, then include it to the display php file. 

 

start.php

<?php

// set the default timezone to use.
date_default_timezone_set('America/Toronto');

$curr_d = date("l");

function displayMessage($day) {
 if($day != 'Sunday') {
   return null;
 }
 else {
   return "Today is Sunday";
   }
}

echo displayMessage($curr_d);

display.php

include('start.php');
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.