Jump to content

Index.php


garyblackpool
 Share

Recommended Posts

It's a good technique if you want to use index.php as a template. Very often the only difference between similar files is a small content area. headers, footers, and navigation are all the same. You could paste all that stuff into your other files, but maintaining them becomes a chore when you want to make changes. Much easier to change one template file than 20 content files. Less work, less chance for error.So you might build a template system like this. What I'm going to show you is a series of documents that represent stages along the way. You would actually use just the final document, but seeing to to get there.So, start with a basic document and no PHP. (Assume that the banner divs and others actually contain information.)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3.org/TR/html4/strict.dtd"><html>	<head>		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">		<title></title>	</head>	<body>		<div id="banners"></div>		<div id="navigation"></div>		<div id="content"></div>		<div id="footer"></div>	</body></html>

Now we'll use a PHP include to add content to the content div:

	<body>		<div id="banners"></div>		<div id="navigation"></div>		<div id="content">			<?php include "default.php"; ?>		</div>		<div id="footer"></div>	</body>

Now some flexibility:

		<div id="content">			<?php				$file = $_GET['file'];				include $file;			?>		</div>		<!-- link to it this way: <a href="index.php?file=something.php">something</a> -->

Now we need to give us some options, error-handling, and a little security:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3.org/TR/html4/strict.dtd"><html>	<head>		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">		<title></title>	</head>	<body>		<div id="banners"></div>		<div id="navigation"></div>		<div id="content">			<?php				$file = "restricted/"; // make sure your files really are stored here!				$file .= isset($_GET['f']) && file_exists($file . $_GET['f'] . '.php') ? $_GET['f'] . '.php' : 'default.php';				 // that's a pretty terse statement. Ask if you don't get it.				include $file;			?>		</div>		<!-- 			link to it this way: <a href="index.php?f=something">something</a>			this gets the default: <a href="index.php">Home</a>		-->		</div>		<div id="footer"></div>	</body></html>

An advantage to using query strings is that they are part of simple anchors, and also because get requests are assumed to be stable, and your browser will cache the contents.Oh, yeah. Remember that included files contain ONLY the exact content required to merge with the including document. That is, DON'T frame your included docs in <html>, <head>, <etc> tags. You'll confuse your browser and miss most of the benefit of this whole technique.EDIT. I've spent the last few minutes cleaning up errors.EDIT2. Dang javascript got me confused. I used a + where I should have used a dot. (It's not there now, but if you copied it earlier, you may have grabbed it. It should be easy to find since it's the only + on the page.)Lemme know if I screwed up anywhere else. :)

Edited by Deirdre's Dad
Link to comment
Share on other sites

I am confused abou the: <div id="content"> <?php include "default.php"; ?> </div>
Better tell me how it confuses you.
and the third code box why does that not have the include statement in it or is that the default.php?
It is NOT the default.php. (I've left that to your imagination.) The 3rd box does have an include statement. Maybe you're not used to seeing PHP? The opening and closing tags are top and bottom, and the include statement is the second statement inside the PHP block.The file to be included is not hardcoded. We've used a variable instead, and the value of the variable is passed in the query string on the tag that got us here. But as you can see in the next box, the better set up is NOT to use the complete file name at all. We wouldn't want users going after it without the template to surround it. Edited by Deirdre's Dad
Link to comment
Share on other sites

PHP doesn't know or care what the origin of data is. It has no idea what a form is. A browser takes the data from a form and shapes it into a request that server software like PHP CAN understand.What servers know are GET requests and POST requests. These generally correlate to non-forms and forms, but exceptions are everywhere. These are GET requests:a normal request for a web pagea request for an image named in an image taga request for a CSS style sheet linked to a pagea request for an external script named in a script taga request for a web page with a ?query string attached to the URLForms can use POST or GET methods. GET data is by rule limited to so many bytes, so POST is best for long data. Also, a form request using GET would create a URL with a query string, and a user might not want that data visible to someone else in the room. POST requests never do that, so they are more private in that sense. (Both requests are equally vulnerable to packet sniffers and such, however.)Likewise, an AJAX object can use a POST or GET request. I mostly use POST for forms and AJAX.Anyway, sending PHP a URL with an attached query string causes PHP to populate the $_GET array with items from the string.

Edited by Deirdre's Dad
Link to comment
Share on other sites

The included portions are embedded in the main document at the server. The document received by the browser is an HTML document like any other. So if I understand you, yes, javascript can be a part of it in the traditional manner.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...