...

View Full Version : index.php?action=XXXXX ..



tylerh
11-21-2007, 07:18 PM
i was wondering how to make a lot of my content to run under index.php instead of a bunch of files

ex. - SITE.tld/index.php?action=contact
vs. - SITE.tld/contact.php

matak
11-21-2007, 07:35 PM
although it might be tempting to use only index.php to show all content, it is wise to use other .php files for pages. especialy files that have special functions - eg. contact.php

but, if you want it all to be index.php you can use $_GET[] predefined variable.
what does get do?

if your requested url was something like index.php?id=xxx then this PHP code in index.php file

echo $_GET['id'];

will echo 'xxx' .

you can then use $_GET to include files on your page like this

include ($_GET['id'].html);

in your case it would include xxx.html file .

be careful when using get when page first loads, beacouse then GET isn't set. you might use if control structure with isset to load get.

it would be something like


if (!isset($_GET['id'])) {
//code to load default page (page when get is not set)
}
else {
//code to use $_GET eg.
include ($_GET['id']."html");
}

remember what i wrote first, it's not smart to use only index. but it is all up to you and what you think is better. i used only index.php for all my site, and when loading forms that have special functions (eg e-mail), i had to write special pieces of code to load those functions.
you can avoid that if you put all your functions in one file and include it on top of your index.php but then it is just unnecessary server load

aedrin
11-21-2007, 08:25 PM
although it might be tempting to use only index.php to show all content, it is wise to use other .php files for pages.

I'm curious what your reasons are.

I myself can't find any obvious reasons.

It's actually a lot more flexible, and I would say that in most cases you should use a solution like that. You also end up repeating a lot less code (you generally shouldn't ever repeat blocks of code).


i used only index.php for all my site, and when loading forms that have special functions (eg e-mail), i had to write special pieces of code to load those functions.
you can avoid that if you put all your functions in one file and include it on top of your index.php but then it is just unnecessary server load

You're not forced to use 1 include, you can use as many as you want.

matak
11-22-2007, 01:32 AM
sure but if all functions are included in index file, and aren't used always, then why including them?

aedrin
11-26-2007, 04:43 PM
The functions can be included in the individual pages. No one forces you to include them in index.php

croatiankid
11-26-2007, 05:05 PM
I'm curious what your reasons are.

I myself can't find any obvious reasons.

It's actually a lot more flexible, and I would say that in most cases you should use a solution like that. You also end up repeating a lot less code (you generally shouldn't ever repeat blocks of code).

Independent files (preferably with no extensions) are much more user friendly. I'd only use it where it can be beneficial to the user and not harmful, e.g. form information; on my contact page, I use a $_GET variable to see which page the user came from, and based on that set the selected OPTION in the SELECT element (drop-down list).

aedrin
11-26-2007, 05:44 PM
Independent files (preferably with no extensions) are much more user friendly.

Why? A user clicks on links, rarely does a user enter a link manually. And if they do, it's generally only your main URL/homepage.

And if worried about the looks of the link, you can just set up Mod_Rewrite.

rpgfan3233
11-26-2007, 06:20 PM
I have always found that it is easier to manage separate files (contact.php, index.php, forumlist.php, thread.php, etc.) than to destroy your PHP code with comments everywhere just to make sure that you understands the code. Not only that, but it defeats the purpose of having an include statement and causes longer page load times! Modularity is a big thing that has many advantages, though the biggest advantage is probably time.

Zurvan
11-26-2007, 06:47 PM
This is a good way to setup a site that's using one particular template. There's advantages and disadvantages, like every other method of formatting a site.

One thing to keep in mind, though...

This code:



if (!isset($_GET['id'])) {
//code to load default page (page when get is not set)
}
else {
//code to use $_GET eg.
include ($_GET['id']."html");
}

Is just begging to get hacked.

You should have some sort of check to ensure that you're only including valid files. For example, an array of acceptable file names, and if the requested file is not in that array, show a 404 page. When you're including whatever file was requested without checking, you've left a big XSS hole in your site.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum