Newbie - Redirect to do processing

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi there,

I am creating my own php site but am a bit concerned with putting all
form processing into a page that is also displaying stuff (i get
overwhelmed with very long pages and like to separate processing from
displaying for maintainability). I found a possible way of separating
processing through using a redirection such as:

    header("Location:" . mylocation);

so if i was logging into my site instead of going straight to the page
it needs to go to and doing all the post processing there I would have
a "processing page" which would redirect me to the correct location
perhaps depending on what was written. So if the incorrect password was
given it would go to an error page otherwise the user would be
successfully logged in. My processing pages would be in effect the
controller in the MVC pattern.

I was wondering if there were any problems people see in this and in
what situations people use it if at all and in which cases not. It
seems a misuse but if it works....who cares?

It may even be standard practise but I have looked at quite a lot of
open source php projects and found that most people do a lot of
processing in the pages that do the displaying.

Cheers for all your advice,


Re: Newbie - Redirect to do processing wrote:
Quoted text here. Click to load it

I don't think the redirecting for the processing using the header()  
function is neccessarry as the FORM tag in the HTML provides an  
attribute to specify the location to process the form. You shoud use the  
action entry of the FORM element for that (see site about the  
FORM element ). So:

<FORM action="/url/to/processing/page/here" method="post">

In the processing script you can then use the header() function to  
specify the location you want to forward the user to based on the  
entries that were submitted by the form.


Re: Newbie - Redirect to do processing

Thanks for your reply.

This is exactly what i meant. is this really good practise though?
Would this "misuse" of the redirection functionality in browsers work
in all browsers ? Do other people do it?

(The smarty library looks good but it is a quite different approach)

Re: Newbie - Redirect to do processing wrote:
Quoted text here. Click to load it

I don't think doing everything in one place is a good way. That's why  
they invented MVC :)
Anyways, one way would be:

Have a control url parameter, say, page.
Define an array like this:

$pages = array(
   "id1" => "class1",
   "id2" => "class2",
   "idN" => "classN"

Now have a script retrieve the url parameter, and instantiate the  
respective class. Have all classes have a method to output content:

$id = $_REQUEST["page"];
$class = $pages[$id];
$obj = new $$class();

The script will be your controller, the array will be the model, and the  
classes will be the viewports, perhaps together with persistence layer.

This is, of course, overly simplified :)


Re: Newbie - Redirect to do processing

Hi lugh!

I see what you are getting at. I however do not understand how the
array can be a model. Normally in MVC the model is what the view
represents and it (the model) should not really know anything about the
view (or the controller). The controller knows about the view and the

In your example I would say that the array is more like a controller is
it "controls" what the next view will be based upon a url parameter (i

I like the idea that you give pages ids. In fact I do that in some way
as well. Instead of using urls, I use a class which converts ids to
urls so that if i ever change a path I do have to do a grep over all my
php files.

Is this how you organise your php web pages?



Re: Newbie - Redirect to do processing

funky wrote:
Quoted text here. Click to load it

Well, I'm working on an MVC-based library for creating template-driven  
sites right now. There is an abstract Model, View, and Controller  
classes. For templates, we have

       /          \
      /            \
     v              v
TemplateModel <-- TemplateView

In which TemplateModel is data about different pages in xml. The  
viewport provides only output functionality, and is used purely for  
providing layout, filling in "static" parts of it, and then uses an  
object in model to output dynamic content. That variable in model, in  
turn, is instantiated by the controller depending on the input (url  
parameter), and may be another controller or a viewport. So, in this  
case, template model is more or less template view's model :) Another  
type of controller may deal with a database query, or a login  
processing, or somthing else.
Having all this, it becomes quite easy to separate data from logic from  
presentation, and, as a consequence, find and modify specific pieces of  
them :)


Re: Newbie - Redirect to do processing

Thanks for that. That does look like MVC. Why did you choose to build
your own instead of using an open source library like Smarty?

Re: Newbie - Redirect to do processing

oh by the way, I found what I originally asked at:

a really good article!

Re: Newbie - Redirect to do processing

funky wrote:
Quoted text here. Click to load it

Well, one of the reasons is that if you do it yourself, you are likely  
to learn a thing or two :) It also gives you experience and promotes  
analytical thinking :)


- lüpher
"Man sieht nur das, was man weiß" (Goethe)

Re: Newbie - Redirect to do processing

If you want to seperate HTML and PHP you should look into templates,
such as

Re: Newbie - Redirect to do processing

i found smarty straight after I posted so I am glad someone mentioned
it :) It does look like an interesting library along with PEAR::DB

Site Timeline