Results 1 to 3 of 3

Thread: Rewritten PHP Includes tutorial

  1. #1
    [XiRE] is offline x10Hosting Member
    Join Date
    Jul 2006
    Location
    Detroit Michigan
    Posts
    44

    Rewritten PHP Includes tutorial

    PHP includes are an important part of PHP. With HTML sometimes it gets very annoying having to change everything if for example you change your layout, it can be very time consuming. PHP includes break down this process so that can have a header.php file and a footer.php file and these will appear on every main page, so if you were to change your layout you would only have to change these two files.

    1. Here is an example of the HTML page, it is a very simple page to make it easier for you to understand. Basically this coding would be on every individual page but the only bit that would change would be where the content is. So you see how inefficient this is?

    HTML Code:
    <html>
    <head>
    <title>Your page title</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    
    <body>
    <div id="content">Main content here</div>
    
    <div id="footer">Copyright</div>
    </body>
    </html>
    2. Take the page header

    HTML Code:
    <html>
    <head>
    <title>Your page title</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    </head><body>
    And save this as header.php

    3. Take the footer and end html and body tags

    HTML Code:
    <div id="footer">Copyright</div>
    </body></html>
    and save it as footer.php

    4. Now you will be left with the middle part... this should change with each page as this is the actual content. So now we can join this together

    HTML Code:
    <?php
    // include header
    include ("header.php");
    ?>
    
    <div id="content">Main content here</div>
    
    <?php
    // include footer
    include ("footer.php");
    ?>
    Continuing, you could always add an include in the middle that contains your page content (content is placed in content.php

    PHP Code:
    <?php
    // include header
    include ("header.php");
    // include content
    include ("content.php");
    // include footer
    include ("footer.php");
    ?>
    This makes changing layouts simple, without having to change EVERY page every time.

    5 . but say you wanted to have a page.php?id=whatever to include content, no problem. This example is more complex, but still very basic.

    PHP Code:
    <?php
    include ("header.php"); // the header

    if(isset($_GET['id'])){ // if ?id= is found
    $id=$_GET['id']; // convert the $_GET to variable form
    if(is_file($id ".php")){ // if the file is a php file, include it :D
    include $id ".php"// the actual including of the file
    } else { // if the file isnt found
    echo("404 -- file not found!"); // echo the 404 error message
    }}} // close the tags
    else include("home.php"); // if there is no ?id= found, include this page

    include ("footer.php"); // the footer
    ?>
    and thats all you need

    Now if you want to do mod_rewrite... say your page was index.php?id=foo and you wanted to do like index/foo

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule index/(.*)/$ /index.php?id=$1
    and save that as .htaccess

    Now if you wanted to go further with that, you'dhave to edit the above code to your liking but i hope i got you started.

    more information MOD_REWRITE here

    If you like my tutorial, please consider donating a few points to me :D
    Last edited by [XiRE]; 07-28-2006 at 10:23 AM.

  2. #2
    lambada's Avatar
    lambada is offline x10 Elder
    Join Date
    Mar 2006
    Location
    Caister, Gt Yarmouth, Norfolk, ENGLAND
    Posts
    1,222

    Re: Rewritten PHP Includes tutorial

    Good basic tutorial with very effective methods used. I'll donate some poents in a bit, but just a quick suggestion:
    What about covering mod-rewrite rules which x10 supports - i.e. http://foo.com/bar/x/y is converted to http://foo.com/index.php?id=bar&start=x&post=y

    If you do know about this please post or pm me with information, as i'm interested in using this method on my sites for nice neat URL's with the power of ?id=foo etc.
    Lambada - the former Account Manager (before I resigned)




  3. #3
    [XiRE] is offline x10Hosting Member
    Join Date
    Jul 2006
    Location
    Detroit Michigan
    Posts
    44

    Re: Rewritten PHP Includes tutorial

    Quote Originally Posted by lambada
    Good basic tutorial with very effective methods used. I'll donate some poents in a bit, but just a quick suggestion:
    What about covering mod-rewrite rules which x10 supports - i.e. http://foo.com/bar/x/y is converted to http://foo.com/index.php?id=bar&start=x&post=y

    If you do know about this please post or pm me with information, as i'm interested in using this method on my sites for nice neat URL's with the power of ?id=foo etc.
    http://www.webmaster-toolkit.com/mod...enerator.shtml
    Thats what you need to do if your interested in using mod_rewrite.

    It explains this very well. I'll try to explain it in the thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 45
    Last Post: 02-09-2013, 09:45 PM
  2. tons of PHP Resources
    By Chris S in forum Scripts, 3rd Party Apps, and Programming
    Replies: 10
    Last Post: 01-16-2009, 10:07 AM
  3. Unstand PHP?
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 01-07-2008, 09:16 PM
  4. PHP: Includes Tutorial
    By pulse__xx in forum Tutorials
    Replies: 14
    Last Post: 07-27-2006, 11:15 AM
  5. [PHP] Dynamic Includes Tutorial
    By Bryon in forum Tutorials
    Replies: 3
    Last Post: 12-16-2005, 01:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
dedicated servers