Page 1 of 1

Help w/ Templates and Basics? :/

Posted: Fri Dec 20, 2002 9:49 am
by PrOzAk
Ok, I've googled this and gone on IRC to ask with help, and either I cant find an answer, or ppl totally misinterpret my question and just point me at the php.com/manual of syntax or whatever.

What im looking for...Is a damned website that can explain PHP minorly IN NEWB TERMS, and how you make a PHP-HTML Template....Aka...as index.php that inclues Like A top header part.html or .php or whatever, A Side menu.html or .php and those stay the same...Just the Body.php changes.

Get it? Normal PHP TEMPLATE STUFF. lol. Thats all, explaination of how to work that and do that. Im a PHP newb so And also how you do like index.php?page=something

Does that just refer to something.php and loads it in the body.php or what?

Thanks for all the help if you actually understand me and can help me unlike the other 20 or so ppl i've tried to contact

PrOzAk

OK Here goes

Posted: Fri Dec 20, 2002 10:10 pm
by evilcoder
I'm going to make this an detailed as i possibly can, so you dont need to go to a website.

OK first off.

Templates are designed for 2 reasons, to take the strain off designers wrists and to allow for easy updating of large sites.

Lets take my site as an example of template driven website.

http://www.digitalnetworkz.com

Underneath that addy is my other address: http://www.clandazed.com which runs all the files, so for the sake of it, enter http://www.clandazed.com instead.

Click on any page and dont worry about the monitor.clandazed...... thats just a site tracking system.

You will notice that all pages come from index.php and are linked by ?com=thepage

now index.php has all the side bars and design elements of the site.

You will notice that every page you click, the menus are the same and so are the voting polls and members bar. However the content in the middle changes. This is done using includes.

Where you want your information to be produced, this code is used:

<?php
if ($com == '')
{
include "homepage.php"
}
else {
include "$com.php"
}
?>

Lets run through that.

if ($com == '') {
include "homepage.php"
}

This is basically saying "if" the variable $com is equal to nothing then include homepage.php. This is good for a startup page.

else {
include "$com.php"
}

What this is saying is, if $com is not equal to "null" or nothing, then include $com.php, whereby $com is the name of file which is called by the query eg: index.php?com=harry this code will look for harry.php and include it where that code is.

Now that you're familiar with variables lets look at other possible ways to use them.

If you want a more secure, still not 100% secure, but anywho, site using that sort of variable system, this can be done:

<?php

if ($com == '') {
include "homepage.php"
}
elseif ($com == 'page1') {
include "page1.php"
}
elseif ($com == 'page2') {
include "page2.php"
}
else {
header("Location: http://www.yourdomain.com")
}

?>

Right, now lets look at that. As stated in the other example of variables if $com equals nothing, then include homepage.php

However, the code has now changed and is now looking for specific variables, eg: index.php?com=page1

Now, thats the same principle as before, if $com equals page1 then include page1.php

But this is where this one is more secure. If the user puts in the wrong query, say for example they type: index.php?com=heaven

then this code will look through all the if's and if it doesn't find heaven as being one of them, it will redirect the person to the mainpage of your site. By using this you are protecting the location of your include files. However, the downside to this is you have to define all the pages you want to be used as queries. If your site is large, this may take ages and will be alot of code for the visitors browser to interpret.

Either way you do this the outcome is going to be an easier to manage website and tends to be faster too.

Now, i skipped the first part of your question for a reason, i forgot about it. No no no, i did that so you could get an idea of how includes are used.

Now back to the template system.

You have your index.php file and its blank. You want to make sure that when you update a design or add code, that you don't have to edit every single page on the site, this is where templates come in to play.

You have 3 files:

header.php
mainsec.php
footer.php

Your header page contains maybe a top navbar and an image. This page will be inserted first, and so must be the file that contains all the <html><head> tags etc etc.. But it mustn't contain </body></html> that will be at the end of the footer file.

Your mainsec for purposes that we've been over it already, contains the PHP code we discussed earlier, and will be the place where all the content will be inserted.

Your footer contains stuff at the bottom of your page and the closing tags for your page.

index.php should look like this:

<?php

include "header.php";
include "mainsec.php";
include "footer.php"

?>

EASY! Now PHP interprets from top to bottom so, naturally, header.php will be inserted into index.php first then mainsec and so on and so forth for as mainy includes as you want.

Well, thats the basics of templates, if you want me to explain more complex templates like placing code into a MySQL database then extracting it as if it were an include i can do that as well. The advantages with that is faster loading websites and webbased modifications.

Hope this helps.

Posted: Sun Jan 05, 2003 12:01 pm
by bohemian
Hello,
If you want a more secure, still not 100% secure, but anywho, site using that sort of variable system, this can be done:
Can you explain what security risk(s) do I face, please.


Also, how do you think of the followings? Is this safe to use isset function?

Code: Select all

<?php
if (isset($com)) &#123;
include("$com");
&#125; else &#123;
include("homepage.php");
&#125;
?>
Thanks in advance!