Jump to content

Error message passed through url not displayed correct


drayarms

Recommended Posts

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:


//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:


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

Link to comment
Share on other sites

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?

 

 

Isn't it because you're exploding the error string based on the space character?

 

<?php

$errors = explode(" ", $errors_string);

?>

Link to comment
Share on other sites

@cyberrobot.  So which character do you suggest I explode the string based on?

 

As xyph said, you would explode the string based on whatever's between the different error messages. Of course, if you're using the space character you should consider changing it to something else. Preferable something that's not commonly used in the error messages such as the straight bar "|". You could also use multiple characters such as ":|:".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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