Page 1 of 1

Header location, issue. small piece of code, can anyone take

Posted: Thu Sep 22, 2011 9:54 pm
by danjapro
My Error Output is this:
Warning: Cannot modify header information - headers already sent by (output started at city-search.php:1) in city-search.php on line 65

What I'm I missing in the code, I can't seem to get these. It works excellent in Localhost, but in production. Error above.

Code: Select all

<?php
// MG CREATED THIS SIMPLE REGISTRATION PHP FOR THAT GOES TO ONE SPECIFIC USER.
// WILL TEST FORM REGSITRATION NG 07-2011
//If the form is submitted
if(isset($_POST['submit'])) {

	//Check to make sure that the name field is not empty
	if(trim($_POST['name']) == '') {
		$hasError = true;
	} else {
		$name = trim($_POST['name']);
	}

	//Check to make sure that the subject field is not empty
	if(trim($_POST['subject']) == '') {
		$hasError = true;
	} else {
		$subject = trim($_POST['subject']);
	}

	//Check to make sure sure that a valid email address is submitted
	if(trim($_POST['email']) == '')  {
		$hasError = true;
	} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
		$hasError = true;
	} else {
		$email = trim($_POST['email']);
	}
	//Check to make sure comments were entered
	if(trim($_POST['message']) == '') {
		//$hasError = true;
		$comments == 'Have No Friends';
	} else {
		if(function_exists('stripslashes')) {
			$comments = stripslashes(trim($_POST['message']));
		} else {
			$comments = trim($_POST['message']);
		}
	}

	//If there is no error, send the email
	if(!isset($hasError)) { 
		//$emailTo = 'regina@shushmedeals.com';
		//$emailTo = 'reginabyrd32@yahoo.com';
		$emailTo = 'mp3@danjaproduction.com'; 		
		//$emailBcc = 'ganja99@netzero.net';
		//$emailBcc = 'info@danjaproduction.com';  
		//$emailBcc = 'ganja99@netzero.net';    
		//Put your own email address here
		$body = "Location: $name \n\nEmail: $email \n\nShushMeDeals Sign-up Info:\n $comments";
	
		// To send HTML mail, the Content-type header must be set
	
		$headers  = 'From: ShushMeDeals.com City Location  sign-up - '.$name.'' . "\r\n" . 'Reply-To: ' . $email . "\r\n" .'Bcc: print@extremeatlanta.com'; 
		//$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
        //$headers .= 'Bcc: info@danjaproduction.com' . "\r\n";

		mail($emailTo, $subject, $body, $headers);
		$emailSent = true;
		if($emailSent = true) {
			setcookie("location", $name);

			//echo '<script type="text/javascript">
           //       window.location = "index.php?option=com_enmasse&controller=deal&task=today&locationName='.$name.'"
           //      </script>';
		}
	}
}
?>   

:?:

Re: Header location, issue. small piece of code, can anyone

Posted: Fri Sep 23, 2011 12:19 am
by twinedev
Well, since if I take the code you give, and paste it into an editor, and line 65 does nothing with headers, but line 61 does, my guess would be that you are not opening your PHP tag at the first character of the first line of the file, especially since it tells you that you sent output on line 1 (Remember, even a blank line outside of <?php ?> tags is output.

-Greg

PS. It is probably a good idea to get rid of (client?) e-mails before you post code to a public forum

PPS. yeah, I'm a snoop.... May want to let the person with the second e-mail address know that if you google her e-mail, one of the results you get is another web development company that let their log of people using their contact form get indexed by google.... (and being the nice guy I am, I was gonna contact them to let them know, but according to their "About Page", they closed business back in March)

Re: Header location, issue. small piece of code, can anyone

Posted: Fri Sep 23, 2011 2:24 am
by danjapro
Nuff thanks for reminder with info in php script, and also the insight with the client info.
Appreciate your assistance max.

Re: Header location, issue. small piece of code, can anyone

Posted: Fri Sep 23, 2011 2:39 am
by danjapro
Tried fixing space at line 1, staill same error.
What I'm I missing here, can anyone take look and give some guidance.

Thank you guys...

Re: Header location, issue. small piece of code, can anyone

Posted: Fri Sep 23, 2011 3:03 am
by twinedev
If this script had no space or blank lines before the <?php tag then I'm not sure what it is. My thought was that maybe the code you gave was something included by city-search.php and that had the space, but just double checked the error, and see they were both the same file (where output was started and the line tripping the error).

Any chance there is an auto-prepend file? I would think that would list the actual file prepended, but I'm stumped at this point without actually being able to see the code on the server. Any chance you can save a copy on the server as city-search.php.txt and then PM me the URL to it?

-Greg

Re: Header location, issue. small piece of code, can anyone

Posted: Sat Sep 24, 2011 4:14 am
by twinedev
BOY OH BOY! This was a fun one.

Ok, it boiled down to something simple: You have 3 characters at the beginning of your file that do not show up in your editor, or when you do a view source. See below, which is from a dump of when I browse to the .txt version you set up for me using Fiddler. These are same three characters I get browsing to the php version. (Note, that when I paste it here, they show up as just question marks. in HEX they are EF BB BF):
[text]HTTP/1.1 200 OK
Date: Sat, 24 Sep 2011 08:17:32 GMT
Server: Apache
Last-Modified: Sat, 24 Sep 2011 04:29:58 GMT
ETag: "17fa758-38bd-4ada860e6a3ec"
Accept-Ranges: bytes
Content-Length: 14525
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/plain

???<?php if(isset($_POST['submit'])) {
^^^[/text]

Now, I looked it up, and those three byes are (just going from what I'm reading, hey I'm learning here too LOL) the Byte Order Mark to indicate the file is UTF-8. What program did you use to first create and edit this file? According to some sites I saw, Windows Notepad will include these automatically, so will some others. You need to open in a different program such as Notepad++ (which will also assist in coding BIG time and it is FREE - http://notepad-plus-plus.org/ ) and when you have the file open, in the menu under "Encoding" chose "Convert to UTF-8 Without BOM" then save the file back out.

I just tested this, I took the code, which was working 100% fine on a copy I placed on my server. I copied all the text, opened up Windows Notepad, pasted the code in there, saved it at as UTF-8 format as test2.php, uploaded to my server, and BOOM! got the same exact problem you are getting! I opened it in NotePad++, did the above step, re-uploaded and now the problem is again gone!

Learn something new every day I tell ya! Took me a while to track it down but was able to as that was a heck of a puzzle if you didn't know about it already since you can't easily see those characters!

Also, as notes as from the "Coding Critiquer" in me: (and I do realize this is probably a work in progress, but just wanted to list them anyhow in case you were not aware)

1. You have a line with if($emailSent = true) { this will ALWAYS evaluate as TRUE, as you are assigning (=) the value true to $emaillSent, where you are wanting to compare (==) them using: if($emailSent == true) { (of course at this point, it will still always be true since you are setting it directly above, but you should change this to be proper code.)

2. You have a block of code that should be in the <head> section which is now before the <!DOCTYPE . I usually place the <!DOCTYPE right on the same line following the closing ?> to make sure it is the first character sent to the browser.

3. You do not have closing </body> and </html> tags.

4. Need to run the page through a validator (http://validator.w3.org/) again, I realize you may not be done with the page yet, but just a reminder.

Well good luck with this. Also, any other files you have created/saved using whatever you used to save this one, you will also probably want to resave though something like Notepad++

-Greg