DarkDragonLord
New Member
- Messages
- 782
- Reaction score
- 0
- Points
- 0
Greetings!
Welcome to my PHP Tutorial!
Suggestions are welcome
Here we will learn a way to include your pages in the same layout, in a easy way.
I'll show all code then comment:
I'll explain what it does than i'll break in.
Insert this in your index.php. If you go to yourdomain.com, it will open index.php and, since no variable is set, it show us main.html. So main.html will be your opening page.
Then, if you make any link as following yourdomain.com/index.php?ddl=something , the code will read the variable ddl and get the "something" name. Then, it will search in the folder for a something.html and include in the place main was. But if something.html is not found, it will replace main.html with a 404.html.
Now, lets break it!
This is our main code. If ddl is set, it goes to the sub-code. If it is not set, it include main.html. This code only happens when you have yourdomain.com/index.php , that means, with no variable ddl.
When you create a link, you should make it as index.php?ddl=nameofpage
the ? after the .php says to the script that is a variable. Then, = is to tell what is the value of the variable.
Now the sub-code. It is triggered when if ($_GET['ddl']) detects a variable.
If the 'ddl' . html exists, include it. Else, show us a error page.
As you can see, this version is a little better and simple to work than coolv1994's one, since we do not need to declare all pages on it (imagine a website with more than 50 pages .. geez).
NOTE1:
Those @ before the include, prevent those PHP error logs like those:
That means, if the code dont have @ and the script generated an error log like those, EVERYONE will see your login name in the host. (the XXXX are the login name). And yy is the line that generated the error.
Using it without @ is good when you are debugging, when the page dont not include and you are sure you are right
NOTE2:
the 'ddl' can be changed to anything you want. Just be sure to change your link's as well (index.php?NameOfVariable= )
Hope you like it.
You can found more tutos at
http://ddl.exofire.net
(implementing tutorial pages this week)
A FULLY EXAMPLE:
Question:
it will get the value of ddl you said in the URL and automatic add a .html, then it will find this "valueOfDdlyouEnter.html"
I'll show you using the template i just made:
It have the following files.
index.php
dummy1.html
404.html
main.html
When you go to the webpage ( http://ddl.exofire.net/temp/index.php ), it include the main.html, since in the url, no ?ddl= was found.
Then, if you click in the links, you can see that is index.php?ddl=dummy1
When you click on it, the script see that now have a value for ddl and then, look in my folder for a dummy1.html
Then, it include the dummy1 and show you.
I made an example with the link "terceiro", linking to dummy2 (but as you can see in my files, there is none dummy2.html). So, when you click in the index.php?ddl=dummy2, the script check for a dummy2.html. Since it will not find any, it includes the 404.html (in the tutorial, error.html).
The good things i think that is including HTML files, not PHP files are:
* Easy to manage - Check the file icons. Green for PHP (Dreamweaver) and blue for HTML (IE) so i can easy find what is what;
* Even as HTML files, you CAN add PHP codes on it. Why? Well, the include is simple get all code in the html, and add it in the .PHP, this way, even as HTML, when the file is included, it become a php file.
Here are examples of the html you need to include:
(the ID and classes i added, is just for formatting them in the CSS)
dummy1.html
404.html / error.html
main.html
Now go http://ddl.exofire.net/temp/index.php and test
As you can see, you DO NOT NEED to add the <html> <body> etc tags.. They already are in the .php. These html are not really html files (for browsing). They are just containers with code and the script will make the 'body replacement', adding the content of the html you ask.
So, if you want to test my theory, change this line:
include($_GET['ddl'].'.html');
with this one:
@include($_GET['ddl'].'.txt');
Then, create a dummy1.txt and test the url index.php?ddl=dummy1 in your host. As you can see, those html codes inside the txt will become part of the php page.
I hope i explained well enough to understand
Welcome to my PHP Tutorial!
Suggestions are welcome
Here we will learn a way to include your pages in the same layout, in a easy way.
I'll show all code then comment:
PHP:
<?php
if ($_GET['ddl'])
if (file_exists('./'.$_GET['ddl'].'.html'))
@include($_GET['ddl'].'.html');
else
@include('./error.html');
else
@include ("./main.html");
?>
I'll explain what it does than i'll break in.
Insert this in your index.php. If you go to yourdomain.com, it will open index.php and, since no variable is set, it show us main.html. So main.html will be your opening page.
Then, if you make any link as following yourdomain.com/index.php?ddl=something , the code will read the variable ddl and get the "something" name. Then, it will search in the folder for a something.html and include in the place main was. But if something.html is not found, it will replace main.html with a 404.html.
Now, lets break it!
PHP:
<?php
if ($_GET['ddl'])
(...)
else
@include ("./main.html");
?>
When you create a link, you should make it as index.php?ddl=nameofpage
the ? after the .php says to the script that is a variable. Then, = is to tell what is the value of the variable.
Now the sub-code. It is triggered when if ($_GET['ddl']) detects a variable.
PHP:
if (file_exists('./'.$_GET['ddl'].'.html'))
@include($_GET['ddl'].'.html');
else
@include('./error.html');
As you can see, this version is a little better and simple to work than coolv1994's one, since we do not need to declare all pages on it (imagine a website with more than 50 pages .. geez).
NOTE1:
Those @ before the include, prevent those PHP error logs like those:
Code:
Warning: include(./error.html) [function.include]: failed to open stream: No such file or directory in /home/XXXX/public_html/index.php on line yy
Warning: include(./error.html) [function.include]: failed to open stream: No such file or directory in /home/XXXX/public_html/index.php on line yy
Warning: include() [function.include]: Failed opening './error.html' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/XXXXX/public_html/index.php on line yy
Using it without @ is good when you are debugging, when the page dont not include and you are sure you are right
NOTE2:
the 'ddl' can be changed to anything you want. Just be sure to change your link's as well (index.php?NameOfVariable= )
Hope you like it.
You can found more tutos at
http://ddl.exofire.net
(implementing tutorial pages this week)
A FULLY EXAMPLE:
Question:
Yes, automatic search,you just need to say the name of the HTML, as you can see in the script,Very nice but how do you put in the actual file name, does it automatically search for it?
PHP:
@include($_GET['ddl'].'.html');
I'll show you using the template i just made:
It have the following files.
index.php
dummy1.html
404.html
main.html
When you go to the webpage ( http://ddl.exofire.net/temp/index.php ), it include the main.html, since in the url, no ?ddl= was found.
Then, if you click in the links, you can see that is index.php?ddl=dummy1
When you click on it, the script see that now have a value for ddl and then, look in my folder for a dummy1.html
Then, it include the dummy1 and show you.
I made an example with the link "terceiro", linking to dummy2 (but as you can see in my files, there is none dummy2.html). So, when you click in the index.php?ddl=dummy2, the script check for a dummy2.html. Since it will not find any, it includes the 404.html (in the tutorial, error.html).
The good things i think that is including HTML files, not PHP files are:
* Easy to manage - Check the file icons. Green for PHP (Dreamweaver) and blue for HTML (IE) so i can easy find what is what;
* Even as HTML files, you CAN add PHP codes on it. Why? Well, the include is simple get all code in the html, and add it in the .PHP, this way, even as HTML, when the file is included, it become a php file.
Here are examples of the html you need to include:
(the ID and classes i added, is just for formatting them in the CSS)
dummy1.html
Code:
<div id="phpinclude">
<h1 class="dest">Title Here</h1>
<p> Lorem ipsum dolor!. </p>
<p> Lorem ipsum dolor!. </p>
</div>
404.html / error.html
Code:
<div id="phpinclude">
<h1 class="dest">Error Page</h1>
<p> This is an ERROR PAGE. </p>
<p> Will show only if the URL is wrong. I've done a link with wrong url so you can check this error page. </p>
</div>
main.html
Code:
<div id="phpinclude">
<h1 class="dest">Welcome to Template #1 by Raphael DDL</h1>
<p> This is a FREE Template provided by .dragon//CREATIVE Website. </p>
<p> Report imediatly if you have purchased or downloaded this template other than ddl.exofire.net</p>
<p>Regards,<br /> Raphael DDL.</p>
</div>
Now go http://ddl.exofire.net/temp/index.php and test
As you can see, you DO NOT NEED to add the <html> <body> etc tags.. They already are in the .php. These html are not really html files (for browsing). They are just containers with code and the script will make the 'body replacement', adding the content of the html you ask.
So, if you want to test my theory, change this line:
include($_GET['ddl'].'.html');
with this one:
@include($_GET['ddl'].'.txt');
Then, create a dummy1.txt and test the url index.php?ddl=dummy1 in your host. As you can see, those html codes inside the txt will become part of the php page.
I hope i explained well enough to understand
Last edited: