Have you ever wonderd how websites have that
cool linking? or is there a simpler way than using iframes? Well with
this tutorial I am going to show you all of those. You will be able to
edit one file for your links, and all the rest of your templates.
PHP Include
Have you ever wonderd how websites have that cool linking? or is there
a simpler way than using iframes? Well with this tutorial I am going to
show you all of those. You will be able to edit one file for your
links, and all the rest of your templates.
The Code:
<?
$dir = "directory/to/your/files/" #make sure you have a forward slash at the end.
if($action) {
$page = "main.$action.php";
if(file_exists($dir . $page)) {
include($dir . $page);
} else {
include($dir . "main.error.php");
}
} else {
include($dir . "main.main.php");
}
?>
Now, if you know php farly well then you might as well skip this and
have fun with your new script. But for those of you that dont quite
have the knowledge please keep reading.
If you get an error you might need to add the fallowng line to the top of your script.
$action = $_GET['action'];
The nice thing about this script is that it makes sure that the file
that is spesifed in the URL of your link exist on your server. This
will make it harder for others to get into your site.
Basic linking for this script.
To use this script make your links on your site as fallows.
page.php?action=tutorials
Replace page with the name of the file the script is in.
Replace tutorials with the name of the file on your server.
Such as main.tutorials.php would be the name of the file included in the script.
Your might be askng why put the main.
infront of the file? Well this is just how I organize my content. Main
means the file is being used in the main section of the webpage. I
might also have another set of files with the prefix of tutorial. which would be a file that contains a tutorial page.
001.<?
002.$dir = "directory/to/your/files/" #make sure you have a forward slash at the end.
003.
004.if($action) {
005.
006. $page = "main.$action.php";
007.
008.
009. if(file_exists($dir . $page)) {
010.
011. include($dir . $page);
012.
013. } else {
014.
015. include($dir . "main.error.php");
016.
017. }
018.
019.} else {
020.
021. include($dir . "main.main.php");
022.
023.}
024.?>
What I just did was add line numbers so you can fallow what im saying easyer.
First we are going to start with line 1.
001.?
That is a tag for opening a php script there are some other ways as show below.
<?php
<script language="php">
002.$dir = "directory/to/your/files/" #make sure you have a forward slash at the end.
This is the directory to where your files are stored. This lets the rest of the script know where to look for the files.
004.if($action) {
An if statment looks to see if the condition is true or false so in
this if statment it is loooking to see if $action is true or not NULL
006. $page = "main.$action.php";
In this part of code we are setting a varible to the name of the file we will end up including.
009. if(file_exists($dir . $page)) {
This is another if statment, but in this one we are checking to see if a file exists using the function file_exists();.
If the file exists the statment will return true and contune the rest
of the script. if the file does not exists the statment will return
false and go to the else(line 013) part of the statment whtch we will
be getting to later.
011. include($dir . $page);
This part of the script includes the page we want. In the directory we specifide. You can go here to see a tutorial on include();
013. } else {
Now we are to the else part of the if statment above, as I promised.
This part of the statment will be exicuted if the condition in the
statment returns false.
015. include($dir . "main.error.php");
This is part of the else in are if statment. When the statment returns false this will execute just like the one on line 011.
017. }
This closes the if statment so nothing below it will be part of any
results of the condtion(so if it returns true or false it wont make a
difference everything below will still execute).
019.} else {
You might be asking why there is another else in this script. Well this
else is for the if statment on line 004. So if that statment returns
false($action is NULL or another wise known as not set) It will execute
another set of code.
021. include($dir . "main.main.php");
This part of code is much like the others (the other includes that is). This one will include your main page. This is in here so when someone first comes to your site something will show besides an error.
023.}
This closes the first if statment.
024.?>
This is the closing of the php script. You can use these to close as well. To mach the ones above I showed.
?>
</script>
Well thats it fokes there is my tutorial on advanced includes.
|