Error message passed through url not displayed correct

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:
[php]
//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");

    
}

[/php]

And here is the code for the target page:[code]

				<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-->[/code]

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.

Hi,

change the delimiter for explode and implode.
ex: explode(",", $error); AND implode(",", $message);

i hope this is you answer.

sarthakpatel

Your awesome :smiley:

I use error messages via URL’s and havent had any issues.
I know its different to what you use. Just thought I would share

[php]function getNotice($code) {
$exc1000 = ‘You have created your maximum number of pages!’;
$exc1001 = ‘Email addresses does not match!’;
$exc1002 = ‘Wrong username!’;
$exc1003 = ‘Incorrect password’;
$exc1004 = ‘Internal error: Could not create user!’;
$exc1005 = ‘User not found’;
$exc1006 = ‘Failed setting password’;
$exc1007 = “Failed changing password”;
$exc1008 = “Security level denied!”;
$exc1011 = ‘Invalid email address’;
$exc1012 = ‘Email already in use’;
$exc1013 = ‘The password must contain 6 characters or more’;
$exc1014 = ‘Passwords does not match’;
$exc1015 = ‘Username cannot contain spaces’;
$exc1016 = ‘Username must contain letters (A-Z)’;
$exc1018 = ‘Username must be between 3 and 12 characters!’;
$exc1020 = ‘Username is reserved!’;
$exc1021 = ‘Username already exists’;
$exc1022 = ‘You must write a username and password’;
$exc1023 = ‘Failed to create cookie!’;
$exc1024 = ‘You must select a level and a username!’;
$exc1025 = “You have been logged out!”;
$exc1026 = ‘Internal Error: Failed to create level!’;
$exc1027 = ‘You must fill out all required fields!’;

#Positive messages
$exc0 = ‘Everything is fine’;
$exc1028 = ‘The Staff Member has been successfully deleted!’;
$exc1029 = ‘The Staff Member has not been deleted!’;
$exc1030 = ‘The Staff Member has been edited!’;
$exc1031 = ‘The Staff Member has been added to database!’;
$exc1032 = ‘Your Addon has been successfully created!’;
$exc1033 = ‘Your Addon has been successfully deleted!’;
$exc1034 = ‘Your Addon has been successfully edited!’;

$message = ‘exc’.$code;
return “${$message}”;
}[/php]

Then this displays it on the page.
[php]<?php if(isset($_GET['notice'])){ echo getNotice($_GET['notice']); }?>[/php]

@Sarthakpatel. Is it possible to make the delimeter for implode/explode a line break? If so how?

@Sarthakpatel. That worked. Good looking out man! :slight_smile:


or \n as a delimeter for explode doesn’t work.

But of course, a solution does exist! :slight_smile:

Try this :

EXAMPLE

$string = str_replace("\n",";",$_REQUEST[‘my_string’]);
$my_array = explode(’;’,$string);

:slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service