473,320 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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("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
13 11732
"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

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

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
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
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
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
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
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
.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
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
.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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stijn Goris | last post by:
Hi all, I have a question regarding the header function. I send a browser to a certain page (eg first.php ) wich sends no output to the browser. This page sends the browser to another page (eg...
11
by: Francisco Mendez | last post by:
I get the following message when trying to run my script: "Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/newcustomer.php:23) in...
6
by: bissatch | last post by:
Hi, I am trying to use the following function: header("Location: http://www.mysite.co.uk/home/update/index.php"); ....but getting the following error: Warning: Cannot modify header...
5
by: Duderino82 | last post by:
I'm working on a very simple peace of php where basically there is a form and 3 buttoms. One refreshed the page, one posts the form, and another one (since this form contains values of a record)...
1
by: hemingvej45 | last post by:
Session variables set before a header("Location: whatever") are not being written. And yes, before the header() call, I have session_write_close() but the result is the same. This problem is...
3
by: thomasg | last post by:
Hi, I am moving some scripts from a UNIX box to a Windows box. A data entry form calls a PHP script the updates the records from the form. After the script completes, The script redirects to...
4
by: andre rodier | last post by:
Hello, I need to display or propose a jpeg image on a web page. When I need to display the image, I use this code : header("Content-Length: $fileSize") ; header("Content-Type: $type") ;...
5
bilibytes
by: bilibytes | last post by:
hi, i am making a website with php OOP. i have a class called session: it has the attribues -logged_in; -user_name; -user_ip; -user_level;
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.