[Tutorial PHP] Switching Content / Including Pages

DarkDragonLord

New Member
Messages
782
Reaction score
0
Points
0
Greetings!

Welcome to my PHP Tutorial!
Suggestions are welcome :D

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");
?>
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.

PHP:
if (file_exists('./'.$_GET['ddl'].'.html'))
@include($_GET['ddl'].'.html');
else
@include('./error.html');
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 o_O.. 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
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 :p

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:
Very nice but how do you put in the actual file name, does it automatically search for it?
Yes, automatic search,you just need to say the name of the HTML, as you can see in the script,
PHP:
@include($_GET['ddl'].'.html');
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.
811369asdf.JPG

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 :D
 
Last edited:

DarkDragonLord

New Member
Messages
782
Reaction score
0
Points
0
Hmm.. Just to know. This tuto was useful for someone? Because i always see people using php but declaring each file in arrays so, its kinda boring. My method is a quite simple.
 

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
Hmm.. Just to know. This tuto was useful for someone? Because i always see people using php but declaring each file in arrays so, its kinda boring. My method is a quite simple.

I completely agree with you, I am currently making a tutorial and mod for SMF, using a similar method that will allow you to access all files within a certain folder by using the already created action array that SMF uses. Its uncompleted but have a look anyway, notice the ?action=tutorial which I use.

http://verbsite.x10hosting.com/index.php?action=tutorial

Edit: I'll finish the tutorial and the mod, and make a post about it in a day or two.
 
Last edited:

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
how would you go about retrieving file from a different directory?
since ?dll=dir1/dir2/page1
looks ugly, and the browser might read it wrong?
 

BorderLineSigs

New Member
Messages
232
Reaction score
0
Points
0
nice tut...but it was to long...i got bored half way through it xP...try to shrink it a bit next time(or put some pictures in...not just words and codes...doesn't look very attractive =P)
 

DarkDragonLord

New Member
Messages
782
Reaction score
0
Points
0
how would you go about retrieving file from a different directory?
since ?dll=dir1/dir2/page1
looks ugly, and the browser might read it wrong?


Hmm.. Yeah, its ddl=dir1/dir2/page1
Yeah, looks ugly lol.
To my portfolio, i made a new variable.


So, when someone goes to ?ddl=porfolio ,it calls portfolio.html right? In portfolio.html, i added
Code:
<?php
if ($_GET['ports'])
if (file_exists('./portfolio/'.$_GET['ports'].'.html'))
@include('./portfolio/'.$_GET['ports'].'.html');
else
@include('./404.html');
else
@include ("./portfolio_menu.html");
?>

and then, i created a portfolio_menu.html, that have all links to the portfolios, like 'videos', 'ads' etcetera Like this:
Code:
<a href="?ddl=porfolio&ports=videos" />videos</a>

Now Explaining: When you go to ?ddl=portfolio, it checks if have a ports variable in the URL. If have, goes to portfolio folder and search for the HTML with the name declared at 'ports'. If the file exists, it include, if not exist, it include 404. But when no 'ports' variable is found at URL, it calls portfolio_menu, which is the one with the links.



So, what looks ugliest?
yoursite.com/?ddl=dir1/dir2/page1
or
yoursite.com/?ddl=page1&ports=page2


Lol. And no, no problem, browsers does not understand wrongly. I use the &ports because when i manage to understand how Friendly URL's work, will be easier to make them 'more friendly' than dir1/dir2/page1 (will look like xxx.com/portfolio/videos for example)





nice tut...but it was to long...i got bored half way through it xP...try to shrink it a bit next time(or put some pictures in...not just words and codes...doesn't look very attractive =P)



The tutorial was little. But i used an answer i gave to a possible question users might have (another board asked :p)











AH, an important thing: I was checking my Logs and someone was trying to make his/her homepage shows inside mine, maybe trying to hack or dunno what.... But, since i used if (file_exists, he wasn't able to do anything lol. because the url he was using does not existed in MY host. :D

example: my website, when someone is viewing the portfolio is: www.raphaelddl.com/?ddl=portfolio&lang=en He tried change the variable of ddl to an URL [im just copying from the log, do not try go in there. Might be virus, dunno] like this: http://raphaelddl.ddl.exofire.net/?...tures.de/content_system/ola/itil/&amp;lang=en

he even tried change the &lang= too but not sucessful since my language system just apply IF lagn is en or pt, nothing more lol :D

So, all tries it did just gave him the 404.html hahahahahah
 
Last edited:

ThePaintGuru

New Member
Messages
208
Reaction score
0
Points
0
This is an excellent tutorial, albeit for a simple concept. Brings back memories, too. The first program I wrote in Perl (hadn't heard of PHP yet) was a "CMS" that worked basically the same way. Can be quite useful if you're looking to make an easily-customizable static website.
 

TonnyORG

I Code Things
Community Support
Messages
5,907
Reaction score
16
Points
38
i have one problem here T.T how i can put more than one page?
Can you giveme one example in quote how here
<?php
if ($_GET['ddl'])
if (file_exists('./'.$_GET['ddl'].'.html'))
@include($_GET['ddl'].'.html');
else
@include('./error.html');
else
@include ("./main.html");
?>
but whit more than 3 webs please ^^


thankyou ^^
grettings
salu2
 

DarkDragonLord

New Member
Messages
782
Reaction score
0
Points
0
lol?

make the index with all the layout. And then,put the code in the place the content have to be shown.

create a main.html, with things to be shown when ppl enter in your website. Create a 404.html just for when ppl put wrong links and then, x number of html pages with JUST content.

then the link will be xxx.com/index.php?ddl=NAMEofTheHTMLhere

just that.
 

TonnyORG

I Code Things
Community Support
Messages
5,907
Reaction score
16
Points
38
so, how i can separate each section in main.html?

for example this main.html:
This is the main.html

Title 1
And all the content from the 1 ^^

Title 2 //this is other link
And all the content from the 2

Title 3 //this is other link
And all the content from the 3
How i can separate the links in main.html?
Thankyou for helpme ^^


grettings
salu2
 

DarkDragonLord

New Member
Messages
782
Reaction score
0
Points
0
put in index a menu.
like this:
Code:
<a href="./index.php">index|home</a>
<a href="./index.php?ddl=2">Link to page 2</a>
<a href="./index.php?ddl=3">Link to page 3</a>

main.html
Code:
Title 1
And all the content from the 1 ^^
This will be shown when ppl enter your site


2.html
Code:
Title 2 //this is other link
And all the content from the 2



3.html
Code:
Title 3 //this is other link
And all the content from the 3



like this
 

TonnyORG

I Code Things
Community Support
Messages
5,907
Reaction score
16
Points
38
omg thankyou, so if the page 2 is named "demo.html" in the link i need put "/index.php?ddl=demo" ?

Thankyou for this tutorial is very userfull ^^


grettings
salu2
 
Last edited:

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
How do I change page title and metatags per page when doing this?

lol nvm, found out a way. I don't include anything exept the php code in the main file and I put the meta tags in the template.
Edit:
Whats wrong with:

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
if ($_GET['id'])
if (file_exists('./template/'.$_GET['id'].'.html'))
@include($_GET['id'].'.html');
else
@include('./error.html');
else
@include ("./index.php");
?> 
</body>
</html>

it doesen't include anything...

the source in game.php when accessing it with a browser is:

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
</body>
</html>
Edit:
nvm, got it working :)
 
Last edited:

DarkDragonLord

New Member
Messages
782
Reaction score
0
Points
0
How do I change page title and metatags per page when doing this?

lol nvm, found out a way. I don't include anything exept the php code in the main file and I put the meta tags in the template.
Edit:
Whats wrong with:

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
if ($_GET['id'])
if (file_exists('./template/'.$_GET['id'].'.html'))
@include($_GET['id'].'.html');
else
@include('./error.html');
else
@include ("./index.php");
?> 
</body>
</html>
it doesen't include anything...

the source in game.php when accessing it with a browser is:

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
</body>
</html>
Edit:
nvm, got it working :)

i think its missing a <body> after </head> lol
 

xiadow001

New Member
Messages
23
Reaction score
0
Points
0
mind if I bring this up again?? uhhmm... just wondering what's the use of this..



<a href="?ddl=porfolio&ports=videos" />videos</a>



coz i dont understand why there are two variables..... the ddl and ports .. where does the value of ddl go....:drool:
 

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
maybe the file is called "portfolio&ports=video"? lol. It isn't in the main code...
 

DarkDragonLord

New Member
Messages
782
Reaction score
0
Points
0
mind if I bring this up again?? uhhmm... just wondering what's the use of this..



<a href="?ddl=porfolio&ports=videos" />videos</a>



coz i dont understand why there are two variables..... the ddl and ports .. where does the value of ddl go....:drool:


ur saying in my website?
because ddl=portfolio called the portfolio.html and inside portfolio.html have a code that, if is declared another variable, called ports, it get the html's that refer to the portfolio sections. If no ports are found, is shows the portfolio main menu.

just that..

But i stopped using that, too much things to set when using that. So, now i have a better way.. i did a portfolio folder and inside it, the sections.. so i call ddl=porfolio/videos and it call the video.html inside the porfolio folder xD
 
Last edited:
Top