Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Dave Smithz
Guest
 
Posts: n/a
#1: Jul 17 '05
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("Location: ./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
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Using header("Location:...". 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("Location: ./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($array[$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($value) . '&';
}

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:

http://www.php.net/urlencode
http://www.php.net/serialize
http://www.php.net/unserialize
http://www.php.net/foreach

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Dave Smithz
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Using header("Location:...". 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


micha
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Using header("Location:...". 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("Location: ./error_page.php");
> }
>[/color]

header("Location: ./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]

LetsSurf
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Using header("Location:...". 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.

Erwin Moller
Guest
 
Posts: n/a
#6: Jul 17 '05

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


micha wrote:
[color=blue]
> header("Location: ./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
Anonymous
Guest
 
Posts: n/a
#7: Jul 17 '05

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


Erwin Moller wrote:[color=blue]
>
> micha wrote:
>[color=green]
> > header("Location: ./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.
Ewoud Dronkert
Guest
 
Posts: n/a
#8: Jul 17 '05

re: Using header("Location:...". 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/
Tim Van Wassenhove
Guest
 
Posts: n/a
#9: Jul 17 '05

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


On 2005-02-02, Erwin Moller <since_humans_read_this_I_am_spammed_too_much@spam yourself.com> wrote:[color=blue]
> micha wrote:
>[color=green]
>> header("Location: ./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:
http://www.ietf.org/rfc/rfc3986.txt


--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Michael Fesser
Guest
 
Posts: n/a
#10: Jul 17 '05

re: Using header("Location:...". 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
Chris Hope
Guest
 
Posts: n/a
#11: Jul 17 '05

re: Using header("Location:...". 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/
Michael Fesser
Guest
 
Posts: n/a
#12: Jul 17 '05

re: Using header("Location:...". 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
John Dunlop
Guest
 
Posts: n/a
#13: Jul 17 '05

re: Using header("Location:...". 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("Location: ./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.

http://www.ietf.org/rfc/rfc2396.txt
http://www.ietf.org/rfc/rfc3986.txt

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 ]

http://skrb.org/ietf/http_errata.htm...tion-fragments

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
Anonymous
Guest
 
Posts: n/a
#14: Jul 17 '05

re: Using header("Location:...". 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.
Closed Thread