Sunday, November 23, 2008

Multiple Pages with one PHP file

OMG.. i am so sorry for the delay. i was seriously slow and lazy as promised. well, today i am going to talk about having multiple pages with only one php files.

many PHP noobs, they create one small module with tons and tons of pages. that's really bad because you duplicate loads and loads of the same thing! This is made possible using the system-defined variable $_GET, which returns all $_GET variable .. eg index.php?a=b to get the value 'b' you can use $_GET['a'].

what you can do is to combine these pages into one php files. this makes things easy as you are able to backup less files: instead of hundreds of files.

Previously you can have:
+ index.php
+ postback.php
+ about.php
+ process.php

now you can have:
+ index.php
+ index.php?p=pb
+ index.php?p=a
+ index.php?p=prc

Simply in index.php you just have to use IF ELSE statements, or use any other conditional statements.


// display page for postback
// for about page
// for processing
// displaying main page...


This is simple and easy. If you have common template for all the pages, you can combine and put them before or after the IF conditions. Most client browsers identify different GET headers as different pages, so it doesn't affect much of your application.


MikeT said...

It is not a good idea to have more than one page created in the same PHP file as it breaks the back button and makes it hard to maintain as a developer.

Put it this way I have recently been paid to undo the mess created by someone who did this and it's given me a whole months pay packet just for that! It was hell to fix though even as an experienced developer.

Maybe ok for very small projects, but it's a bad habit to get in to.

The only acceptable time to have multiple pages emanating from one script is when you have a huge database and want to have virtual pages representing different parts of data, e.g. a web directory might have:


thephpdeveloper said...

Hi Mike

nice point. making PHP do this does not affect the browser behaviour as current browsers (IE6-8, FF2.0+, Safari, Chrome) all identify that if you have different queries, it is treated as a different page, and thus added to the history as another entry.

you can still have this, and use mod_rewrite (Apache) or isapi_rewrite (IIS) to rewrite the URL to the way you want it. i.e you pointed out


Sam Yong

iLove Apple said...

Nice, thanks for the post, yes, it does work fine with modern browsers!

John Doe said...

I love this idea regardless of what Mike pointed out. But going back to what Mike said, did the person who pay you was an in-experience developer or did they not know what they were doing? Cuz it's not really the code's fault. Sometimes if a developer doesn't know where to place codes, the codes and functions get messed up. Now going back to the published code, is there a way to just do ?=pb instead of having ?p=pb?

Sankar lp said...

I have read your blog its very attractive and impressive. I like it your blog.

PHP 7 Training in chennai | PHP 7 Training Course

PHP 7 Training in chennai | Online PHP 7 Course