|
Hello, Guest! - Register - |
|
| Articles | HTML & CSS Scripts | JavaScript Scripts | PHP Scripts | Forums Index | Mar 13, PST |
|
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. <?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. $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. $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. <?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. <?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.
| Written by: Thomas, Haily | Added: Feb 7 2010 | Last Modified: Feb 7 2010 | Views: 1,073 | Member Comments
| |||||||||||||||||||
|
|
|||||||||||||||||||