Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

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!

Conditional Content Publication

Content can be published to a web page depending on whether or not the web page is in a specific directory.

As an example of use, the navigation area of web pages in the /widget/ directory need a link to each page in the /widget/ directory. But not links to each page in other directories.

As another example of use, let's suppose you want to publish a "See Our New Widget Pages" link on every web page – except the /widget/ directory (because they're already looking at the widget pages).

The PHP code to accomplish it is a two-part snippet. The content to be published is between the two parts. There may be many snippet/content sets to address many different directories.

All the snippet/content sets can be contained in one central file to be imported into individual web pages. Changes to the central file are relatively easy and quick.

You'll find 4 things in this article.

  1. The code to import the central PHP file into web pages

  2. The code and an example of publishing certain content only if the web page is in a certain directory.

  3. The code and an example of publishing certain content only if the web page is not in a certain directory.

  4. An example central PHP file.

Importing the Central PHP File

I will assume the central file is named central.php and that it is located in the document root directory. (The document root directory is where the domain's main/index page is at.)

This is the the code to import the central file into a web page:

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

If your external file is not named central.php or if it is not located in the document root directory, change the above accordingly. For example, if it is located in the "external" subdirectory and named "nav.php", then the PHP code to import it is:

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

Publishing Only When in a Certain Directory

This will print certain content only when the web page is in the /widget/ directory.

<?php if( strpos($_SERVER['PHP_SELF'],"/widget/") !== false ): ?>
Content to publish.
<?php endif; ?>

The first and last lines are the PHP two-part snippet. Everything between those lines is the content. The content can be anything that can be published on a web page.

The content will be published if the web page being loaded is in the /widget/ directory. Otherwise, the content will not be published on that web page.

To change the directory name, change the word "widget" in the first line to the new directory name.

Here is an example to publish certain links only in the /widget/ directory.

<?php if( strpos($_SERVER['PHP_SELF'],"/widget/") !== false ): ?>
<h1>Widget Directory</h1>
<p><a href="one.php">One</a></p>
<p><a href="two.php">Two</a></p>
<p><a href="three.php">Three</a></p>
<?php endif; ?>

Publishing Only When Not in a Certain Directory

This will print certain content only when the web page is not in the /widget/ directory.

<?php if( strpos($_SERVER['PHP_SELF'],"/widget/") === false ): ?>
Content to publish.
<?php endif; ?>

Everything between the first and last lines will be published if the web page being loaded is not in the /widget/ directory. To change the directory name, change the word "widget" in the first line to the new directory name.

This example with publish a "See Our New Widget Pages" link to web pages in any directories except the /widget/ directory.

<?php if( strpos($_SERVER['PHP_SELF'],"/widget/") === false ): ?>
<h3>
<a href="/widget/index.php">
<span style="color:white; background-color:blue">
See Our New Widget Pages
</span>
</a>
</h3>
<?php endif; ?>

An Example Central PHP File

The following example central PHP file specifies three directories for specific content: /widget/, /smidget/ and /mo/. And it also specifies content to be printed to any web pages except those in the /widget/ directory.

<?php if( strpos($_SERVER['PHP_SELF'],"/widget/") !== false ): ?>
<h1>
I print in the "widget" directory.
</h1>
<?php endif; ?>

<?php if( strpos($_SERVER['PHP_SELF'],"/smidget/") !== false ): ?>
<h1>
I print in the "smidget" directory.
</h1>
<?php endif; ?>

<?php if( strpos($_SERVER['PHP_SELF'],"/mo/") !== false ): ?>
<h1>
I print in the "mo" directory.
</h1>
<?php endif; ?>

<?php if( strpos($_SERVER['PHP_SELF'],"/widget/") === false ): ?>
<h3>
<a href="/widget/index.php">
See Our New Widget Pages
</a>
</h3>
<?php endif; ?>

The three named directories will print the content relevant to the directories. All directories except /widget/ will print the See Our New Widget Pages link.

The two-part PHP snippet does a lot of work. It needs to be imported into a PHP web page, of course, in order to run the PHP code. PHP web pages usually have a .php file name extension.

Import the central PHP file into all web pages to be affected. When a browser requests a page, the PHP determines what is and is not published.

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