Re: Redirecting between PHP Pages
From: Justin Wyer (justin_at_isogo.co.za)
Date: 05/19/04
- Next message: Marcin Dobrucki: "Re: PEAR::HTML_Table question: link in cell"
- Previous message: Justin Wyer: "Re: Need Help with installing PHP on Windows 2000 client"
- In reply to: Rob Tweed: "Re: Redirecting between PHP Pages"
- Next in thread: Tim Van Wassenhove: "Re: Redirecting between PHP Pages"
- Reply: Tim Van Wassenhove: "Re: Redirecting between PHP Pages"
- Reply: Rob Tweed: "Re: Redirecting between PHP Pages"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 19 May 2004 16:08:22 +0200
Rob Tweed wrote:
> Hmmm interesting. Let me just put this in context so perhaps others
> can comment and so I can get my head round this some more.
>
> One of the most common things you need to do in a web application is
> as follows :
>
> User has page "a.php" in his browser. It contains a form and a submit
> button. When the user fills out the form and hits the submit button,
> the form contents must be validated at the back end - typically
> against a database, but at the very least in a php script. Depending
> on the outcome of the validation, you want to do one of two things :
>
> - if the validation was OK, move the user to the next page, eg "b.php"
>
> -if the the validation failed, put "a.php" back on the user's
> browser, probably with an appropriate error message.
>
> The validation script could run at the start of "a.php" and redirect
> to "b.php" using header("Location: b.php") if there were no errors.
>
> Alternatively the form in "a.php" could submit with an action of
> "b.php" in which case the validation script would be in "b.php", with
> a redirection back to "a.php" if an error occurred.
>
> Of course a third approach would be to do the validation in a third
> page "c.php" which redirects to "a.php" or "b.php" depending on
> outcome.
>
> A fourth approach would be to have all the HTML and scripts in a
> single container php page, with the various bits of HTML being
> rendered conditionally depending on outcome. I think your suggestion
> is a variant on this theme where the chunks of HTML to be rendered
> come conditionally from files that are included inside a generic
> container page.
>
> The question is, what's the best approach? The first two suffer from
> the nuisance value and limitations of the redirection via the
> header("Location: xxx") that we've already noted. It seems a shame
> that there's no apparent way to do a PHP server-side redirect to
> another page in a way that would retain the current environment -
> variables, arrays etc - that seems a simpler and more intuitive
> approach than the alternatives.
>
> What's the most common practice(s) for this scenario? Any other
> thoughts/ideas?
>
> Rob
>
>
> On 16 May 2004 17:01:29 GMT, Tim Van Wassenhove <euki@pi.be> wrote:
>
>
>>In article <dn5fa0hv2d0nq8ges341fq07fj5apvtdb1@4ax.com>, Rob Tweed wrote:
>>
>>>>>If I'm in PHP page a.php, and I want to switch control to page b.php,
>>>>>the only mechanism I've come across is to use header('Location:
>>>>>someURL") ;
>>
>>>>What is wrong with require(_once), include(_once), readfile, eval
>>>>functions?
>>>
>>>Sorry but I'm not sure what you're getting at. Could you give an
>>>example of what you mean - ie how these functions can be used to
>>>achieve what I'm trying to do?
>>
>>Well, assume you have a file index.php
>>
>>If the user is authenticated, he should see a screen with his settings,
>>if he's not authenticated, he should see the login page.
>>
>><?php
>>
>>if (isAuthenticated()) {
>>
>> // lookup some stuff we would like to pass to the next page
>>
>> require_once('user_settings.php');
>>
>>} else {
>>
>> require_once('user_login.php');
>>
>>}
>>?>
>
>
> ---
> Rob Tweed
> M/Gateway Developments Ltd
>
> Global DOMination with eXtc : http://www.mgateway.tzo.com
> ---
Rob you can do all of this in one page say index.php, you are mistaking
displaying output with requiring multiple files
if ($_POST['postform'] == "login")
{
..... Validate the user details however u like
}
if ($uservalidated == true)
{
..... Display page here
}
else
{
..... Display login form
}
Now in your html you display for the login form
<form action="index.php" method="post">
<input type="hidden" name="postform" value="login"/>
......... rest of form
Most sites only run off of their index.php and just call functions from
other php files, you should never need a redirect your page except to
reload index.php before any output for some reason, you can dump any
varibles u need to carry over in a rediect into your session, try
running your index.php like the void main() of a c app you will find
life becomes much easier.
- Next message: Marcin Dobrucki: "Re: PEAR::HTML_Table question: link in cell"
- Previous message: Justin Wyer: "Re: Need Help with installing PHP on Windows 2000 client"
- In reply to: Rob Tweed: "Re: Redirecting between PHP Pages"
- Next in thread: Tim Van Wassenhove: "Re: Redirecting between PHP Pages"
- Reply: Tim Van Wassenhove: "Re: Redirecting between PHP Pages"
- Reply: Rob Tweed: "Re: Redirecting between PHP Pages"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]