Jump to content

is it possible to send multiple arrays through a header?


shadiadiph

Recommended Posts

Is it possible to send multiple arrays through a header in the url?

 

The code below is only sending one even though it send $error[$i]

 

$name  = strtolower($_POST["name"]);
$name = stripslashes(ucwords($name));
$email = strtolower($_POST["email"]);

$emailx ="/^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\\.[a-z]{2,4}$/";
$alphaspace ="/^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/";

if ($name=="") 
{
$error[] ="Name is a required field please complete and submit it again.";
}
elseif (preg_match($alphaspace, $name) ==false)
{                    
$error[] ="Please fill in a correct value for name numbers are not allowed.";
}
if ($email=="") 
{
$error[] ="Email is a required field please complete and submit it again.";
}
elseif (preg_match($emailx, $email) ==false) 
{
$error[] ="Please fill in a correct email address";
}
for ($i=0; $i<count($error);$i++)
{
header ("location: ../careers.php?error=$error[$i]");
exit;
}

why not set a session containing your errors. in some browsers there are limits on the querystring length.

 

my advice:

store the array in a session then on the receiving script loop through the session and echo out the errors.

simple answer "Yes"

 

long answer "Yes, but..."

 

and the but is:

If you send too many errors though the query string, they have a great possibility of being truncated. i guess it is also considered as a security flaw.

 

if you must do it that way (which i strongly advise against!) you do something like the following:

<?php

//$error = the array of errors

for($i = 0; $i < count($error); $i++){
$qs .= "error[]={$error[$i]]&";
}

header("Location: index.php?$qs");

?>

then loop through $_GET['error'] on the display page and display accordingly.

The way to send any array through the URL is to serialize it first:

<?php
if (isset($_GET['error'])) {
   echo '<pre>' . print_r(unserialize($_GET['error']),true) . '</pre>';
   exit;
}
$error[] ="Name is a required field please complete and submit it again.";
$error[] ="Please fill in a correct value for name numbers are not allowed.";
$error[] ="Email is a required field please complete and submit it again.";
$error[] ="Please fill in a correct email address";
header ('location: ?error=' . serialize($error));
?>

 

But, a much better way is to use a session:

<?php
session_start();
if (isset($_SESSION['error'])) {
   echo '<pre>' . print_r($_SESSION['error']),true) . '</pre>';
   exit;
}
$error[] ="Name is a required field please complete and submit it again.";
$error[] ="Please fill in a correct value for name numbers are not allowed.";
$error[] ="Email is a required field please complete and submit it again.";
$error[] ="Please fill in a correct email address";
$_SESSION['error'] = $error;
header ('location: ' . $_SERVER['PHP_SELF']);
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.