This script written in PHP 5 allows you to copy a tag with the given ID attribute in an HTML page on any other HTML pages in the same directory and its subdirectories.

It can be used for example to incorporate the Analytics code on all pages of a site built from files stored locally.

If the site is powered by a CMS, this script is useless if you want to put the code to propagate in the template, for example in the footer or a sidebar. If you want to put it in the body of the articles, SQL queries must be used to propagate the data, according to the tables of the CMS, and that goes beyond the scope.


Due to a bug in PHP 5.2.8, the copy is turned off in pages containing PHP code.
When a PHP script is contained in a Web page, and that the script code has a comparison with the ">" (greater than) symbol, then the loadHTMLFile function (used by Propagator) replaces the end of the script "?>" by the string "?&gt ; ".

This is clearly a bug in PHP and we has to wait until this is corrected.
At this time we can not use the -p option to handle files containing the PHP code.

The php-test.php script in the archive could be used to test the method.

Using the script

It works in command line on Windows, Linux and other systems that support PHP provided that PHP 5 is installed.

    1. You must have a local copy of the site. The script does not work remotely.
    2. Make a backup.
    3. Insert the data to propagate in a file, usually into index.html.
    4. Give an identifier to the tag that holds these data.
    5. Choose the option for the location where to copy the code in pages (see options below).
    6. Run the script.
    7. Check the results on some samples of pages.
    8. If the code of a page is malformed, the PHP DOM functions will not work, you have to copy the code manually.

The script can be reused indefinitely on the same site. The code is added to the pages first, then it will be updated the next time when you change the source page.

You can also delete the codes previously included with the appropriate command.


The syntax of the command is as follows:

      php propag.php [options] ID sourcefile 

ID is the name of the identifier of the tag containing the code to copy.


<div id="demo"> 
 <script .... 
 </ script> 
 </ div> 
php propag.php demo sourcefile      

sourcefile is the path and file name of the file that holds the code to copy. Generally index.html.

You can type php propag.php simply to display the version and options.


You can combine the options after the - sign, for example: -hbi

-h copy the code in the <head> section. By default it is copied into <body> at the end.

-b copy the code at the beginning of the contents of the head or body tag. By default it is added to the end.

-i limit to the directory, no change in sub-directories.

-d delete the code in all the pages where it was inserted.

-v display the details of operations.

-q display nothing (except error messages).

-0 .. 9 defines the minimum size of a file to be taken into account. The digit is multiplied by 1024. The default is 1, so files with less than 1024 bytes are ignored.

-t test just to see what happens, should be used in conjunction with -v as -tv.
No files will be changed.

The -p option should not be used with PHP 5.2.8 and all versions having the loadHTMLFile bug described above.


The script can be downloaded in a ZIP archive.

Download Propagator

Requires PHP 5.
GPL 3.0.