I have a small form at the top of each page that, after validation, is meant to redirect to a search page based on the input result. However, I find that it doesn't work on my local setup, yet works fine on the remote setup.

On my localhost, I get the error:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\rw-pw\includes\wrapper.php:13) in C:\xampp\htdocs\rw-pw\includes\wrapper.php on line 75

...which does not occur on the remote server.

		<div style="float:right;margin-right:5px;margin-top:5px;clear:right">
			<FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD="post" ENCTYPE="multipart/form-data" NAME="mlsSearch" TITLE="mlsSearch">
			<input name="mlsnum" type="hidden" value="<?php echo trim($_POST['mlsnum']); ?>" />
			<input name="PropertyType" type="hidden" value="<?php echo $PropertyType; ?>" />
			<INPUT NAME="mlsnum" TYPE="text" VALUE="" SIZE="10" MAXLENGTH="6" onfocus="style.borderColor='#000000';" onblur="style.borderColor='#cccccc';" /><input type="submit" name="mlssubmit" value="Find MLS#" /></form>
		</div>
				<?php
				if ($_POST['mlssubmit']) {

				$mlsnum = trim($_POST['mlsnum']);

				if (!Validate::isNotEmpty($mlsnum)) {
					$errmsg = "<span style=\"font-size:12px;font-weight:bold;color:red;\">Please enter an MLS number.</span>";
				}
				elseif(Validate::isInteger($mlsnum)) { //if data entered is all integers, do this
					$validlength = 6;
					if (strlen($mlsnum) != $validlength) {
						$errmsg = "<span style=\"font-size:12px;font-weight:bold;color:red;\">A valid MLS number is 6 digits.</span>";
					}
				} elseif(!Validate::isInteger($mlsnum)) {
					$errmsg = "<span style=\"font-size:12px;font-weight:bold;color:red;\">A valid MLS number is 6 digits, only numbers.</span>";
				}

				if ($errmsg) {	
					echo "<div style=\"float:right;padding-top:-10px;clear:right;\">".$errmsg."</div>";
				} else {

					$mlsnum = trim($_POST['mlsnum']);
					header("Location: http://".$_SERVER['HTTP_HOST']."/listingsDetail.php?mlsnum=".$mlsnum."");
				}

			}
			?>
</div>	

A bit more info: dev box is Windows latest XAMPP install (Apache 2.2.3, MySQL 5.0.24a, PHP 5.1.6) and remote/production box is php 4.4.4, Apache/1.3.37 (Unix), mysql 5.0.19. I'm not using any of the 'extras' special to php5 and apache2.2. Let it be known I'm about ready to toss out this new PC laptop and get a Mac; it's been driving me nuts to develop from Windows to Unix as I usualy develop on a Unix or Unix-based platform.

Having said that, I checked to ensure there are no spaces before and after the opening/closing PHP tags. I read somewhere that you should have no code following the header function; is there then another way I need to accomplish this?

Thanks,
Eve

    You already have the output ie. the form sent to the browser before the header is called.

    What you need to do is put all the code after it's checked for submission into a new php file and set that as the action for the form.

    form.php

    <div style="float:right;margin-right:5px;margin-top:5px;clear:right">
                <FORM ACTION="processing.php" METHOD="post" ENCTYPE="multipart/form-data" NAME="mlsSearch" TITLE="mlsSearch">
                <input name="mlsnum" type="hidden" value="<?php echo trim($_POST['mlsnum']); ?>" />
                <input name="PropertyType" type="hidden" value="<?php echo $PropertyType; ?>" />
                <INPUT NAME="mlsnum" TYPE="text" VALUE="" SIZE="10" MAXLENGTH="6" onfocus="style.borderColor='#000000';" onblur="style.borderColor='#cccccc';" /><input type="submit" name="mlssubmit" value="Find MLS#" /></form>
            </div> 
    

    processing.php

    $mlsnum = trim($_POST['mlsnum']);
    
                    if (!Validate::isNotEmpty($mlsnum)) {
                        $errmsg = "<span style=\"font-size:12px;font-weight:bold;color:red;\">Please enter an MLS number.</span>";
                    }
                    elseif(Validate::isInteger($mlsnum)) { //if data entered is all integers, do this
                        $validlength = 6;
                        if (strlen($mlsnum) != $validlength) {
                            $errmsg = "<span style=\"font-size:12px;font-weight:bold;color:red;\">A valid MLS number is 6 digits.</span>";
                        }
                    } elseif(!Validate::isInteger($mlsnum)) {
                        $errmsg = "<span style=\"font-size:12px;font-weight:bold;color:red;\">A valid MLS number is 6 digits, only numbers.</span>";
                    }
    
                    if ($errmsg) {    
                        echo "<div style=\"float:right;padding-top:-10px;clear:right;\">".$errmsg."</div>";
                    } else {
    
                        $mlsnum = trim($_POST['mlsnum']);
                        header("Location: http://".$_SERVER['HTTP_HOST']."/listingsDetail.php?mlsnum=".$mlsnum."");
                    } 
    

      Thanks. I'll do it that way. But how can I get any resulting error codes to appear on the referring page, rather than on processing.php?

      I'm curious however why this would work on the remote box, and not locally?

        Check the remote server's php.ini. It probably has "output_buffering" turned on.

          Checked my local php.ini flie and noted that output_buffering was set to Off. I turned on output_buffering on my local system, and also attempted to initialize at runtime. It still did not work. It also failed when setting to 5000. Apache was restarted each time; new browser instances brought up each time as well.

          I normally build my own installs of apache/php/mysql, but am strugging in a Windows environment, so opted for a pre-built package. Now I am beginning to wonder if installing xampp was a good idea.

            Personally I'd do all the error checking using javascript seeing as you're not going to need to use preg_match

              ClarkF1 wrote:

              Personally I'd do all the error checking using javascript seeing as you're not going to need to use preg_match

              And what happens if the user does not have JS available and enabled? What if a malicious user submits his own HTTP form posting totally bypassing the "real" form?

              JS can be nice enhancement to the user interface when it's available, but you still need to validate input on the server side just in case.

                I've turned off javascript once or twice to avoid client-side validation.

                Who the heck cares when my birthday is, or where I live. I mean, really?

                Beats lying 😉

                  So when I implement server-side validation, I use a class that does the validation but my error messages are output from process.php. (following ClarkF1's advice) Of course, the form does as it's told...it goes to process.php and spits out the error message.

                  How can I work this to get the error messages to appear of the submitted form - or, in some way, redirect the user back to the form if any error messages > 0? I would think if $errors > 0 then header("Location: " . $_SERVER['HTTP_REFERER'] ."") would work, does that sound right? But the error messages themselves are not returned on the original posted form.

                  (I realize this question has departed from the original title, but this problem was what originally prompted me to use header(Location:...) in the first place.)

                  And yeah...my fear of using JavaScript, while much easier, is the fact that users have more control over it and it would break things if JS were turned off.

                    Write a Reply...