My Ini Design: Introduction: Difference between revisions

From The Uniform Server Wiki
Jump to navigation Jump to search
(New page: {{Nav My Ini Design}} PHP has a number of functions for manipulating the php.ini file great I thought there must be similar functions for my.ini files. If there is I could not find them h...)
 
No edit summary
Line 1: Line 1:
=[http://itygeligub.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
{{Nav My Ini Design}}
{{Nav My Ini Design}}
PHP has a number of functions for manipulating the php.ini file great I thought there must be similar functions for my.ini files.
PHP has a number of functions for manipulating the php.ini file great I thought there must be similar functions for my.ini files.
Line 17: Line 18:
=== my_ini_set ===
=== my_ini_set ===
'''''Description'':''' Sets the value of the given configuration option within the specified block.  
'''''Description'':''' Sets the value of the given configuration option within the specified block.  
<pre>
&lt;pre&gt;
   my_ini_set ( string $file, string $block, string $optionname , string $newvalue )
   my_ini_set ( string $file, string $block, string $optionname , string $newvalue )
</pre>
&lt;/pre&gt;
'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''


=== my_ini_get ===
=== my_ini_get ===
'''''Description'':''' Returns the value of the configuration option within the specified block on success.
'''''Description'':''' Returns the value of the configuration option within the specified block on success.
<pre>
&lt;pre&gt;
   my_ini_get (string $file, string $block, string $optionname )
   my_ini_get (string $file, string $block, string $optionname )
</pre>
&lt;/pre&gt;
'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''


Line 33: Line 34:


PHP has two really neat functions that perform this task with ease:
PHP has two really neat functions that perform this task with ease:
{|cellpadding="5"
{|cellpadding=&quot;5&quot;
|-valign="top"
|-valign=&quot;top&quot;
|'''$array=file($file)'''||This reads every line from named file $file and saves each line as an element in the array $array
|'''$array=file($file)'''||This reads every line from named file $file and saves each line as an element in the array $array
|-valign="top"
|-valign=&quot;top&quot;
|'''file_put_contents($file,$array)'''||This writes the entire contents of array $array into file $file.
|'''file_put_contents($file,$array)'''||This writes the entire contents of array $array into file $file.
|}
|}
Line 47: Line 48:
Create a new folder named test and the following files with content as shown below
Create a new folder named test and the following files with content as shown below
{|
{|
|-valign="top"
|-valign=&quot;top&quot;
|
|
UniServer\plugins\test\'''Run_test.bat'''
UniServer\plugins\test\'''Run_test.bat'''
<pre>
&lt;pre&gt;
COLOR B0
COLOR B0
@echo off
@echo off
Line 56: Line 57:
..\..\usr\local\php\php.exe test.php
..\..\usr\local\php\php.exe test.php
pause
pause
</pre>
&lt;/pre&gt;
UniServer\plugins\test\'''test.php'''
UniServer\plugins\test\'''test.php'''
<pre>
&lt;pre&gt;
<?php
&lt;?php
  $ini_array = file("cron.ini");
  $ini_array = file(&quot;cron.ini&quot;);
  file_put_contents("out_cron.txt",$ini_array);
  file_put_contents(&quot;out_cron.txt&quot;,$ini_array);
?>
?&gt;
</pre>
&lt;/pre&gt;
UniServer\plugins\test\'''cron.ini'''
UniServer\plugins\test\'''cron.ini'''
<pre>
&lt;pre&gt;
; Test example file for cron timers
; Test example file for cron timers
; Period values hourly, daily or weekly
; Period values hourly, daily or weekly
Line 83: Line 84:
period = hourly
period = hourly
ref =  
ref =  
</pre>
&lt;/pre&gt;
|
|
&nbsp;
&amp;nbsp;
|
|
'''''Run_test.bat'':'''
'''''Run_test.bat'':'''
Line 142: Line 143:


{|
{|
|-valign="top"
|-valign=&quot;top&quot;
|
|
However to manipulate the content of an array we need access to its keys and values.
However to manipulate the content of an array we need access to its keys and values.
Line 158: Line 159:
'''''Note'':''' A key points to a location of an element (value) within an array.   
'''''Note'':''' A key points to a location of an element (value) within an array.   
|
|
&nbsp;&nbsp;&nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;
|
|
'''''Scan Array'':'''
'''''Scan Array'':'''
<pre>
&lt;pre&gt;
  foreach($ini_array as $key => $value){
  foreach($ini_array as $key =&gt; $value){
   Do something
   Do something
  }
  }
</pre>
&lt;/pre&gt;
'''''Scan Array and print'':'''
'''''Scan Array and print'':'''
<pre>
&lt;pre&gt;
  foreach($ini_array as $key => $value){
  foreach($ini_array as $key =&gt; $value){
   print "Key = $key Value = $value";
   print &quot;Key = $key Value = $value&quot;;
  }
  }
</pre>
&lt;/pre&gt;
|}
|}
'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
=== Test Code ===
=== Test Code ===
{|
{|
|-valign="top"
|-valign=&quot;top&quot;
|
|
'''''Edit Script'''''
'''''Edit Script'''''
Line 189: Line 190:
* Writes array to file
* Writes array to file
|
|
<pre>
&lt;pre&gt;
<?php
&lt;?php
  $ini_array = file("cron.ini");
  $ini_array = file(&quot;cron.ini&quot;);


  foreach($ini_array as $key => $value){
  foreach($ini_array as $key =&gt; $value){
   print "Key = $key Value = $value";
   print &quot;Key = $key Value = $value&quot;;
  }
  }


  file_put_contents("out_cron.txt",$ini_array);
  file_put_contents(&quot;out_cron.txt&quot;,$ini_array);
?>
?&gt;
</pre>
&lt;/pre&gt;
|-valign="top"
|-valign=&quot;top&quot;
|
|
'''''Run Script'''''
'''''Run Script'''''
Line 211: Line 212:
'''''Important Note'':'''
'''''Important Note'':'''


The line: '''print "Key = $key Value = $value";''' does not require a new line character '''"\n"''' each line read from a file is intact new line characters are not stripped. Hence each value (line) in the array contains a new line character.
The line: '''print &quot;Key = $key Value = $value&quot;;''' does not require a new line character '''&quot;\n&quot;''' each line read from a file is intact new line characters are not stripped. Hence each value (line) in the array contains a new line character.




|
|
<pre>
&lt;pre&gt;
Key = 0 Value = ; Test example file for cron timers
Key = 0 Value = ; Test example file for cron timers
Key = 1 Value = ; Period values hourly, daily or weekly
Key = 1 Value = ; Period values hourly, daily or weekly
Line 234: Line 235:
Key = 16 Value = ref =
Key = 16 Value = ref =
Press any key to continue . . .
Press any key to continue . . .
</pre>
&lt;/pre&gt;
|}
|}
'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''

Revision as of 01:16, 24 November 2010

This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page

MPG UniCenter

MPG UniCenter

Uniform Server Cron Configuration

PHP has a number of functions for manipulating the php.ini file great I thought there must be similar functions for my.ini files.

If there is I could not find them hence decided to write two functions loosely based on ini_set and ini_get another function I hoped would be of use is parse_ini_file however this strips all comments.

This tutorial again is practical it covers design steps with example code that can be run on Uniform Server 5-Nano.

Problems

Above two functions ini_set and ini_get target unique option names in the php.ini file, however my options are unique to each block hence the same option name can appear in any block.

The two functions resolve this issue.

Top

Functions

Before looking at design details here are the two functions:

my_ini_set

Description: Sets the value of the given configuration option within the specified block. <pre>

 my_ini_set ( string $file, string $block, string $optionname , string $newvalue )

</pre> Top

my_ini_get

Description: Returns the value of the configuration option within the specified block on success. <pre>

 my_ini_get (string $file, string $block, string $optionname )

</pre> Top

Starting point

There are two basic requirements an easy way to read a file into an array and to save that modified array back to the file.

PHP has two really neat functions that perform this task with ease:

$array=file($file) This reads every line from named file $file and saves each line as an element in the array $array
file_put_contents($file,$array) This writes the entire contents of array $array into file $file.

The above hides detail such as open files for write, write data and close file or open file for read, read data into array and close file. Top

Examples Initial Set-up

All examples are CLI scripts I like to run them from a batch file this makes testing easier. I have assumed scripts we will be turned into a pugin hence folder UniServer\plugins is a convenient place to locate test files.

Create a new folder named test and the following files with content as shown below

UniServer\plugins\test\Run_test.bat <pre> COLOR B0 @echo off cls ..\..\usr\local\php\php.exe test.php pause </pre> UniServer\plugins\test\test.php <pre> <?php

$ini_array = file("cron.ini");
file_put_contents("out_cron.txt",$ini_array);

?> </pre> UniServer\plugins\test\cron.ini <pre>

Test example file for cron timers
Period values hourly, daily or weekly

[moodle] path = http://localhost/moodle/admin/cron.php period = daily ref =

[drupal] path = http://localhost/drupal/cron.php period = daily ref =

[dtdns] path = ..\..\plugins\dtdns_updater\dtdns_updater.php period = hourly ref = </pre>

&nbsp;

Run_test.bat:

Sole purpose of this batch file is to run our test script test.php

It display any printed results and pauses allowing output to be read.

Press any key to closes this window.


test.php:

This file is the script we want to develop.

It currently contains two lines the first reads cron.ini into an array named $ini_array.

The second line in a final script would write the array back to cron.ini however for testing it’s clearer to write the array to a second file out_cron.txt


cron.ini:

This is the configuration file we wish to manipulate. It can be named anything you like and have a different file extension if you wish.

It currently contains comments three bocks [moodle],[drupal] and [dtdns]

Each block contains identical configuration options path, period and ref.

The first two (path and period) are user configurable.

The last option (ref) is dynamically configured during script run time purpose is for tracking current status.

Additional blocks added must have a unique name and options set as follows:

path A path including file name of a script to be run by cron
period Sets how often above script is to be run.
ref Set by cron script. Tracker when next to run the above script

Top

Testing

Check the above files are correctly set-up by running Run_test.bat as follows

  1. Double click on Run_test.bat
  2. Command window displays: Press any key to continue . . .
  3. A new file out_cron.txt is created
  4. Check the content of out_cron.txt and cron.ini are identical

Step four confirms correct operation.

Top

Display Array Content

To display an array's content we could use function print_r() for example print_r($ini_array) would display content of $ini_array.

However to manipulate the content of an array we need access to its keys and values.

Further we need to scan every element (line) of the array. A foreach loop performs

this task giving access to both keys and values.


Replacing the Do something with a print statement we can display array's content.

Example on the right prints the variables $key and $value

Note: A key points to a location of an element (value) within an array.

&nbsp;&nbsp;&nbsp;

Scan Array: <pre>

foreach($ini_array as $key => $value){
 Do something
}

</pre> Scan Array and print: <pre>

foreach($ini_array as $key => $value){
  print "Key = $key Value = $value";
}

</pre>

Top

Test Code

Edit Script

Edit test file UniServer\plugins\test\test.php

Add lines as shown on the right the script now

performs the following:

  • Reads file into array
  • Displays Array content
  • Writes array to file

<pre> <?php

$ini_array = file("cron.ini");
foreach($ini_array as $key => $value){
  print "Key = $key Value = $value";
}
file_put_contents("out_cron.txt",$ini_array);

?> </pre>

Run Script

Double click on Run_test.bat

Result as shown on right


Important Note:

The line: print "Key = $key Value = $value"; does not require a new line character "\n" each line read from a file is intact new line characters are not stripped. Hence each value (line) in the array contains a new line character.


<pre> Key = 0 Value = ; Test example file for cron timers Key = 1 Value = ; Period values hourly, daily or weekly Key = 2 Value = Key = 3 Value = [moodle] Key = 4 Value = path = http://localhost/moodle/admin/cron.php Key = 5 Value = period = daily Key = 6 Value = ref = Key = 7 Value = Key = 8 Value = [drupal] Key = 9 Value = path = http://localhost/drupal/cron.php Key = 10 Value = period = daily Key = 11 Value = ref = Key = 12 Value = Key = 13 Value = [dtdns] Key = 14 Value = path = ..\..\plugins\dtdns_updater\dtdns_updater.php Key = 15 Value = period = hourly Key = 16 Value = ref = Press any key to continue . . . </pre>

Top

Summary

That completes our test set-up. Although the test script contains only few lines of code it is a very powerful structure,

The next page covers my_ini_set function.

Top