Error message passed through url not displayed correct

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

Error message passed through url not displayed correct

Post by drayarms »

I'm trying to display error messages on the same page that submits a form (register_page.php). So in the script that executes the form submission, I create an array to hold all the error messages, and towards the end of the script I include a conditional at the end, which does the following if there are errors: First I convert the $errors array into a string, url encode it, then pass it into the url of the target page (register_page.php). Then on the target page, I retrieve the message through GET, url decode it, reconvert it to an array and try to print it out the contents of the array. So here is the excerpt of the conditional on the execution page:

Code: Select all

//Display error messages.


} else {// if errors array is not empty

	//Conver the errors array into a string
	
	$errors_string = implode(" ", $errors);

	//Encode the imploded string.
	
	$message = urlencode($errors_string);


	header("Location: register_page.php?message=$message");

        
    }


And here is the code for the target page:

Code: Select all

</div> <!--closes register box-->

					
					<div id="error">


					<?php   if(isset($_GET['message'])) {

						//Decode the url
						
						$errors_string = urldecode($_GET['message']);

						//Explode the decoded errors string back to an array.
                			
						$errors = explode(" ", $errors_string);

						echo '<h3>Error!</h3>
        					The following error(s) occured:<br />';

       
        					foreach ($errors as $msg) {
            					echo "$msg<br/>\n";
        					}


					  }

					?>

					</div> <!--closes error-->

Now everything works just fine, except that the error messages are printed one word per line for example
Your
email
is
not
a
valid
email
address.

I can't seem to find the fluke in the code. Any ideas? Also is there any potential security risk for passing error messages in a url? No sensitive information is being transmitted. Just error messages.
amargharat
Forum Commoner
Posts: 82
Joined: Wed Sep 16, 2009 2:43 am
Location: Mumbai, India
Contact:

Re: Error message passed through url not displayed correct

Post by amargharat »

First code:

Code: Select all

} else {// if errors array is not empty



        $message = serialize($errors);





        header("Location: register_page.php?message=$message");



       

    }

Second Code:

Code: Select all

<?php   if(isset($_GET['message'])) {



												$errors = unserialize($_GET['message']);



                                                echo '<h3>Error!</h3>

                                                The following error(s) occured:<br />';



       

                                                foreach ($errors as $msg) {

                                                echo "$msg<br/>\n";

                                                }





                                          }



                                        ?>
Post Reply