Web Hosting Talk







View Full Version : Which system is better?


Zaitech
09-07-2006, 09:22 PM
When creating a large PHP script, should I just have a ton of if ($_GET["page:) = "register") again and again in one big index.php file, or should I craete various files like index.php, register.php, login.php, etc?

Right now, Im setup with multiple files but I noticed a lot of big websites just have index.php and a ton of variables.

Like this:

index.php?page=members&sortby=C

Rather then having a sepate file like this:

members.php?sortby=C

maxymizer
09-07-2006, 09:38 PM
Websites that you mention don't necessarely have to have a giant index.php. You can also include various files based on input gained via _GET variables.

What you're asking now cannot be answered exactly..In general, it's better to break down common tasks and group them with common functions that work with them.

Your question about having "members.php" file to handle user operations is an answer in itself. So yes, it's better to have more files that will group certain tasks (for example member.php, news.php, login.php etc) because it's easier to maintain.

Having said that, that leads you towards object oriented design and programming which you will probably be looking in next.

layer0
09-07-2006, 09:55 PM
Would probably make more sense to do members.php, login.php, etc. assuming I understand you correctly.

Czaries
09-07-2006, 10:19 PM
You should maybe try something like:

index.php

<?php
// ...
// ...
// General configurations, db connection, etc. above

$page = !empty($_GET['page']) ? $_GET['page'] : 'home';

if( file_exists($page) )
{
include($page . '.php');
}
else
{
echo 'Unable to find requested page';
}

?>

Jatinder
09-08-2006, 12:47 AM
Right now, Im setup with multiple files but I noticed a lot of big websites just have index.php and a ton of variables.

Like this:

index.php?page=members&sortby=C

The sites you are refering to are most probably using a MVC (Model View Controller) approach. In a MVC approach index.php is used as the common entry point for all pages, but the functionality is still split in multiple files.

If you need an easy to understand MVC framework, take a look at http://codeigniter.com

nnormal
09-08-2006, 12:23 PM
The big advantage to using a index.php?p=home type url is that by sending everything through the index it is very easy to set site wide variables, functions, layouts etc. On a more advanced level it lets you use an MVC (model, view control) design pattern which is especially useful when you have designers doing the html/css and developers doing php/js.

simplified example:

index.php

<?
include('settings.php');
include('functions.php');
include('layout.php');
?>


settings.php

<?
$index='index.php';
$title='my webpage';
$default='home';
$pages=array('home','photos','contact');
?>


functions.php

<?
function get_contents() {
global $default, $pages;
if (isset($_GET['p'])) $page = $_GET['p'];
else $page = $default;
if ((in_array($page, $pages))
&& (file_exists($page.'.php'))) include($page.'.php');
else echo 'error page does not exist';
}
function get_nav() {
global $index, $pages;
foreach ($pages as $p) echo '<li><a href="'.$index.'?p='.$p.'">'.$p.'</a>';
}
?>


layout.php


<html>
<title><? echo $title; ?></title>
<table>
<tr>
<td><? get_nav(); ?></td>
<td><? get_contents(); ?></td>
</tr>
</table>
</html>