webmasters.icecaves.net -  - Blog -  -  -  -  - Forums
Hello, Guest! - Register -
ArticlesHTML & CSS ScriptsJavaScript ScriptsPHP ScriptsForums Index Mar 13,  PST
PHP Page Inclusion by URL
Written by: Thomas, Haily | Learn how to set up your web page organization using the php include() function.
  Mxx Digg StumbleUpon  

In this article I'll show you how to set up your web page organization by utilizing php include. This is an alternative to including template files in each of your pages like in this technique. The urls on your site will look something like this: http://yoursite.com/index.php?page=example.html.

First set up the file for index.php and put this code at the top of the page.

Code:
<?php
// Set the homepage. Be sure to include the file extension.
$homepage = 'home.html';
// List your valid pages that are expected to show up as index.php?page=pagename
$validpages = array(

// Just follow the pattern and list all of the page names. Be sure to add the file extension.

'pagename.html' => 1,
'othername.html' => 1,
'articles/example.html' => 1,

);

// Check if the page name in the url matches your list of valid pages
if(isset($_GET['page']) && isset($validpages[$_GET['page']])) {
$loadpage = $_GET['page'];
}else
{
$loadpage = $homepage;
}
?>

Now create a new page. This will be the homepage content, which I will name home.html. On this page put the content you wish to display as the homepage of your site. Do not include the layout on this page. Next, find the part of the code that looks like this.

Code:
$homepage = 'home.html';

Change home to whatever you named the homepage file. In my case it would be home.html.

Now, to add new pages to this setup, simply create the pages that you will call up like home.html, only the page content, not the layout. If you would like to place some files in directories, then the directory will have to be part of the page name. In index.php, edit the following part of the code setting your pages following the same pattern as the code.

Code:
$validpages = array(

'pagename.html' => 1,
'othername.html' => 1,
'articles/example.html' => 1,

);

You can add as many pages as you need.

Your almost done! In index.php, start placing the code for your website layout AFTER the PHP code that is already there. In the part of your website layout for your page content, place the code below to include your files. But before you use the code below, replace SITE with the correct path to your public_html directory.

Code:
<?php
include("/home/SITE/public_html/".$loadpage."");
// Change the above to the path of your website and to the directory where the pages are located
?>

Once completed it should look something like this on index.php.

Code:
<?php
// Set the homepage. Be sure to include the file extension.
$homepage = 'home.html';
// List your valid pages that are expected to show up as index.php?page=pagename
$validpages = array(

// Just follow the pattern and list all of the page names. Be sure to add the file extension.

'pagename.html' => 1,
'othername.html' => 1,
'articles/example.html' => 1,

);

// Check if the page name in the url matches your list of valid pages
if(isset($_GET['page']) && isset($validpages[$_GET['page']])) {
$loadpage = $_GET['page'];
}else
{
$loadpage = $homepage;
}
?>
<html>
<head>
<title>PHP Page Inclusion by URL - IceCaves.net Webmasters</title>
</head>
<body>

<div align="center">
<?php
include("/home/SITE/public_html/".$loadpage.""); //This will display page content
?>
</div>

</body>
</html>

You can still use the include() function for your layout but it's not necessary because when you update this, it'll update the layout for all the pages on your site.

The links

The urls on your site will now look something like this: http://yoursite.com/index.php?page=example.html. See how the URL has this on the end: ?page= After the equal sign (=) put the page name. Remember to include the file extension and any directories.

Thanks for reading! If you need anymore help with this or if you have any questions or suggestions, comment below.

  Mxx Digg StumbleUpon  

| Written by: Thomas, Haily | Added: Feb 7 2010 | Last Modified: Feb 7 2010 | Views: 1,073 | (Log in to rate) |

Member Comments

Icy of icecaves.netFebruary 8 2010, 5:02 am PST - Karma: 0 - Quote - Link -
5/5
Awesome article guys   
Jenny of windymill.skyness...February 8 2010, 10:46 am PST - Karma: 0 - Quote - Link -
4.5/5
Wonderful! So handy, when I decide to put up Windymill's new layout, I'll be sure to re-do everything in accordance to this.  
Grace of moonwalked.netFebruary 8 2010, 12:07 pm PST - Karma: 0 - Quote - Link -
5/5
Helpful, I might use this (=
ZaphiieFebruary 9 2010, 10:16 pm PST - Karma: 0 - Quote - Link -
Awesome article - if only this was here about 6 months ago I learnt php inclusion way back then. It's the only thing I can do with php   

In order to post a comment, you must be logged into the IceCaves.net Community.
Click here to login.


Figmint WindymillSkylish
The IceCaves.net Topsites


Rabidish | NeopetsGuide | Neoeditor | RockyRoadRules | KeliJo.net | Smiley Helper | Windymill | Hidden Star | I Peed A Little | Guildpets | Skyline Designs | Vintaged.org | Neo Nutters | Paint-Pops.net