Reduce Website Maintenance by Importing Common Contact Information with PHP

While working on websites, one thing I've learned over the years is that change happens. People will leave the organization. Organizations change names. When change occurs, the thing we need to keep in mind is how that change affects the website. For example, if someone retires and their contact information is listed dozens or hundreds of times throughout the website, it's going to take a while to update everything. But this type issue could be avoided. Instead of hard-coding the information everywhere, it could be imported with PHP.

Using a Database

Now the information could be stored in a database. For example, maybe you have a table containing all the staff information for your organization. With a quick query, you could pull Sally Joe's contact information for any pages that need it. Then if her email address changes, you only need to update the database. But what happens if Sally leaves the organization? If you're pulling the information based on an ID number, you may still need to go through the website and replace each query with the new ID.

Also, a database may be overkill for importing something very specific like the organization's name or a common phrase to be used throughout the website. So let's take a look at some other options for importing content.

PHP's include() or require()

Common information could be stored in an external file and imported using an include() or require() statement. For example, let's create a file called "include_WebmasterContactInfo.html" which contains the following:

please email <a href="mailto:webmaster@yourwebsite.com">webmaster@yourwebsite.com</a>.

We could then grab the information with the following code:

echo "<p>The registration form is temporarily unavailable. If you have any questions, ";
include 'include_WebmasterContactInfo.html';
echo "</p>";

As a result, the website will display "The registration form is temporarily unavailable. If you have any questions, please email webmaster@yourwebsite.com."

The downside of including information this way is that it's imported as is. In the previous example, to display the information multiple times on the same page, we would need to execute the include statement every time. However, we could modify include_WebmasterContactInfo.html so that it doesn't echo directly to the screen. Instead the information could be assigned to a variable:

$contact_info = 'please email <a href="mailto:webmaster@yourwebsite.com">webmaster@yourwebsite.com</a>.';

Now we can import the new variable and use it throughout our page. Note that the include() statement needs to be executed before the variable can be used.

include 'include_WebmasterContactInfo.html';
echo "<p>The registration form is temporarily unavailable. If you have any questions, $contact_info</p>";

PHP's file_get_contents()

Another option for importing information is the file_get_contents() function. So if our include_WebmasterContactInfo.html looks like the following:

please email <a href="mailto:webmaster@yourwebsite.com">webmaster@yourwebsite.com</a>.

The content could be displayed directly to the screen:

echo "<p>The registration form is temporarily unavailable. If you have any questions, ";
echo file_get_contents('include_WebmasterContactInfo.html');
echo "</p>";

Or it could be assigned to a variable to use later:

$contact_info = file_get_contents('include_WebmasterContactInfo.html');
echo "<p>The registration form is temporarily unavailable. If you have any questions, $contact_info</p>";

The benefit of using file_get_contents() is that you can create the variable on the fly. You also don't need to remember what the variable was called in the included file.

0 Comments

There are currently no comments.

Leave a Comment