By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,925 Members | 1,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,925 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jul 17 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
"Dave Smithz" <SPAM FREE WORLD> wrote:
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>";
}
This looks like a string not an array.
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.


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/
Jul 17 '05 #2

P: n/a

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

Thanks for the info there, much appreciated.

Regards

Dave
Jul 17 '05 #3

P: n/a

Dave Smithz wrote:
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");
}

header("Location: ./error_page.php?NAME=VALUE"); could be a way 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


Jul 17 '05 #4

P: n/a
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.

Jul 17 '05 #5

P: n/a
micha wrote:
header("Location: ./error_page.php?NAME=VALUE"); could be a way


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

:-)

Regards,
Erwin Moller
Jul 17 '05 #6

P: n/a
Erwin Moller wrote:

micha wrote:
header("Location: ./error_page.php?NAME=VALUE"); could be a way


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

:-)


No, it doesn't.
Jul 17 '05 #7

P: n/a
On Wed, 02 Feb 2005 14:42:57 +0100, Erwin Moller wrote:
locationheader needs an absolute URL.


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/
Jul 17 '05 #8

P: n/a
On 2005-02-02, Erwin Moller <si******************************************@spam yourself.com> wrote:
micha wrote:
header("Location: ./error_page.php?NAME=VALUE"); could be a way


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


But now we have:
http://www.ietf.org/rfc/rfc3986.txt
--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #9

P: n/a
.oO(Anonymous)
Erwin Moller wrote:

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

No, it doesn't.


It does.

RFC 2616, section 14.30

Micha
Jul 17 '05 #10

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

No, it doesn't.


It does.

RFC 2616, section 14.30


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/
Jul 17 '05 #11

P: n/a
.oO(Chris Hope)
Michael Fesser wrote:
RFC 2616, section 14.30
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.


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.
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.


Agreed.

Micha
Jul 17 '05 #12

P: n/a
Tim Van Wassenhove wrote:
[Erwin Moller wrote:]
micha wrote:
header("Location: ./error_page.php?NAME=VALUE")


As I was told in this very group some weeks back (by you. :P)
One of us is getting our Michas confused!
a locationheader needs an absolute URL.


Quite true here.
But now we have:
http://www.ietf.org/rfc/rfc3986.txt


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
Jul 17 '05 #13

P: n/a
Michael Fesser wrote:

.oO(Anonymous)
Erwin Moller wrote:

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

No, it doesn't.


It does.

RFC 2616, section 14.30


[readin it up]

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

It also works relative, however.
Jul 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.