In our last php
Article, we discussed a couple simple functions and some basic premises
of php coding. In this article we are going do something with php a tad more
useful. Not too much more though, so if you are looking for an advanced php
tutorial, this is not it. We are going to make a simple footer with php to
include into any and all of your pages. First thing I try and think about
right after i come up with a script idea is whether it is a good and practical
idea for php code. Just because you can do something does not always
mean you should do it! That brings us to: Why use php to make an included
footer? Using web standards, a footer should contain auxiliary navigation
and things of that nature. From an adult webmaster's (or adult webmistress')
point of view, a footer should also contain a legal link (you know, the 2257
link) and a link or two to filter some more traffic if they make it that far
down the page. That said, let's do something practical with php!
Anything I fail to cover in this article on basic php should be covered in
the previous article and if not, feel free to use the "Feed
Back" link at the end to ask questions or make comments. There are lots and lots of ways to do any one thing with php code, so I make no claims that
this is the best way, it is just a way to do it. Learn as many of the ways
as you can, they all have their uses.
To start with, we are going to need a few things to work with. Basic webmaster/webmistress
tools and abilities are required, a page to use the footer, php installed
on the server, and the actual footer file. If you need the basic skills, read
some of the other articles here. If you have the basic skills, then it is assumed you
have the pages to hang a footer on. We will also assume you have php on your
server or local machine, if not, see this
article on setting up php on a server or testing machine. Take a look
at this footer example.
You will see a very basic and ugly web page with a couple links and not much
else. If you remember from above, that is all a footer is. Look at the code
here. Nothing too fantastic there at all, just HTML (for now), Do notice
it is NOT valid HTML! There are no <html> or <body> tags, it is
just a section of a web page, so when you are writing sections (or snippets)
of code, make sure that code rewriting is turned OFF in your WYSIWYG editor!
Make a file just like my footer or use mine for testing, (I am not proud,
I will take links any way i can get them!) and put it on your server. Name
it footer.htm for the sake of this tutorial. Once we have those things in
place, it is time for the actual php code. Use an existing page of yours (renamed
and NOT a live page of course) and we will add the footer to it. I am going
to use this page for an example
for those of you following along.
Fire up your favorite code editor (Dream Weaver does work for editing php
and so does just about any text editor) and edit the page you are going to
put the footer in. Name the page test.php to make it easy. You can name it
learn_php.php if you want, it does not matter as long as it gets the .php
extension. Go to the second to last line in your test.php file (if you are
just reading now and not doing, view the source of my example
page) and put this code in just above the </body> tag:
<?php
include('footer.htm');
?>
You can see how it should look with this example.
This php code uses the function include
to get the footer and put it in place when a browser calls the text.php file.
Php is a server side language and HTML is a client side language. This means
that all the php code gets parsed (or run) before it ever leaves the server
and HTML does not get rendered until it hits the surfer's browser. Stripping
the Greek from this, before the test.php page leaves your server it will have
the footer as part of the page and virtually no one will ever see your actual
php code. Click this example
and you will see the finished product. View the source and you will see what
appears to be a normal HTML page except the extension. In the future, I will
show you a couple methods for removing the php extension from your files if
they bug you.
The actual explanation for the php code above is the first line (<?php)
tells the php parser on the server to pay attention and do something with
the following lines. The include is the function and it includes the next
text. The parentheses are a bit on the verbose side, which means they are
not exactly needed but are part of good php form. The filename needs to be
quoted so that php does not treat it as a variable but as a value. The semi-colon
MUST be there or the code will fail. It lets the parser know that the particular
function is finished. The last line tells the php parser that it is done for
now and to ignore the rest of the text until it sees <?php again in the
code.
If you uploaded test.php and footer.htm to the same directory and have php installed
and working on your server and did not make any typos this should work just fine for you.
If it does not, check for typos and make sure you uploaded the files to the same folder and
are using your web browser to view them. Unless you have php installed on your workstation,
you will not be able to preview php from the WYSIWYG editor.
Ok, it works...... now what? Now you can add that footer to any page you have on the server
that can parse php. You only need one copy of footer.htm for the whole site. If you want to use
the footer.htm on a page that is not in the same folder as it is, just include the path like you
would for an HTML link or image. For example: include('../footer.htm'); or include('/usr/server/path/footer.htm');
or even include('http://domain.com/footer.htm'); but be warned, it is a security violation to try and
include a file with an http address and good hosts will not allow this to happen. Now we have this footer
all over our site and it is groovy and was a neat little experiment on using php... what is the big deal?
Notice the "Free Porn" link in my original footer. That goes to one of my TGP's. What if I want to add another?
How long will it take to add it to all my site's pages manually? How about you add a new feature to your site
and you want to let your existing members find it as many ways as possible.
Do you want to edit 500 pages (or even 5) or just one? Think about other ways to use this, like as a header
include or links include or what ever you can come up with. Have fun and realize that the true power to php
is it's simple flexibility. For our next trick in coding php we will probably get into variables and other cool
things.... maybe build a simple php page hit counter and integrate it into this footer? Let me know what you think
good or bad in the forums!
