Rewritten PHP Includes tutorial

[XiRE]

New Member
Messages
44
Reaction score
0
Points
0
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:
<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:
<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:
<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:
<?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:
<?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:
<?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:

lambada

New Member
Messages
2,444
Reaction score
0
Points
0
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.
 

[XiRE]

New Member
Messages
44
Reaction score
0
Points
0
lambada said:
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_rewrite-rewriterule-generator.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
 
Top