Redirecting between PHP Pages

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Tweed

    Redirecting between PHP Pages

    Two inter-related questions:

    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('Locatio n:
    someURL") ;

    This has two drawbacks - it seems inefficient to me - you essentially
    have to "bounce" a request/response off the browser to switch between
    PHP pages - leaving and returning to PHP just to switch pages, and
    also it uses the GET method and, I'd assume, therefore restricts you
    on the length of the URL and therefore the number of name/value pairs
    you can send before they get truncated.

    Q1: Is there an internal PHP server-side page redirection mechanism
    that would/could provide a more efficient mechanism for switching
    between PHP pages?

    Q2: Alternatively, is there a POST equivalent to header("Locatio n:")
    that I could use from within PHP to redirect between pages, so that
    the number of name/value pairs I could send is unlimited?



    ---
    Rob Tweed
    M/Gateway Developments Ltd

    Global DOMination with eXtc : http://www.mgateway.tzo.com
    ---
  • Tim Van Wassenhove

    #2
    Re: Redirecting between PHP Pages

    In article <0mhea0d2k2iohg kbcsd8akt5ojbgf 03pau@4ax.com>, Rob Tweed wrote:[color=blue]
    > Two inter-related questions:
    >
    > 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('Locatio n:
    > someURL") ;
    >
    > This has two drawbacks - it seems inefficient to me - you essentially
    > have to "bounce" a request/response off the browser to switch between
    > PHP pages - leaving and returning to PHP just to switch pages, and
    > also it uses the GET method and, I'd assume, therefore restricts you
    > on the length of the URL and therefore the number of name/value pairs
    > you can send before they get truncated.[/color]

    the major drawback: You also loose the variables that where in the
    GET/POST request.
    [color=blue]
    > Q1: Is there an internal PHP server-side page redirection mechanism
    > that would/could provide a more efficient mechanism for switching
    > between PHP pages?
    >
    > Q2: Alternatively, is there a POST equivalent to header("Locatio n:")
    > that I could use from within PHP to redirect between pages, so that
    > the number of name/value pairs I could send is unlimited?[/color]

    What is wrong with require(_once), include(_once), readfile, eval
    functions?


    --

    Comment

    • Rob Tweed

      #3
      Re: Redirecting between PHP Pages

      Tim

      Thanks for the reply.

      See below...

      On 16 May 2004 13:01:56 GMT, Tim Van Wassenhove <euki@pi.be> wrote:
      [color=blue]
      >In article <0mhea0d2k2iohg kbcsd8akt5ojbgf 03pau@4ax.com>, Rob Tweed wrote:[color=green]
      >> Two inter-related questions:
      >>
      >> 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('Locatio n:
      >> someURL") ;
      >>
      >> This has two drawbacks - it seems inefficient to me - you essentially
      >> have to "bounce" a request/response off the browser to switch between
      >> PHP pages - leaving and returning to PHP just to switch pages, and
      >> also it uses the GET method and, I'd assume, therefore restricts you
      >> on the length of the URL and therefore the number of name/value pairs
      >> you can send before they get truncated.[/color]
      >
      >the major drawback: You also loose the variables that where in the
      >GET/POST request.[/color]

      Very true,
      [color=blue]
      >[color=green]
      >> Q1: Is there an internal PHP server-side page redirection mechanism
      >> that would/could provide a more efficient mechanism for switching
      >> between PHP pages?
      >>
      >> Q2: Alternatively, is there a POST equivalent to header("Locatio n:")
      >> that I could use from within PHP to redirect between pages, so that
      >> the number of name/value pairs I could send is unlimited?[/color]
      >
      >What is wrong with require(_once), include(_once), readfile, eval
      >functions?[/color]

      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?

      Many thanks

      Rob



      ---
      Rob Tweed
      M/Gateway Developments Ltd

      Global DOMination with eXtc : http://www.mgateway.tzo.com
      ---

      Comment

      • Tim Van Wassenhove

        #4
        Re: Redirecting between PHP Pages

        In article <dn5fa0hv2d0nq8 ges341fq07fj5ap vtdb1@4ax.com>, Rob Tweed wrote:[color=blue][color=green][color=darkred]
        >>> 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('Locatio n:
        >>> someURL") ;[/color][/color][/color]
        [color=blue][color=green]
        >>What is wrong with require(_once), include(_once), readfile, eval
        >>functions?[/color]
        >
        > 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?[/color]

        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 (isAuthenticate d()) {

        // lookup some stuff we would like to pass to the next page

        require_once('u ser_settings.ph p');

        } else {

        require_once('u ser_login.php') ;

        }
        ?>


        --
        Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>

        Comment

        • Rob Tweed

          #5
          Re: Redirecting between PHP Pages

          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("Locatio n: 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("Locatio n: 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:
          [color=blue]
          >In article <dn5fa0hv2d0nq8 ges341fq07fj5ap vtdb1@4ax.com>, Rob Tweed wrote:[color=green][color=darkred]
          >>>> 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('Locatio n:
          >>>> someURL") ;[/color][/color]
          >[color=green][color=darkred]
          >>>What is wrong with require(_once), include(_once), readfile, eval
          >>>functions?[/color]
          >>
          >> 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?[/color]
          >
          >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 (isAuthenticate d()) {
          >
          > // lookup some stuff we would like to pass to the next page
          >
          > require_once('u ser_settings.ph p');
          >
          >} else {
          >
          > require_once('u ser_login.php') ;
          >
          >}
          >?>[/color]

          ---
          Rob Tweed
          M/Gateway Developments Ltd

          Global DOMination with eXtc : http://www.mgateway.tzo.com
          ---

          Comment

          • Theo

            #6
            Re: Redirecting between PHP Pages

            Rob Tweed <rtweed@blueyon der.co.uk> wrote in
            news:u8dfa05td0 2fjqfbou93j1fqt 1p61ta7e1@4ax.c om:
            [color=blue]
            > What's the most common practice(s) for this scenario? Any other
            > thoughts/ideas?[/color]

            cgi scripts are used alot, or so it seems.

            Im no expert but there is no bad point to keeping pages simple. Load the
            form, go to the validation page, go to the success page... or go back to
            the form page. Otherwise you will have to embed all the html in print
            statements, instead of only where its needed. You could use specific
            includes, but that would defeat the purpose of using a single file anyways.

            Comment

            • Chung Leong

              #7
              Re: Redirecting between PHP Pages

              "Rob Tweed" <rtweed@blueyon der.co.uk> wrote in message
              news:0mhea0d2k2 iohgkbcsd8akt5o jbgf03pau@4ax.c om...[color=blue]
              > This has two drawbacks - it seems inefficient to me - you essentially
              > have to "bounce" a request/response off the browser to switch between
              > PHP pages - leaving and returning to PHP just to switch pages, and
              > also it uses the GET method and, I'd assume, therefore restricts you
              > on the length of the URL and therefore the number of name/value pairs
              > you can send before they get truncated.[/color]

              In a typical setup, where the request is done through a persistent HTTP
              connection, PHP is running as a module, and database connection is
              persistent, the overhead of a redirect isn't very large. There's of course
              the problem of passing variables to the other page. But then if it makes use
              of so many variable from the original page, then maybe it should be part of
              the same script.
              [color=blue]
              > Q1: Is there an internal PHP server-side page redirection mechanism
              > that would/could provide a more efficient mechanism for switching
              > between PHP pages?[/color]

              As Tim said, there's include() or require(). It's not a practice I would
              recommend though. Makes it harder to debug when you lose the 1 to 1
              correlation between URL and PHP script.
              [color=blue]
              > Q2: Alternatively, is there a POST equivalent to header("Locatio n:")
              > that I could use from within PHP to redirect between pages, so that
              > the number of name/value pairs I could send is unlimited?[/color]

              Yes. If you do

              header("HTTP/1.1 Temporary Redirect 307");
              header("Locatio n: /screw_rfc2616.p hp");

              then the browser will repost to the other page. Only works correctly in IE
              though.


              Comment

              • Tim Van Wassenhove

                #8
                Re: Redirecting between PHP Pages

                In article <u8dfa05td02fjq fbou93j1fqt1p61 ta7e1@4ax.com>, Rob Tweed wrote:[color=blue]
                > The question is, what's the best approach? The first two suffer from
                > the nuisance value and limitations of the redirection via the
                > header("Locatio n: 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.[/color]

                An extension of the include/require stuff:




                --
                Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>

                Comment

                • Savut

                  #9
                  Re: Redirecting between PHP Pages


                  "Rob Tweed" <rtweed@blueyon der.co.uk> wrote in message
                  news:0mhea0d2k2 iohgkbcsd8akt5o jbgf03pau@4ax.c om...[color=blue]
                  > Two inter-related questions:
                  >
                  > 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('Locatio n:
                  > someURL") ;
                  >
                  > This has two drawbacks - it seems inefficient to me - you essentially
                  > have to "bounce" a request/response off the browser to switch between
                  > PHP pages - leaving and returning to PHP just to switch pages, and
                  > also it uses the GET method and, I'd assume, therefore restricts you
                  > on the length of the URL and therefore the number of name/value pairs
                  > you can send before they get truncated.
                  >
                  > Q1: Is there an internal PHP server-side page redirection mechanism
                  > that would/could provide a more efficient mechanism for switching
                  > between PHP pages?
                  >[/color]

                  Yes and no, include() and require() is use for this kind of RESULT.
                  Switching between webpages ??? this is not logical as on the server-side,
                  the client can't see what happen with the process, so why make it
                  complicated like switching pages. It's just like making 2-3 additionnal
                  steps.
                  [color=blue]
                  > Q2: Alternatively, is there a POST equivalent to header("Locatio n:")
                  > that I could use from within PHP to redirect between pages, so that
                  > the number of name/value pairs I could send is unlimited?
                  >
                  >[/color]

                  you can use fopen, fsockopen on your own server, but again, include() and
                  require() is the logical solution.

                  Maybe you should reconsider your programming method, maybe posting some code
                  so we can make correction, as far as I see, you have this kind of problem
                  because you didn't use efficiently your include() with functions and maybe
                  you dont understand enough the HTTP protocol.

                  Savut
                  [color=blue]
                  >
                  > ---
                  > Rob Tweed
                  > M/Gateway Developments Ltd
                  >
                  > Global DOMination with eXtc : http://www.mgateway.tzo.com
                  > ---[/color]

                  Comment

                  • Rob Tweed

                    #10
                    Re: Redirecting between PHP Pages

                    On Mon, 17 May 2004 11:27:28 -0400, "Savut" <webki@hotmail. com> wrote:
                    [color=blue]
                    > maybe you dont understand enough the HTTP protocol.[/color]

                    Do me a favour - I've been involved in web applications since 1994. I
                    think I understand the HTTP protocol pretty thoroughly. Thanks for
                    your other comments however.


                    ---
                    Rob Tweed
                    M/Gateway Developments Ltd

                    Global DOMination with eXtc : http://www.mgateway.tzo.com
                    ---

                    Comment

                    • RootShell

                      #11
                      Re: Redirecting between PHP Pages


                      "Rob Tweed" <rtweed@blueyon der.co.uk> wrote in message
                      news:vnqha0tp9f 1t42rsn7cuueo9p sh1q3enll@4ax.c om...[color=blue]
                      > On Mon, 17 May 2004 11:27:28 -0400, "Savut" <webki@hotmail. com> wrote:
                      >[color=green]
                      > > maybe you dont understand enough the HTTP protocol.[/color]
                      >
                      > Do me a favour - I've been involved in web applications since 1994. I
                      > think I understand the HTTP protocol pretty thoroughly. Thanks for
                      > your other comments however.
                      >
                      >
                      > ---
                      > Rob Tweed
                      > M/Gateway Developments Ltd
                      >
                      > Global DOMination with eXtc : http://www.mgateway.tzo.com
                      > ---[/color]

                      Rob, for me that kind of answer is uncalled for, anyway, requesting help and
                      then using such strong replies get's you nowere.

                      expect to get what you provide? think about it.



                      Comment

                      • Rob Tweed

                        #12
                        Re: Redirecting between PHP Pages

                        Point taken and I apologise.

                        In my defence, I'd simply make the point that asking questions should
                        not be taken by others as an indication of ignorance of a subject.
                        More often than not it's actually an indication of a desire to gain
                        more depth in existing knowledge. The answers I received were
                        extremely helpful in that respect.

                        I shall try to bite my tongue in future :-)


                        On Tue, 18 May 2004 23:27:53 +0100, "RootShell" <RootShell@Netc abo.pt>
                        wrote:
                        [color=blue]
                        >
                        >"Rob Tweed" <rtweed@blueyon der.co.uk> wrote in message
                        >news:vnqha0tp9 f1t42rsn7cuueo9 psh1q3enll@4ax. com...[color=green]
                        >> On Mon, 17 May 2004 11:27:28 -0400, "Savut" <webki@hotmail. com> wrote:
                        >>[color=darkred]
                        >> > maybe you dont understand enough the HTTP protocol.[/color]
                        >>
                        >> Do me a favour - I've been involved in web applications since 1994. I
                        >> think I understand the HTTP protocol pretty thoroughly. Thanks for
                        >> your other comments however.
                        >>
                        >>
                        >> ---
                        >> Rob Tweed
                        >> M/Gateway Developments Ltd
                        >>
                        >> Global DOMination with eXtc : http://www.mgateway.tzo.com
                        >> ---[/color]
                        >
                        >Rob, for me that kind of answer is uncalled for, anyway, requesting help and
                        >then using such strong replies get's you nowere.
                        >
                        >expect to get what you provide? think about it.
                        >
                        >[/color]

                        ---
                        Rob Tweed
                        M/Gateway Developments Ltd

                        Global DOMination with eXtc : http://www.mgateway.tzo.com
                        ---

                        Comment

                        • Justin Wyer

                          #13
                          Re: Redirecting between PHP Pages

                          Rob Tweed wrote:
                          [color=blue]
                          > 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("Locatio n: 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("Locatio n: 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:
                          >
                          >[color=green]
                          >>In article <dn5fa0hv2d0nq8 ges341fq07fj5ap vtdb1@4ax.com>, Rob Tweed wrote:
                          >>[color=darkred]
                          >>>>>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('Locatio n:
                          >>>>>someURL" ) ;[/color]
                          >>[color=darkred]
                          >>>>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?[/color]
                          >>
                          >>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 (isAuthenticate d()) {
                          >>
                          >> // lookup some stuff we would like to pass to the next page
                          >>
                          >> require_once('u ser_settings.ph p');
                          >>
                          >>} else {
                          >>
                          >> require_once('u ser_login.php') ;
                          >>
                          >>}
                          >>?>[/color]
                          >
                          >
                          > ---
                          > Rob Tweed
                          > M/Gateway Developments Ltd
                          >
                          > Global DOMination with eXtc : http://www.mgateway.tzo.com
                          > ---[/color]

                          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.p hp" 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.

                          Comment

                          • Tim Van Wassenhove

                            #14
                            Re: Redirecting between PHP Pages

                            In article <c8fpok$61s$1@c tb-nnrp2.saix.net> , Justin Wyer wrote:[color=blue]
                            ><form action="index.p hp" method="post">[/color]

                            I prefer to use action="<?= $_SERVER['PHP_SELF'] ?>".

                            --
                            Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>

                            Comment

                            • Justin Wyer

                              #15
                              Re: Redirecting between PHP Pages

                              Tim Van Wassenhove wrote:[color=blue]
                              > In article <c8fpok$61s$1@c tb-nnrp2.saix.net> , Justin Wyer wrote:
                              >[color=green]
                              >><form action="index.p hp" method="post">[/color]
                              >
                              >
                              > I prefer to use action="<?= $_SERVER['PHP_SELF'] ?>".
                              >[/color]
                              Thats even better yeah, but I use smarty template for my html and then
                              i'd have to pass an extra varible to smarty :/

                              Comment

                              Working...