Using header("Location:...". How to pass over array variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Smithz

    #1

    Using header("Location:...". How to pass over array variable?

    Hi there,

    I have a php script that does some form input validation. As it verifies
    each field if the field has incorrect data, it appends an error message to
    an $error array. E.g.

    if (an_error = true) {
    $errors.="<tr>< td>You have note entered a surname. This is mandatory date
    of birth incorrectly.</td></tr>";
    }

    Previously I had always had the HTML error page code contained within the
    same script, but now I am moving it out to make maintenance easier.

    When validation completes there is now a statement that says
    if ($errors!="") {
    header("Locatio n: ./error_page.php" );
    }

    However, how can I pass the errors array to the error_page. I want to be
    able to list each error individually.

    Forgive me if this is a silly easy question. I have only been doing PHP for
    about a week.
    Thanks in advance.

    Kind regards

    Dave





  • Chris Hope

    #2
    Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?

    "Dave Smithz" <SPAM FREE WORLD> wrote:
    [color=blue]
    > I have a php script that does some form input validation. As it
    > verifies each field if the field has incorrect data, it appends an
    > error message to an $error array. E.g.
    >
    > if (an_error = true) {
    > $errors.="<tr>< td>You have note entered a surname. This is
    > mandatory date
    > of birth incorrectly.</td></tr>";
    > }[/color]

    This looks like a string not an array.
    [color=blue]
    > Previously I had always had the HTML error page code contained within
    > the same script, but now I am moving it out to make maintenance
    > easier.
    >
    > When validation completes there is now a statement that says
    > if ($errors!="") {
    > header("Locatio n: ./error_page.php" );
    > }
    >
    > However, how can I pass the errors array to the error_page. I want to
    > be able to list each error individually.
    >
    > Forgive me if this is a silly easy question. I have only been doing
    > PHP for about a week.
    > Thanks in advance.[/color]

    Assuming it is actually an array, you can loop through it along the
    lines of something like so:

    $vars = '?';
    for($i = 0; $i < sizeof($array); $i++) {
    $vars .= "array[]=" . urlencode($arra y[$i]) . '&';
    }

    Then the receiving page will have an array $_GET['array'] filled with
    elements.

    If it's an associative array you can do this:

    $vars = '?';
    foreach($array as $name => $value) {
    $vars .= $name . '=' . urlencode($valu e) . '&';
    }

    You can also use the serialize and unserialize functions to turn the
    array into a string which you would then use urlencode make it url
    safe.

    Functions in the manual online here:


    Generates a storable representation of a value

    Creates a PHP value from a stored representation



    --
    Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

    Comment

    • Dave Smithz

      #3
      Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?


      Thanks for that Chris. I did mean to put an array there.

      Thanks for the info there, much appreciated.

      Regards

      Dave


      Comment

      • micha

        #4
        Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?


        Dave Smithz wrote:[color=blue]
        > Hi there,
        >
        > I have a php script that does some form input validation. As it[/color]
        verifies[color=blue]
        > each field if the field has incorrect data, it appends an error[/color]
        message to[color=blue]
        > an $error array. E.g.
        >
        > if (an_error = true) {
        > $errors.="<tr>< td>You have note entered a surname. This is[/color]
        mandatory date[color=blue]
        > of birth incorrectly.</td></tr>";
        > }
        >
        > Previously I had always had the HTML error page code contained within[/color]
        the[color=blue]
        > same script, but now I am moving it out to make maintenance easier.
        >
        > When validation completes there is now a statement that says
        > if ($errors!="") {
        > header("Locatio n: ./error_page.php" );
        > }
        >[/color]

        header("Locatio n: ./error_page.php? NAME=VALUE"); could be a way[color=blue]
        > However, how can I pass the errors array to the error_page. I want to[/color]
        be[color=blue]
        > able to list each error individually.
        >
        > Forgive me if this is a silly easy question. I have only been doing[/color]
        PHP for[color=blue]
        > about a week.
        > Thanks in advance.
        >
        > Kind regards
        >
        > Dave[/color]

        Comment

        • LetsSurf

          #5
          Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?

          Wouldn't it be easier to just serialise the array and pass it as a
          value though get, then unseralise it.

          Or even just include the error_page.php and pass it as a global and
          then use the die() command to stop execution.

          Comment

          • Erwin Moller

            #6
            Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?

            micha wrote:
            [color=blue]
            > header("Locatio n: ./error_page.php? NAME=VALUE"); could be a way[/color]

            As I was told in this very group some weeks back (by you. :P) a
            locationheader needs an absolute URL.

            :-)

            Regards,
            Erwin Moller

            Comment

            • Anonymous

              #7
              Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?

              Erwin Moller wrote:[color=blue]
              >
              > micha wrote:
              >[color=green]
              > > header("Locatio n: ./error_page.php? NAME=VALUE"); could be a way[/color]
              >
              > As I was told in this very group some weeks back (by you. :P) a
              > locationheader needs an absolute URL.
              >
              > :-)[/color]

              No, it doesn't.

              Comment

              • Ewoud Dronkert

                #8
                Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?

                On Wed, 02 Feb 2005 14:42:57 +0100, Erwin Moller wrote:[color=blue]
                > locationheader needs an absolute URL.[/color]

                Depends on the client, but yes.

                To the OP: you could also use sessions. Put session_start() at the top
                of both scripts, set $_SESSION['err'] = "My error message." in the first
                one and echo $_SESSION['err'] in the second.


                --
                Firefox Web Browser - Rediscover the web - http://getffox.com/
                Thunderbird E-mail and Newsgroups - http://gettbird.com/

                Comment

                • Tim Van Wassenhove

                  #9
                  Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?

                  On 2005-02-02, Erwin Moller <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> wrote:[color=blue]
                  > micha wrote:
                  >[color=green]
                  >> header("Locatio n: ./error_page.php? NAME=VALUE"); could be a way[/color]
                  >
                  > As I was told in this very group some weeks back (by you. :P) a
                  > locationheader needs an absolute URL.[/color]

                  But now we have:



                  --
                  Met vriendelijke groeten,
                  Tim Van Wassenhove <http://www.timvw.info>

                  Comment

                  • Michael Fesser

                    #10
                    Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?

                    .oO(Anonymous)
                    [color=blue]
                    >Erwin Moller wrote:[color=green]
                    >>
                    >> As I was told in this very group some weeks back (by you. :P) a
                    >> locationheader needs an absolute URL.
                    >>[/color]
                    >No, it doesn't.[/color]

                    It does.

                    RFC 2616, section 14.30

                    Micha

                    Comment

                    • Chris Hope

                      #11
                      Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?

                      Michael Fesser wrote:
                      [color=blue][color=green][color=darkred]
                      >>> As I was told in this very group some weeks back (by you. :P) a
                      >>> locationheader needs an absolute URL.
                      >>>[/color]
                      >>No, it doesn't.[/color]
                      >
                      > It does.
                      >
                      > RFC 2616, section 14.30[/color]

                      And section 5 and the following subsections of RFC3986 deal with the
                      resolution of relative URIs. However, it was only published in January
                      2005 but I'm pretty sure all the browsers I have ever used since 1997
                      have supported relative URI redirection.

                      It is still probably best to have full URIs though, and it's easy enough
                      to do using the $_SERVER['HTTP_HOST'] variable in your location string.

                      --
                      Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

                      Comment

                      • Michael Fesser

                        #12
                        Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?

                        .oO(Chris Hope)
                        [color=blue]
                        >Michael Fesser wrote:
                        >[color=green]
                        >> RFC 2616, section 14.30[/color]
                        >
                        >And section 5 and the following subsections of RFC3986 deal with the
                        >resolution of relative URIs. However, it was only published in January
                        >2005 but I'm pretty sure all the browsers I have ever used since 1997
                        >have supported relative URI redirection.[/color]

                        The HTTP RFC clearly states that the Location URI has to be absolute.
                        IMHO a conforming user agent doesn't have to be able to resolve relative
                        URIs if he can expect an absolute one.
                        [color=blue]
                        >It is still probably best to have full URIs though, and it's easy enough
                        >to do using the $_SERVER['HTTP_HOST'] variable in your location string.[/color]

                        Agreed.

                        Micha

                        Comment

                        • John Dunlop

                          #13
                          Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?

                          Tim Van Wassenhove wrote:
                          [color=blue]
                          > [Erwin Moller wrote:][/color]
                          [color=blue][color=green]
                          > > micha wrote:[/color][/color]
                          [color=blue][color=green][color=darkred]
                          > > > header("Locatio n: ./error_page.php? NAME=VALUE")[/color]
                          > >
                          > > As I was told in this very group some weeks back (by you. :P)[/color][/color]

                          One of us is getting our Michas confused!
                          [color=blue][color=green]
                          > > a locationheader needs an absolute URL.[/color][/color]

                          Quite true here.
                          [color=blue]
                          > But now we have:
                          > http://www.ietf.org/rfc/rfc3986.txt[/color]

                          But that doesn't change RFC2616, which requires that HTTP
                          Location headers consist of a single absolute URI. HTTP/1.1
                          delegates the definition of 'absoluteURI' to RFC2396, now
                          obsoleted by RFC3986. By looking at Appendix D.2 of the new
                          RFC, you can see that the 'absoluteURI' rule from RFC2396 is
                          equivalent to the 'absolute-URI' rule of RFC3986. The only
                          real difference I spotted between the two is that now the
                          path component can be empty.




                          Mind you, that's all a bit irrelevant. The HTTP/1.1 errata
                          corrects the definition of Location by allowing a fragment
                          identifier, and provides a new ABNF rule:

                          Location = "Location" ":" absoluteURI [ "#" fragment ]

                          Grey Glacier is a family office focused on acquiring private manufacturing, distribution, and industrial services businesses between $1M - $10M EBITDA.


                          So, to sum up, under the new RFC3986 and by taking into
                          account the HTTP/1.1 errata, an HTTP Location header
                          effectively consists of a single *URI*:

                          URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

                          --
                          Jock

                          Comment

                          • Anonymous

                            #14
                            Re: Using header(&quot;Lo cation:...&quot ;. How to pass over array variable?

                            Michael Fesser wrote:[color=blue]
                            >
                            > .oO(Anonymous)
                            >[color=green]
                            > >Erwin Moller wrote:[color=darkred]
                            > >>
                            > >> As I was told in this very group some weeks back (by you. :P) a
                            > >> locationheader needs an absolute URL.
                            > >>[/color]
                            > >No, it doesn't.[/color]
                            >
                            > It does.
                            >
                            > RFC 2616, section 14.30[/color]

                            [readin it up]

                            Interesting. You are right, according to the RFC it does.

                            It also works relative, however.

                            Comment

                            Working...