Web Hosting Talk







View Full Version : PHP - loading page inside a layout


BSHbrian
09-05-2002, 08:15 PM
Hi, I currently have a gaming site which I just started about a day ago, it is www.gamingexpertz.com, well to get to the main point, I converted the index to php and I want to know how to load a page within the index.php. For example, if you go to www.gamingexpertz.com/index.php you will see the front page of my website, but when I do www.gamingexpertz.com/index.php?page=joinstaff it takes me back to index.php. How do I make this work? Basically I want someone to show me how to load a page inside a layout. I am fairly new in php :)

Thank you!

The Prohacker
09-05-2002, 08:20 PM
if ($page== "joinstaff") {
include("joinstaff.inc");
}


Add that, and put the info you want on joinstaff into joinstaff.inc...

BSHbrian
09-05-2002, 08:28 PM
In what part of the page do I put that, prohacker? Can someone tell me an easier way?

GlideTech
09-05-2002, 08:53 PM
Sample index.php page:

<html>
<head>
<title>Your title</title>
</head>
<body>
<?php
if ($page== "joinstaff") {
include("joinstaff.inc");
}
?>
</body>
</html>



Sample joinstaff.inc:

<b>This is a test. This text will show up in index.php if you call it like: http://www.yoursite.com/index.php?page=joinstaff</b>


Then just add more if statements into the index.php file for other pages:

<?php
if ($page== "joinstaff") {
include("joinstaff.inc");
}
if ($page== "links") {
include("links.inc");
}
if ($page== "forums") {
include("forums.inc");
}
if ($page== "something") {
include("something.inc");
}
?>

BSHbrian
09-05-2002, 08:56 PM
thanks a lot ! that really helped

BSHbrian
09-05-2002, 09:13 PM
Thank you so much for the help, but I have one problem now... if you look at www.gamingexpertz.com/index.php?page=joinstaff, you will see I have an alignment problem! Will someone please help me fix that.

Thanks a lot :D

JustinH
09-06-2002, 06:18 PM
<?php
switch($page) {
case joinstaff:
include("joinstaff.inc");
break;
case links:
include("links.inc");
break;
default:
include("home.inc");
}

The reasons why to use switch:

The more $page values you have the faster the script will run, since it only has to process $page once, instead of multiple times. It really isn't a big deal with only a few but once you get 30-40 pages it's a noticable difference in both page load times, and server load times :).

SpeedFreak
09-06-2002, 07:49 PM
thats a bit of a tedious way of doing it :( - it will take up a lot of space

just put:

<?php
include('/your/web/directory/.$page')
?>

then whatever file you put after .../index.php?page= will be included. that way you dont have to modify it whenever u have a new page. :D

cheers

BSHbrian
09-07-2002, 06:00 AM
Will that fix the alignment problem?? The alignment problem is still there :(

GlideTech
09-07-2002, 11:37 AM
I don't see any alignment problems on your page, but either way alignment is controlled with html not php. Something as simple as <center> or <p align="right"> etc would work.

JustinH
09-21-2002, 02:11 AM
I know the thread is a couple of days old but...

Originally posted by SpeedFreak
thats a bit of a tedious way of doing it :( - it will take up a lot of space

just put:

<?php
include('/your/web/directory/.$page')
?>

then whatever file you put after .../index.php?page= will be included. that way you dont have to modify it whenever u have a new page. :D

cheers

Unless someone screws with the ?page= then you get a crappy file system error. Remember in ANY programming language, you have to create your program to deal with the curious types :).

Ahmad
09-21-2002, 05:23 AM
Originally posted by comphosting
I know the thread is a couple of days old but...



Unless someone screws with the ?page= then you get a crappy file system error. Remember in ANY programming language, you have to create your program to deal with the curious types :).

?page=../../../../../../../../../../../../../../../../../../../etc/.passwd

Ahmad
09-21-2002, 05:25 AM
Originally posted by SpeedFreak
thats a bit of a tedious way of doing it :( - it will take up a lot of space

just put:

<?php
include('/your/web/directory/.$page')
?>

then whatever file you put after .../index.php?page= will be included. that way you dont have to modify it whenever u have a new page. :D

cheers

This is a security threat, you must do some checking for $page. For example, make sure it only consists of alphanumeric characters.

BSHbrian
09-21-2002, 08:52 AM
well on the game cheats part of my website, i have 1000's of pages. How can i make it easier for me to load this amount of pages without adding all the coding for every individual page?

JustinH
09-21-2002, 01:53 PM
Here's what I would do (keep in mind that I'm not that great with regular expressions so something might be wrong here):

If for example the url was http://www.domain.com/?page=index


if (preg_match("/\W/",$page) || !file_exists("/dirtofiles/$page.html")) {
include("/dirtofiles/default.html");
}
else {
include("/dirtofiles/$page.html");
}



Basically what this does is:
The preg_match checks for any "non-word", in otherwords, makes sure there is no character besides letters, numbers and _'s. If it does it simply loads the default page.

If it doesn't then it checks to make sure the file actually exists. If not it loads the default page.

If the previous two ring true (it only contains A-Z, 1-9 and _'s) then it simply includes the file.

[EDIT]
I clicked reply instead of edit :rolleyes:. For whatever reason it's not showing up, but make sure there is a \ BEFORE the W for the regular expression.

JustinH
09-21-2002, 01:55 PM
Note: Make sure to put a "\" before the "W", some stupid bug is making it not show up in the code.

BSHbrian
09-22-2002, 08:39 AM
will that make it so I wont have to add all of my pages?

JustinH
09-22-2002, 02:10 PM
yup... that way it'll include the variable $page, as long as it only contains alphanumeral characters and the _ character. It also will only include it if it actually exists (so you don't get PHP errors).

BSHbrian
09-22-2002, 09:21 PM
Thanks a lot! !!! If there is any other way that is easier than this... please tell me !

Thank you

BSHbrian
09-28-2002, 03:45 PM
Is there a script I could add to make this work (loading pages from a folder, without having to add all the individual coding. )?

Thx