Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
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!

Import Search-engine-friendly Content

Content imported with PHP becomes part of the web page source code. Therefore, search-engine robots see PHP-imported content like they see the content of a static web page.

(JavaScript-imported content is less likely to be indexed as it's not seen like static content of a page — if it's seen at all.)

There are various ways to import content into web pages with PHP.

Using PHP to insert content into web pages has numerous advantages, including

  1. Mastheads, ads, navigation links, footers, and other content can be inserted into pages with one line of code.

  2. When the content is identical on two or more pages, only one central file needs to be updated to affect the content on every page.

  3. Variable date or time sensitive content can always be current when the page loads.

  4. The source code of the page appears like it would for a static page. That's because the page is generated before it's sent to the browser. (Unlike JavaScript, which would import content after the page source code is sent to the browser.)

  5. Importing with PHP makes the imported content available to search engine indexing spiders. (Unlike JavaScript imported content, which is less likely to be indexed.)

One line of code brings you the benefits of importing content with PHP.

PHP can be used to import non-PHP content, even plain text. And it can of course also be used to import PHP content. (PHP content generally contains PHP code within it, but it isn't strictly necessary that it does. PHP content is content in a .php file that would display correctly in the browser.)

The ability to import with PHP can be really handy, a time saver.

Important: There are risks associated with using these PHP functions to import content from other websites, websites you don't control (as there are risks using JavaScript or other technology to import content). Some PHP functions have more risk than others. See the Risks of Importing Remote Content with PHP article for an explanation of some of the risks.

Below is a list of various PHP functions that can be used to import content to help you decide which line of code to use.

You'll probably prefer readfile(), file_get_contents(), or include(), perhaps use one or the other exclusively. (The links in the list are to function-related documentation at php.net.)

  1. readfile() and file_get_contents() — grab a file for inserting the content into the web page. There's no PHP or any other interpretation. Whatever's in the file is inserted into the page as is.

    Any accessible file can be imported into the web page — plain text, content with HTML markup, even files with binary code, like image files.

    readfile() inserts the grabbed contents directly into the web page. file_get_contents() gets the content of the file, which is inserted into the web page with additional code. (In PHP software, file_get_contents() can also used to get contents from a file for reasons other than inserting the content into a page.)

    The file can be specified either as a location on the server or, if "fopen wrappers" are enabled for your server, as a URL to any valid location on the internet. (If you don't know whether or not your PHP installation can use a URL for the location, you can give it a try. Or ask the hosting company.)

    Here are two examples with each of the mentioned functions, one with the file to import specified as a location on the server, the other specified as a URL:

    <?php readfile($_SERVER["DOCUMENT_ROOT"] . "/subdirectory/file.txt"); ?>
    <?php readfile("http://www.blogsjustin.com/"); ?>
    
    <?php echo(file_get_contents($_SERVER["DOCUMENT_ROOT"] . "/subdirectory/file.txt")); ?>
    <?php echo(file_get_contents("http://www.blogsjustin.com/")); ?>
    
    

    In the first line, $_SERVER["DOCUMENT_ROOT"] specifies the server directory path to the document root. (The document root is the directory where your domain's main or index file is located.)

    After $_SERVER["DOCUMENT_ROOT"] is a dot and then the quoted location of the file. The location of the file is the file's URI, the URL minus the http:// and domain name. As an example, the URI of http://example.com/subdirectory/file.txt is /subdirectory/file.txt

  2. include() and include_once() — grab a PHP file and insert its content into the web page. The file must be a valid PHP file; otherwise, an error will be spawned.

    The difference between include() and include_once() is that include() will include a specific file every time its told to import the file and include_once() will include a specific file only the first time it's told to import the file into the current page.

    With the following two lines, file page.php will be included twice:

    <?php include($_SERVER["DOCUMENT_ROOT"] . "/page.php"); ?>
    <?php include($_SERVER["DOCUMENT_ROOT"] . "/page.php"); ?>
    
    

    With the following two lines, file page.php will be included only once per page, the first time include_once is called to import file page.php:

    <?php include_once($_SERVER["DOCUMENT_ROOT"] . "/page.php"); ?>
    <?php include_once($_SERVER["DOCUMENT_ROOT"] . "/page.php"); ?>
    
    

    Additional files can be imported with include_once(), but only once for each specific file per web page.

    The function include_once() may come in handy if you use several templates and both have a specific ad (for example) imported. If both templates are used to construct a page, then only the first include_once() will import the ad.

    The PHP file can be specified either as a location on the server, as in the above examples, or, if "URL include wrappers" are enabled for your server, as a URL to any location on the internet that responds with valid PHP code. (Uncertain whether or not your PHP installation can use a URL for the location? Give it a try or ask your hosting company.)

    Here are two examples, one with the file to import specified as a location on the server, the other specified as a URL:

    <?php include($_SERVER["DOCUMENT_ROOT"] . "/page.php"); ?>
    <?php include("http://www.blogsjustin.com/"); ?>
    
    

    In the first line, $_SERVER["DOCUMENT_ROOT"] specifies the server directory path to the document root. (The document root is the directory where your domain's main or index file is located.)

    After $_SERVER["DOCUMENT_ROOT"] is a dot and then the quoted location of the file. The location of the file is the file's URI, the URL minus the http:// and domain name. As an example, the URI of http://example.com/page.php is /page.php

  3. require() and require_once() — grab a PHP file and insert its content into the web page.

    require() and require_once() are almost identical to include() and include_once() — with one major difference: what the functions do when they can't find the specified file to import.

    include() and include_once() issue a warning and continue with the import. require() and require_once(), however, issue an error and no PHP is executed past that point.

As mentioned earlier, readfile() or include() are likely to be your preferred method of importing content with PHP. If there's no PHP code in the imported file, use readfile(). Otherwise, include().

A person can get spoiled by using PHP to import content. It's so easy, once it's set up. And only one file needs to be changed to update every web page that imports the file.

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
software index page

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