PDA

View Full Version : PHP Include into Table




SpaceMagic
Mar 27, 2004, 07:43 AM
Hey,

I've got an include thing going on ..and you click on the menu and it works, great! Page is loaded into table and everything works:

BUT ... how do I get the index.php page to display a page on load...

here this might be better: (work in progress)

http://www.welian.co.uk



sonofslim
Mar 27, 2004, 09:42 AM
not sure what you're asking. are you asking why you're getting that warning? make sure your include points to the right location; you may have to specify a relative path.

for instance, if the script and the include that it's calling are both in the same dir, you can say include('file.inc'); but if your script is, say, down one level and your include is in the root dir, you'd have to use something like this: include('../file.inc'); anyway, that's my first guess; when i get that warning, i usually have made some mistake in locating my include.

SpaceMagic
Mar 27, 2004, 10:42 AM
nah, sorry, my first post seems a bit drunken! LOL.

http://www.welian.co.uk is my site. Basically I have links at the top, as you can see, which php link to the main site. They are working using the include tag in php. However, when directing to my site, I wanted a default to appear, otherwise the area stays blank until you click a link at the top. This, obviously is no good.

Basically, my question:

Does anyone know how to have a php include as a defeault? So that when someone directs to my site the WHOLE page is there not just the menu with a blank area until someone clicks a link.

At the moment, i'm adopting a stupid way of declaring each one:

<?
switch ($page) {
case "home":
include('main.html');
break;
case "news":
include('news.html');
break;
case "howto":
include('howto.html');
break;
case "preview":
include('previews.html');
break;
case "reviews":
include('reviews.html');
break;
case "merch":
include('merch.html');
break;
case "contact":
include('contact.html');
break;
case "about":
include('about.html');
break;
case "glossary":
include('glossary.html');
break;
default:
include('main.html');
}
?>

Obviously this is silly because i'll have to declare a case for every page I make!

jeremy.king
Mar 27, 2004, 11:37 AM
Why not just append ".html" to your argument AFTER validating its okay.

For example.

<?
//Some validation to check for / or .. or even a pattern match on [a-z]+ happens here
//Trim it too to get rid of spaces.

include("$page.html");

?>

The validation is important cause you dont want someone to try to fudge the URL to include /etc/passwd or something similar.

jeremy.king
Mar 27, 2004, 11:39 AM
As for your default. Check $page for a value with the isset() function and send a redirect header to the same page with a page argument in the URL if $page has no value.

Rower_CPU
Mar 28, 2004, 01:15 AM
I do something like kingjr3 recommends, but without the redirect. This gives me modular content but I can reuse header/nav/footer/etc. elements with further includes.

if(isset($page)) {
include('$page.inc');
} else {
include('default.inc');
}

Works great. :)

SpaceMagic
Mar 28, 2004, 04:24 AM
Okish... using your code Rover, as it seems easier for me, how do I set the default.inc and the page.inc - PHP is quite new for me, but it seems very useful. So could you explain like you would a baby (me :) )

Thanks

jeremy.king
Mar 28, 2004, 11:27 AM
Okish... using your code Rover, as it seems easier for me, how do I set the default.inc and the page.inc - PHP is quite new for me, but it seems very useful. So could you explain like you would a baby (me :) )

Thanks

$page is the variable you passed into the page.

So lets say $page="news"

When you call the include function like
include("$page.html")
its like calling it like this
include("news.html");

The beauty of this approach is as you add more pages, you don't have to touch this part of the code.

Say you added another page called otherstuff.html, to generate your layout you would simply pass page=otherstuff in the URL.

Since you don't know a lot about PHP, I would recommend picking up a book or two.

SpaceMagic
Apr 1, 2004, 10:51 AM
Hey! Thanks guys, I got it working with :

<?php
if(isset($page)) {
include($_GET['page']);
} else {
include('main.html');} ?>

And it works a charm! Thanks again

jeremy.king
Apr 1, 2004, 01:13 PM
Hey! Thanks guys, I got it working with :

<?php
if(isset($page)) {
include($_GET['page']);
} else {
include('main.html');} ?>

And it works a charm! Thanks again
Do remember to do some validation on the $page variable. Otherwise someone could "hack" the url to include files that weren't meant to be included.

SpaceMagic
Apr 1, 2004, 03:26 PM
what do you mean hack? you mean someone could write an address after it to include another, say, illegal, site after it? through my server? Not nice... how do I do what your saying?

jeremy.king
Apr 1, 2004, 06:02 PM
They can't include another site, but they could include a file that was NOT intended to be included.
Lets say your website lived on a Unix box at /www and you had no validation on $page.

Something like

http://yourwebsite.com/?page=../etc/passwd

Would output your passwd file.

Now knowing this, hackers could pull almost any readable file off your server. The same is true for Windows servers too. (Dont believe me? Take a look at your access logs and you will most likely see "robot" programs trying weird URLs trying to execute cmd.exe and others)

This is why you NEVER use a variable passed into a webpage without validating its contents especially when dealing with files.

I often match on a regex like [a-zA-Z]+.html or something similar. This way if it fails to match, you know they are trying a URL they shouldn't be.

Happy coding!