Click to See Complete Forum and Search --> : Single File ?!?


matronik
04-01-2005, 06:06 PM
I'm asking myself if it's a better idea, while building up a website with a content management system, to use only one file (i.e. index.php that controls sections and actions for each module) or it is a better idea to use different files (i.e. register.php for Registering and consultingDB.php for another purpose).

Please help me to take the best way.

aaroncampbell
04-10-2005, 03:51 PM
Well, I'd say that it really depends on what you prefer. I prefer to seperate it. Unless you are REALLY organized, a one-page site will be an awful lot of code to parse for 1 page. Besides, people DO look at the URL to tell them where they are. It helps to keep it somewhat descriptive.

Just my $0.02

thorpe
04-11-2005, 11:15 AM
it really depends. if you have most of your hard work done in classes then just include them into this one script.

check out my post here ( application design? (http://www.phpbuilder.com/board/showthread.php?s=&threadid=10297850) ), that one single file controls ALL the logic for a complete content management system front end. all in about 50 lines of code. it just calls on my classes to do the hard work.

gfoot
04-11-2005, 02:38 PM
If you really want to implement your site in one file, you can still present different names to the user using mod_rewrite. Or just <?php include("index.php") ?> in each other file if you can't use mod_rewrite.

Personally I'd never want to put too much in one file though - it quickly gets out of control and much harder to maintain.

mrhappiness
04-12-2005, 03:36 AM
I have it like this/*
initialise
- template engine
- database class
- user and permission management
- menu class
*/

//check for logout

//activate account

/*
submitted forms
*/

//login
if ($activation_successful or (isset($_POST['login_send']) or isset($_POST['login_name'])))
include_once INCLUDE_DIR.'pages/login_submit.inc.php';

//forgotten password
if (isset($_POST['login_new_pass']) or isset($_POST['login_mail']))
include_once INCLUDE_DIR.'pages/forgotten_pass.inc.php';

//signup of new user
if (isset($_POST['signup_send']) or isset($_POST['signup_username']))
include_once INCLUDE_DIR.'pages/signup_submit.inc.php';

//contact form sent
if (isset($_POST['contact_send']))
include_once INCLUDE_DIR.'pages/contact_send.inc.php';

//profile changed
if (isset($_POST['profil_send']))
include_once INCLUDE_DIR.'pages/profile_submit.inc.php';

//initialise page to display (content)
if (empty($_GET['show']))
$_GET['show'] = 0;

/*
page to display
*/
if (!in_array($_GET['action', $allowed_nav))
$_GET['action'] = 'main';
include_once INCLUDE_DIR.'pages/'.$_GET['action'].'_display.inc.php';

/*
assign menu to template class
assign statistics to template class
display template
*/
this way i have a small index.php handling all the stuff as a "single point of contact" ;) and the application logic itself is "outsorced"

the links are rewritten (mod_rewrite), noone can include pages that are not meant for inclusion and i feel quite safe :)

Weedpacket
04-12-2005, 09:32 AM
What happens if I submit a form that contains both login_send AND login_mail fields (or any such combination)?

mrhappiness
04-12-2005, 10:26 AM
Originally posted by Weedpacket
What happens if I submit a form that contains both login_send AND login_mail fields (or any such combination)? normally this won't happen as these are the names of the submit buttons

but if somehow i get an HTTP POST request with both fields filled the follwing will happen:
you will be logged in (if username and password are valid and the user is activated yet and not locked by an admin)

Afterwards the code of forgotten_pass.inc.php will be executed and the user with the mail adress specified will receive a new password

That is not a bug, but a feature ;)

maybe i replace the ifs by an if elseif ... elseif :)

thorpe
04-12-2005, 11:38 AM
maybe i replace the ifs by an if elseif ... elseif

or a switch. switches are much easier to read when there are multiple choices, but you only want one to resolve.

mrhappiness
04-12-2005, 11:49 AM
Originally posted by thorpe
or a switch but then I would have to use:switch (true)
case ($activation_successful or (isset($_POST['login_send']) or isset($_POST['login_name']))):
case (next_condition):
...
}which is not really more readable imo

pohopo
04-13-2005, 03:54 PM
i have been working on a portal system and i use one page (index.php) to load everything. as someone said you do need to make sure you are well organized as things can get messy.

i use a tiered model so my functions are in classes, my queries are in stored procs, and my design/presentation are include files i call modules. And for each module I assign a unique prefix to identify anything that come from that module. So for a textbox in module na1 my input would be
<input type=text id=na1_txb_user_name ....> na1 tells me which module (include file) this textbox is from.

okay, went off a little on a tangent. hope this helps.

kryptn
09-23-2006, 08:15 PM
it wasnt that hard. took me a while to actually get how i was going to do it, but you can just make a quick "engine" to get it.

head();
$page = $_REQUEST['page'];
if($page == null){
index();
}
else{
if(function_exists($page))
$page();
else
error($page);
}
foot();


it just gets the function called by the get variable "page." simply put, i would type localhost/?page=script

it would make sure the function script exists, and if it does, it runs it!

my website uses it, and i made a quick template for it.

edit:
the if-elseif-else method of making multiple pages is probably better only if there are few pages (2-4). on my site theres at least 20, so i made that quick "engine"

www.kryptn.com
www.kryptn.com/template.php