php and cron job questions, can I change one variable in php file from another php file?
I have 3 questions that will greatly help me with my project that I am stuck on, after much narrowing down these are the resulted questions arised from solutions:
Can I use one php file to change a variable value in another php file, can these values be read also from one php file to another?
How can I use crob job to change variable values within my php code?
Lastly, can cron read variable values in my php files??? for Example, if stat开发者_Go百科ements that will decide what to trigger and how to trigger when cron time comes?
I am a little new at cron and going deeper into php and need all the exeprtise help. I cant use any CURL or frameworks.
Please prevent the hijacking of my topic, the data I want is simple change $variable=1 in filenameA.php to $variable=2 using filenameB.php
This is not a very good practice, but it's the simplest thing you can do:
You need three files: my_script.php
, my_cron_job.php
, and my_data.txt
.
In the script that control's $data
(this is called my_cron_job.php
):
<?php
$values = array(
"some_key" => "some_value",
"anything" => "you want"
);
file_put_contents("my_data.txt",serialize($values));
Running it will also create my_data.txt.
Then, in my_script.php
:
<?php
$data = unserialize(file_get_contents("my_data.txt"));
print_r($data); //if you want to look at what you've got.
I'm not sure what type of data you are exchanging between PHP files. I'm fairly new as well, but will see what the community thinks of my answer. (Criticism welcomed)
I would have my PHP files write my common data to a txt file. When the cron job executes the PHP files, the PHP files can access/write to the txt file with the common data.
You seem to be describing a configuration file of some type.
I would recommend either an XML file or a database table.
For an XML file you could have something like:
<settings>
<backup>
<active>1</active>
<frequency>daily</frequency>
<script_file>backup.php</script_file>
</backup>
<reporting>
<active>1</active>
<frequency>weekly</frequency>
<script_file>generate_report.php</script_file>
</reporting>
<time_chime>
<active>1</active>
<frequency>hourly</frequency>
<script_file>ring_bell.php</script_file>
</time_chime>
</settings>
then have some controller script that cron calls hourly that reads the XML file and calls the scripts accordingly. Your crontab would look like:
0 * * * * php /path/to/script/cron_controller.php
and cron_controller.php would contain something like:
$run_time = time();
$cron_config = simplexml_load_file($conf_file_location);
if($cron_config === false) die('failed to load config file');
foreach($cron_config as $cron) {
if($cron->active != 1) continue; //cron must be active
$run_script = false;
switch((string) $cron->frequency) {
case 'hourly':
$run_script = true;
break;
case 'daily':
if(date('H', $run_time) == '00') //is it midnight?
$run_script = true;
break;
case 'weekly':
if(date('w:H', $run_time) == '0:00') //is it sunday at midnight?
$run_script = true;
break;
}
if($run_script) {
$script_file = (string) $cron->script_file;
if(file_exists($script_file)) {
echo "running $script_file\n";
require($script_file);
}
else {
echo "could not find $script_file\n";
}
}
}
and if you need to edit your configuration with php scripts you can use SimpleXML to do it, then just save it back to the original location with $cron_config->saveXML($conf_file_location);
精彩评论