Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWebsite Development and Maintenance

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Dynamic Page Update From External Files

Something I do a lot with willmaster.com and our other sites is a process to make it easy to update pages, whether manually or automatically.

The secret, so to speak, is external files that are brought into the web page.

Purposes for doing so may be:

  1. It may be easier to update a small file than to update an entire web page.

  2. The website (or several websites) may publish the content in the external file on several pages. One update in one file would update all those pages.

  3. The data may need to be up-to-date at the moment the web page is loaded.

There are several types of external files. And, there are several ways to bring files in.

Types of external files:

  1. Plain text, with or without HTML markup, that get updated manually from time to time.

  2. A frequently-updated file maintained by server-side software that runs every day, or other frequency.

  3. The file is server-side software that provides the content on-the-fly.

Ways to bring files into a web page:

  1. With a PHP include() or echo(file_get_contents()) functions.

  2. With JavaScript through a server-side script that converts the content into JavaScript code before sending it to the web page.

  3. With JavaScript through an Ajax call — so bringing in the external content doesn't slow down the page load.

With all those options, it may sound complicated. But it doesn't have to be.

These three steps can make it more straight-forward.

  1. Determine what you want to do (from the first, "purpose", list);

  2. Determine the type of file that will be pulled intointo web pages (from the second, "types", list);

  3. Determine how you want to bring the file into web pages (from the third, "ways", list);

With those decisions made, let's get it done for you.

Although it may affect your other choices, the purpose isn't critical. Even if you don't know the purpose yet, you can still proceed (you may simply be trying these things out).

The type of external file affects how it can be pulled into the web page only if the file contains PHP code. In that case, the PHP include() function is used to pull in the code.

For all other types of files in the list, any of the listed methods to pull in the content can be used.

Methods to Pull in the Content

Each of the methods mentioned in the "Ways to bring files into a web page" list above are addressed here.

1. With a PHP include() or echo(file_get_contents()) functions.

If the file to be pulled into the web page contains PHP code that must be run, then the PHP function include() must be used. It will get the PHP code into the page in time to run the code before the page is sent to the browser.

Example:

<?php include("/subdirectory/webpage.php"); ?>

Replace /subdirectory/webpage.php with the location of the file to be included.

The PHP function echo() prints something to the page. The PHP function file_get_contents() gets the content of a file. With the echo(file_get_contents()) combination, the content of a file is printed on the page.

Example:

<?php echo(file_get_contents("/subdirectory/webpage.php")); ?>

Replace /subdirectory/webpage.php with the location of the file to be included. On many servers, depending on how the server's PHP is configured, you may optionally use an HTTP or HTTPS URL for the location. https://example.com/subdirectory/webpage.php is an example.

2. With JavaScript through a server-side script that converts the content into JavaScript code before sending it to the web page.

If you prefer to publish certain external content as JavaScript, whether or not it already exists as JavaScript, it can be done with the PHP script below.

A reason for doing so would be to discourage spiders from parsing the content.

Here is the PHP script. No customization required. Save the file to your server as file2js.php or other PHP file name that makes sense to you. Make a note of the file's URL.

<?php
/*
Echo File Content as JavaScript
Version 1.0
January 28, 2019
Will Bontrager Software LLC
https://www.willmaster.com/
*/
$contents = '';
header('Content-type: text/javascript');
if( empty($_SERVER['QUERY_STRING']) ) { exit; }
$file = urldecode($_SERVER['QUERY_STRING']);
if( ! preg_match('!^/|https?://!',$file) ) { $file = "/$file"; }
$contents=@file_get_contents($file);
if( ! $contents )
{
    $file = "{$_SERVER['DOCUMENT_ROOT']}$file";
    $contents=@file_get_contents($file);
}
if( ! $contents )
{
    EchoLine('Unable to acquire ' . urldecode($_SERVER['QUERY_STRING']));
    exit;
}
foreach( preg_split('/[\r\n]+/',$contents) as $line ) { EchoLine($line); }
function EchoLine($js)
{
    $js = str_replace("\\","\\\\",$js);
    $js = str_replace("'","\\'",$js);
    $js = str_replace("<!--","<'+'!--",$js);
    $js = str_replace("-->","--'+'>",$js);
    $js = preg_replace('/(scr)(ipt)/i','$1\'+\'$2',$js);
    echo "document.writeln('$js');\n";
}
?>

Pull in the external file content via a script tag using the URL of file2js.php that you uploaded.

Example:

<script src="https://example.com/file2js.php?/subdirectory/webpage.php"></script>

Replace https://example.com/file2js.php with the URL of file2js.php on your server.

Replace /subdirectory/webpage.php with the location of the file to pull into the web page. On many servers, perhaps yours also, (depending on how the server's PHP is configured) the location of the file to pull into the web page may optionally be expressed as an HTTP or HTTPS URL. https://example.com/subdirectory/webpage.php is an example.

file2js.php grabs the content of the indicated file, converts the content to JavaScript, and then inserts the JavaScript-ized content into the web page.

With file2js.php installed and the script tag on your web page, you are good to go.

3. With JavaScript through an Ajax call — so bringing in the external content doesn't slow down the page load.

The Faster Time-intensive Page Load article provides detailed information on how to implement this method. The method can be used whether or not the external file takes a long time to load.

Now you have a number of choices of types of files to pull into your web pages. And you have a number of ways to do it.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC