Ahnungsloser: Captcha Script in Sessionverwaltung einbauen

Beitrag lesen

Das File zum Auswerten Kontakt_ausw.php

/***********************************************************************/  
// Session starten und confog.php includen  
	session_start();  
	include ("config.php");  
	  
	// CaptchaCodes abfragen  
	$CAPTCHA_RandomText = "";  
	if (isset($_POST['txtCode'])){  
	$CAPTCHA_EnteredText = str_replace("<","",str_replace(">","",str_replace("'","",str_replace("[","",str_replace("]","",$_POST['txtCode'])))));  
	}  
	if (isset($_SESSION['CAPTCHA_RndText'])) {  
	$CAPTCHA_RandomText = $_SESSION['CAPTCHA_RndText'];  
	}  
  
	// Eingabefelder abfragen  
	$_SESSION['name'] = $_POST['name'];  
	$_SESSION['email'] = $_POST['email'];  
	$_SESSION['betreff'] = $_POST['betreff'];  
	$_SESSION['nachricht'] = $_POST['nachricht'];  
	  
	$email_i = $_SESSION['email'];  
	  
	// Email Funktion  
	function pruefe_mail($email_i) {  
		  if(strstr($email_i, "@")) {  
			$email_i = explode ("@", $email_i);  
			if(strstr($email_i[1], ".")) $ok = TRUE;  
		  }  
		  return $ok;  
		}  
	  
	// Eingaben prüfen  
	$fehler = "";  
	if(!pruefe_mail($email_i) && !empty($email_i)) {  
			$fehler .= "<li>Email fehlerhaft!</li>";  
			}  
			if ($_SESSION['name'] == ""){  
			$fehler .= "<li>Name fehlt!</li>";  
			}  
			if ($_SESSION['email'] == ""){  
			$fehler .= "<li>Email fehlt!</li>";  
			}  
			if ($_SESSION['betreff'] == ""){  
			$fehler .= "<li>Betreff fehlt!</li>";  
			}  
			if ($_SESSION['nachricht'] == ""){  
			$fehler .= "<li>Nachricht fehlt!</li>";  
			}  
			if ($CAPTCHA_EnteredText == $CAPTCHA_RandomText and isset($_POST['txtCode']) == true and isset($_SESSION['CAPTCHA_RndText'])){  
			$captcha = true;  
			} else {  
			$fehler .= "<li>Captcha fehlt oder fehlerhaft!</li>";  
			}  
	echo '<div style="border: 1px #000 solid; width: 350px; padding: 5px;">';		  
	if ($fehler == ""){  
	// Email zumsammensetzen  
	$email = "From: " . $_SESSION['email'];  
	$nachrichtfertig = $_SESSION['name']. " schrieb: \n\n" . $_SESSION['nachricht'];  
	$versand = mail($empfaenger, $_SESSION['betreff'], $nachrichtfertig, $email);  
			if ($versand) {  
			echo '<h3>Kontaktformular</h3>  
			<p>Email ist erfolgreich versendet worden!</p>';  
			  
			// Sessionvariablen löschen  
			unset($_SESSION['name']);  
			unset($_SESSION['email']);  
			unset($_SESSION['betreff']);  
			unset($_SESSION['nachricht']);  
			}  
			  
	} else {  
	echo '<h3>Kontaktformular</h3>';  
	echo $fehler;  
	echo '<p><a href="kontakt.php">zurück</a></p>';  
	}  
	echo '</div>';	  
  
	// Session unset  
	unset($_SESSION['CAPTCHA_RndText']);

und dann noch das File zum bauen des Captcha IMG

	// Alle Fehler und Notices anzeigen  
	error_reporting(E_ALL);  
  
/***************************************************************************  
  
 ***************************************************************************/  
  
	// Session initialisieren  
	session_start();  
	  
	include("config.php");  
	  
	$Schriftart = "captcha_files/truetype_$Schrift.ttf";  
  
	// Benötige Strings überprüfen  
	if (isset($_SESSION["CAPTCHA_RndText"])) {  
	$CAPTCHA_RandomText = $_SESSION["CAPTCHA_RndText"];  
	} else {  
		// Fehlermeldung ausgeben  
		header ("Content-type: image/png");  
		$im = @imagecreatetruecolor(300, 30)  
			or die("Cannot Initialize new GD image stream");  
		$text_color = imagecolorallocate($im, 233, 14, 91);  
		imagestring($im, 10, 5, 7, "Kein Zufallscode übergeben!", $text_color);  
		imagepng($im);  
		imagedestroy($im);  
	exit();  
	}  
  
// Auf Funktion überprüfen  
if ($imagettftext == "1") {  
	  
	// Benötige Files überprüfen  
	if (!file_exists($Schriftart)) {  
		// Fehlermeldung ausgeben  
//		header ("Content-type: image/png");  
		$im = @imagecreatetruecolor(300, 30)  
			or die("Cannot Initialize new GD image stream");  
		$text_color = imagecolorallocate($im, 233, 14, 91);  
		imagestring($im, 10, 5, 7, "Schriftart nicht gefunden!", $text_color);  
		imagepng($im);  
		imagedestroy($im);  
	exit();  
	}  
	  
	if (!file_exists($bg1) OR !file_exists($bg2)) {  
		// Fehlermeldung ausgeben  
		header ("Content-type: image/png");  
		$im = @imagecreatetruecolor(300, 30)  
			or die("Cannot Initialize new GD image stream");  
		$text_color = imagecolorallocate($im, 233, 14, 91);  
		imagestring($im, 10, 5, 7, "Hintergrundbild nicht gefunden!", $text_color);  
		imagepng($im);  
		imagedestroy($im);  
	exit();  
	}  
	  
	if (!file_exists($ov1) OR !file_exists($ov2)) {  
		// Fehlermeldung ausgeben  
		header ("Content-type: image/png");  
		$im = @imagecreatetruecolor(300, 30)  
			or die("Cannot Initialize new GD image stream");  
		$text_color = imagecolorallocate($im, 233, 14, 91);  
		imagestring($im, 10, 5, 7, "Overlaybild nicht gefunden!", $text_color);  
		imagepng($im);  
		imagedestroy($im);  
	exit();  
	}  
	  
  
	// Zufallshintergrund  
	mt_srand((double)microtime()*1000000);  
	$Bild1 = mt_rand(1, 2);  
		switch ($Bild1) {  
	case 1:  
	$Grafik=imagecreatefrompng($bg1);  
	$Grafikhilf=imagecreatefrompng($bg1);  
		break 1;  
	case 2:  
	$Grafik=imagecreatefrompng($bg2);  
	$Grafikhilf=imagecreatefrompng($bg2);  
		break 1;  
		}  
  
	// Zufallsgrafik2  
	mt_srand((double)microtime()*1000000);  
	$Bild2 = mt_rand(1, 2);  
		switch ($Bild2) {  
	case 1:  
	$Grafik2=imagecreatefrompng($ov1);  
		break 1;  
	case 2:  
	$Grafik2=imagecreatefrompng($ov2);  
		break 1;  
		}	  
  
	// Textfarben ins Array speichern  
	$textfarbe = array(  
		imagecolorallocate($Grafik, 128, 128, 128),  
		imagecolorallocate($Grafik, 0, 0, 0),  
		imagecolorallocate($Grafik, 50, 171, 4),  
		imagecolorallocate($Grafik, 128, 0, 128),  
		imagecolorallocate($Grafik, 128, 128, 255),  
		imagecolorallocate($Grafik, 255, 128, 128),  
		imagecolorallocate($Grafik, 192, 122, 0),  
		imagecolorallocate($Grafik, 250, 94, 250),  
		imagecolorallocate($Grafik, 122, 146, 224),  
		imagecolorallocate($Grafik, 122, 155, 180)  
	);  
  
	// Text auf das Bild schreiben  
	imagettftext($Grafik, $Textgroesse, mt_rand(-13, 13), 10, $Texthoehe+mt_rand(-7,8), $Textfarbe1 = $textfarbe[array_rand($textfarbe)] , $Schriftart, $CAPTCHA_RandomText[0]);  
	imagettftext($Grafik, $Textgroesse, mt_rand(-13, 13), 45, $Texthoehe+mt_rand(-7,8), $Textfarbe2 = $textfarbe[array_rand($textfarbe)] , $Schriftart, $CAPTCHA_RandomText[1]);  
	imagettftext($Grafik, $Textgroesse, mt_rand(-13, 13), 80, $Texthoehe+mt_rand(-7,8), $Textfarbe3 = $textfarbe[array_rand($textfarbe)] , $Schriftart, $CAPTCHA_RandomText[2]);  
	imagettftext($Grafik, $Textgroesse, mt_rand(-13, 13), 113, $Texthoehe+mt_rand(-7,8), $Textfarbe4 = $textfarbe[array_rand($textfarbe)] , $Schriftart, $CAPTCHA_RandomText[3]);  
	imagettftext($Grafik, $Textgroesse, mt_rand(-13, 13), 150, $Texthoehe+mt_rand(-7,8), $Textfarbe5 = $textfarbe[array_rand($textfarbe)] , $Schriftart, $CAPTCHA_RandomText[4]);  
	imagettftext($Grafik, $Textgroesse, mt_rand(-13, 13), 191, $Texthoehe+mt_rand(-7,8), $Textfarbe6 = $textfarbe[array_rand($textfarbe)] , $Schriftart, $CAPTCHA_RandomText[5]);  
	  
	if ($Welleneffekt == "2") {  
		  
		$stauchung = mt_rand(0,100)/400+0.10;  
		$Grafikalt = $Grafik;  
		$Grafik = $Grafikhilf;  
		for ($i=1;$i<=65;$i++){  
			imagecopy($Grafik, $Grafikalt, round((sin($i*$stauchung)*2))+3, $i, 1, $i, 230,1);  
		}  
		  
	}  
		  
	// Transparente Grafik drüberlegen  
	imagecopy($Grafik, $Grafik2, 0, 0, 0, 0, 230, 65); 		  
			  
	// Bild ausgeben  
	header("Content-type: image/png");  
	imagepng($Grafik);  
	imagedestroy($Grafik);  
  
  
} else if ($imagettftext == "2") {  
	  
	// Alternativbild ausgeben  
	header ("Content-type: image/png");  
	$im = @imagecreatetruecolor(70, 30)  
	or die("Cannot Initialize new GD image stream");  
	$text_color = imagecolorallocate($im, 255, 255, 255);  
	imagestring($im, 10, 5, 7, $CAPTCHA_RandomText, $text_color);  
	imagepng($im);  
	imagedestroy($im);  
	  
} else {  
	  
	// Fehlermeldung ausgeben  
	header ("Content-type: image/png");  
	$im = @imagecreatetruecolor(430, 30)  
		or die("Cannot Initialize new GD image stream");  
	$text_color = imagecolorallocate($im, 233, 14, 91);  
	imagestring($im, 10, 5, 7, "Fehlerhafte Auswahl($imagettftext)der Funktion! (1 od. 2)", $text_color);  
	imagepng($im);  
	imagedestroy($im);  
	exit();  
}