Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Why is this form passing mystery character(s) resulting in an error?


[SOLVED] Why is this form passing mystery character(s) resulting in an error?

Reply
  #1 (permalink)  
Old 05-04-08, 09:46 AM
HighSierraCamp HighSierraCamp is offline
Newbie Coder
 
Join Date: Apr 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Why is this form passing mystery character(s) resulting in an error?

The Situation
I have an information request form on an html page. The form data is sent via the POST method to a php page for processing.

The Problem

I get an error for the variable $City, triggered by the fact that a character does not match one of the permitted characters. During testing, I did not input anything into the City input field. I can't figure out why my php script thinks it's getting an impermissible character.

Here is the html code for the form as it relates to the City input field:

HTML Code:
	 City: <input type=text name='City' size="30" maxlength="25" />
Here is the php code in the page that processes the form data. This section of code is the first time the $City variable appears in the code.

PHP Code:

$City stripslashes($City); /* This function strips the escape slashes inserted when Magic Quotes is turned on. See PHP pp 93-94 */
$City strip_tags($City); /* This function removes all HTML and PHP tags from the data input. See PHP p 96 */
$City trim($City); /* This function trims any extra spaces from both the beginning and end of a string, but not the middle. */
$City strtolower($City); /* Turns the data inputted in the City field first into lower case. See PHP p. 108 */
$City ucwords($City); /*  Capitalizes the first letter of each word in the City field. See PHP p. 108 */
if (!empty ($City)){ /* Checks to make sure the data entered into the City field consists of upper and lower case letters, an apostrophe, space or hyphen, and nothing else. */
    
if(!preg_match ("/^[A-Za-z0-9\' -]+$/",$City)){
        exit (
"<p>It appears that there may be a typo in your City name because the name entered contains characters not usually found in a name. Will you please click the back button on your browser to check your name and try again? Thanks!</p>");
    }

Interestingly, I have nearly identical code for the $Address variable, which immediately precedes the $City variable in both the html form and the php processing page, yet I get NO error from this script.

Here is the html code for the Address field:

HTML Code:
	<input type=text name='Address' size="55" maxlength="30" />
And the php code relating to $Address, which comes just BEFORE the $City variable:

PHP Code:

$Address stripslashes($Address); /* This function strips the escape slashes inserted when Magic Quotes is turned on. See PHP pp 93-94 */
$Address strip_tags($Address); /* This function removes all HTML and PHP tags from the data input. See PHP p 96 */
$Address trim($Address); /* This function trims any extra spaces from both the beginning and end of a string, but not the middle. */
$Address strtolower($Address); /* Turns the data inputted in the Address field first into lower case. See PHP p. 108 */
$Address ucwords($Address); /*  Capitalizes the first letter of each word in the Address field. See PHP p. 108 */
if (!empty ($Address)){ /* Checks to make sure the data entered into the Address field consists of numbers, a period, parentheses, plus, abrevisations for extension, spaces or hyphen, and nothing else. */
    
if(!preg_match ("/^[a-zA-Z0-9\,# -]+$/",$Address)){
        exit (
"<p>The address you entered does not appear to be in a valid format. Will you please click the back button on your browser to check your name and try again? Thanks!</p>");
    }

I first noticed this error after I fixed a bug in my php code relating to conditionals in the php processing page, but that code appears AFTER the $City variable code.

Any ideas?

(Thanks ahead of time for your help!)
__________________
HighSierraCamp.com

Last edited by HighSierraCamp; 05-04-08 at 09:55 AM. Reason: Additional information for consideration.
Reply With Quote
  #2 (permalink)  
Old 05-04-08, 09:52 AM
blinn_shade's Avatar
blinn_shade blinn_shade is offline
Aspiring Coder
 
Join Date: Aug 2007
Posts: 540
Thanks: 0
Thanked 0 Times in 0 Posts
I would turn register_globals off.
__________________
Can you think outside the box but remain inside the box?
Reply With Quote
  #3 (permalink)  
Old 05-04-08, 09:56 AM
HighSierraCamp HighSierraCamp is offline
Newbie Coder
 
Join Date: Apr 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
After checking my error log, I found this:

Premature end of script headers
__________________
HighSierraCamp.com
Reply With Quote
  #4 (permalink)  
Old 05-04-08, 09:57 AM
HighSierraCamp HighSierraCamp is offline
Newbie Coder
 
Join Date: Apr 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the tip. However, if I turn off register_globals I'll lose functionality for other scripts.
__________________
HighSierraCamp.com
Reply With Quote
  #5 (permalink)  
Old 05-04-08, 10:23 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Without seeing your whole actual code, it is highly likely you either have some logic that is setting $City, either directly, for example if($City = '') instead of if($City == '') or it is being set due to register globals and some other get/cookie variable with the same name.

You would need to post your code to get help with what it is doing.

As a side note: Register globals have been completely eliminated in php6. They were turned off in php4.2 in the year 2002 because they are a huge security hole (they allow session variables, which should be safe from tampering, to be set from outside of your code by someone simply sending a post/get/cookie variable with the same name as a session variable.) No new code, new books, new tutorials, new hosting accounts should have been created after that point in time that used register globals. You WILL need to make your code work without relying on register globals. It is best to start now.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #6 (permalink)  
Old 05-04-08, 10:42 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
The reason for the Premature end of script headers error is because you script is ending without causing a whole valid web page to be output - http://httpd.apache.org/docs/1.3/mis...script-headers

If this happens to be php5.2.4, they messed up some end of script cleanup code that fails to flush the output buffering when a script ends without reaching the end of the file.

The error is a side effect of how your script is exiting when it detects an error and is not the cause of or is directly caused by the problem with the $City variable.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 05-04-08 at 10:46 AM. Reason: fixed wording
Reply With Quote
  #7 (permalink)  
Old 05-04-08, 01:47 PM
HighSierraCamp HighSierraCamp is offline
Newbie Coder
 
Join Date: Apr 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
I had PHP 5.2.5 and turning off register_globals cured both the mystery character being passed and the server error of "Premature end of script headers".

Thanks very much!
__________________
HighSierraCamp.com
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ and MSSQL tutorials? scott2500uk C/C++ 8 05-11-09 02:33 AM
I cannot find what i messed up. Parse error: syntax error, unexpected T_CONSTANT_ENCA bilicek.com PHP 3 01-31-08 04:54 PM
very simple Form error handling, please help macintosh PHP 3 04-19-07 08:02 PM
ASP upload prob minority ASP 1 06-27-05 08:35 AM
formmail problem gscraper Perl 12 08-27-04 03:06 AM


All times are GMT -5. The time now is 07:43 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.