selfuser: PHP email form - headers already sent

Beitrag lesen

Hallo Martin,

Ersatzweise zeig uns den relevanten Quellcode.

So long

Ja, es ist tatsächlich etwas long!

mailform.php: (ich weiß nicht, was davon alles wichtig ist - also lasse ich es komplett)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">

<head>

<title>Email formulier</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style type="text/css">
<!--
html {font-size: 18px; font-family: Lucida Sans, sans-serif;}

body {background-color: transparent; color: gray; padding: 0px; margin: 0px; font-size: .7em;}

p {padding: 0px; margin: 5px; padding-bottom: 5px;}

a {color: navy; background-color: transparent;}

code {color: red;}

td, input, textarea, p {
	font-family: Georgia;
	font-size: 1.2em;
}

h3 {
	font-family: Georgia;
	font-size: 2.0em;
}

#container {
	width: 634px;
	height: 580px;
}
-->
</style>

</head>

<body>

<div id="container">

<?php

session_start();

// ENTER YOUR OWN EMAIL ADDRESS AS RECIPIENT.

$mail_ontv = "YOURNAME@YOURDOMAIN.COM";

// Check if email address is correct!!!

function checkmail($mail)
{
	$email_host = explode("@", $mail);
	$email_host = $email_host['1'];
	$email_resolved = gethostbyname($email_host);

	if ($email_resolved != $email_host && eregi("^[0-9a-z]([-_.~]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$mail))
		$valid = 1;

	return $valid;
} 

function display()
{

	// form + tabel 
	echo "<form method=\"post\" action=\"" . $_SERVER['PHP_SELF'] . "\">\n\n";
	echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n\n";

	// naam
	echo "<tr><td colspan=\"2\">Your name:</td></tr>\n\n";
	echo "<tr><td colspan=\"2\"><input type=\"text\" name=\"naam\" value=\"" . $_POST['naam'] . "\" tabindex=1  /></td></tr>\n\n";

	// space
	echo "<tr><td colspan=\"2\">&nbsp;</td></tr>\n\n";

	// mail
	echo "<tr><td colspan=\"2\">E-mail address:</td></tr>\n\n";
	echo "<tr><td colspan=\"2\"><input type=\"text\" name=\"mail\" value=\"" . $_POST['mail'] . "\" tabindex=2 /></td></tr>\n\n";

	// space
	echo "<tr><td colspan=\"2\">&nbsp;</td></tr>\n\n";

	// subject
	echo "<tr><td colspan=\"2\">Subject:</td></tr>\n\n";
	echo "<tr><td colspan=\"2\"><input type=\"text\" name=\"subject\" value=\"" . $_POST['subject'] . "\" tabindex=3 /></td></tr>\n\n";

	// space
	echo "<tr><td colspan=\"2\">&nbsp;</td></tr>\n\n";

    // Bericht
	echo "<tr><td colspan=\"2\">Message:</td></tr>\n\n";
	echo "<tr><td colspan=\"2\"><textarea name=\"msggs\" rows=\"6\" cols=\"45\" tabindex=4 >" . htmlentities($_POST['msggs']) . "</textarea></td></tr>\n\n";

	// space
	echo "<tr><td colspan=\"2\">&nbsp;</td></tr>\n\n";
	
   // Security
	echo "<tr><td>Security Code:</td>\n\n";
	echo "<td rowspan=\"2\" style=\"width: 280px\"><img src=\"button.php\" width=\"100\" height=\"40\" alt=\"captcha\" /></td></tr>\n\n";
	echo "<tr><td><input id=\"security_code\" name=\"security_code\" type=\"text\" tabindex=5 /></td></tr>\n\n";

	// space
	echo "<tr><td colspan=\"2\">&nbsp;</td></tr>\n\n";

	// button
	echo "<tr><td colspan=\"2\">&nbsp;</td></tr>\n\n";
	echo "<tr><td colspan=\"2\"><input type=\"submit\" name=\"submit\" value=\"Send\" tabindex=6 /></td></tr>\n\n";
      
	// sluit form + tabel
	echo "</table>\n\n";
	echo "</form>\n";

}

// als er niet op submit is gedrukt, of als er wel op is gedrukt maar niet alles ingevoerd is

if( isset($_POST['submit'])) {
   if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
   	
		if (!$_POST['naam'] || !$_POST['mail'] || !checkmail($_POST['mail']) || !$_POST['msggs'] || !$_POST['subject']) {
			display();
			echo "<p>You forgot to enter text in one of the fields.<br />";
			echo "Also, you may have misspelled your e-mail address.</p>";

		} else {

	// set datum
	$datum = date("d.m.Y H:i");

	// set ip
	$ip = $_SERVER['REMOTE_ADDR'];

	$inhoud_mail = "===================================================\n";
	$inhoud_mail .= "Email contact formulier www.YOURDOMAIN.com\n";
	$inhoud_mail .= "===================================================\n\n";

	$inhoud_mail .= "Name: " . $_POST['naam'] . "\n";
	$inhoud_mail .= "E-mail address: " . $_POST['mail'] . "\n";
	$inhoud_mail .= "Message: " . $_POST['msggs'] . "\n\n";

	$inhoud_mail .= "Sent on $datum via ip " . $ip . "\n\n";

	$inhoud_mail .= "===================================================\n\n";

    // --------------------
    // spambot protectie !!
    // --------------------

	$headers = "From: " . $_POST['naam'] . " <" . $_POST['mail'] . ">";

	$headers = stripslashes($headers);
	$headers = str_replace("\n", "", $headers); // Verwijder \n
	$headers = str_replace("\r", "", $headers); // Verwijder \r
	$headers = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $headers)); // Slashes van quotes

	$_POST['subject'] = str_replace("\n", "", $_POST['subject']); // Verwijder \n
	$_POST['subject'] = str_replace("\r", "", $_POST['subject']); // Verwijder \r
	$_POST['subject'] = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $_POST['subject'])); // Slashes van quotes

	mail($mail_ontv, $_POST['subject'], $inhoud_mail, $headers);
	echo "<h3>Message has been sent</h3>\n\n";
	echo "<p>Thank you for your message!</p>\n\n";
	echo "<p><a href=\"" . $_SERVER['PHP_SELF'] . "\" target=\"_self\" style=\"color: navy;\">Send another...</a></p>";
	}

	} else {
		display();
		echo "<p>The Security code is wrong</p>";
	}

} else {

	display();

}

?>

</div>

</body>

</html>

Ich habe jetzt extra nichts geändert, auch die Leerzeilen drin gelassen. Wie gesagt, es funktionierte perfekt. Es verwendet auch noch button.php:

<?php
session_start();

class createCaptcha {

	function generateCode($characters) {

		/* list all possible characters, similar looking characters and vowels have been removed */
		$possible = '23456789bcdfghjkmnpqrstvwxyz';
		$code = '';
		$i = 0;
		while ($i < $characters) { 
			$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
			$i++;
		}
		return $code;

	}

	function createCaptcha() {

		$code = $this->generateCode(5);
		$my_img = imagecreatefrompng('button.png');

		$background = imagecolorallocate( $my_img, 0, 0, 255 );
		$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );

		imagestring( $my_img, 4, 20, 3, $code, $text_colour );

		header( "Content-type: image/png" );

		imagepng($my_img);
		imagecolordeallocate($text_color);
		imagedestroy( $my_img );

		$_SESSION['security_code'] = $code;

	}

}

$captcha = new createCaptcha();

?>

Dazu gehört als letztes button.png (93 x 23 pixel, farbe: 444444)

Ich hoffe das hilft.