shadiadiph Posted February 23, 2009 Share Posted February 23, 2009 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; } Link to comment https://forums.phpfreaks.com/topic/146477-is-it-possible-to-send-multiple-arrays-through-a-header/ Share on other sites More sharing options...
wrathican Posted February 23, 2009 Share Posted February 23, 2009 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. Link to comment https://forums.phpfreaks.com/topic/146477-is-it-possible-to-send-multiple-arrays-through-a-header/#findComment-769081 Share on other sites More sharing options...
shadiadiph Posted February 23, 2009 Author Share Posted February 23, 2009 I know I can give the two error messages a different id and post them through a normal header url page.php?eid=$emailerror&nid=$nameerror then use get on the next page I am just asking if it is possible or not? Link to comment https://forums.phpfreaks.com/topic/146477-is-it-possible-to-send-multiple-arrays-through-a-header/#findComment-769122 Share on other sites More sharing options...
wrathican Posted February 23, 2009 Share Posted February 23, 2009 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. Link to comment https://forums.phpfreaks.com/topic/146477-is-it-possible-to-send-multiple-arrays-through-a-header/#findComment-769133 Share on other sites More sharing options...
kenrbnsn Posted February 23, 2009 Share Posted February 23, 2009 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 Link to comment https://forums.phpfreaks.com/topic/146477-is-it-possible-to-send-multiple-arrays-through-a-header/#findComment-769153 Share on other sites More sharing options...
shadiadiph Posted February 23, 2009 Author Share Posted February 23, 2009 Ok thanks guys I shall try that tomorrow going to get some sleep and start fresh in the morning. Link to comment https://forums.phpfreaks.com/topic/146477-is-it-possible-to-send-multiple-arrays-through-a-header/#findComment-769200 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.