Header('Location: test.html' doesn't work

Hi,

I know this question was already asked - I’ve read the tips there but my contact form just dont want to work and I cant find the error.

The redirection with header doesnt work, all other ponts works except of this one.

I know that bevor header() no output should be done - I checked that there is no space or free line bevor <?php

Probably somebody can help me:confused:
here is the code for contact form


<?php
include ("valid_form.php");
if(isset($_POST['submit'])){
 
 		$name=check_input($_POST['name']);
 		$email=check_input($_POST['email']);
 		$anfrage=check_input($_POST['anf']);
		
		
		$error_msg="";
		
		//Ueberpruefung, ob was eingegeben ist im Feld name
		if(strlen($name)<3)
		{
       		$error_msg="<li >Enter your name</li>";
		}
		
		
		
		//Ueberpruefung der validen eamil adresse
 		if (!preg_match("/([\\w\\-]+\\@[\\w\\-]+\\.[\\w\\-]+)/", $email))
 			{
	 			$error_msg.="<li>Enter your e-mail adress</li>";
			 }
			 
			 
		
			 
			 
		//Ueberpruefung ob bei der Anfrage etwas eingegeben wurde
 		if(strlen($anfrage)<3){
       		$error_msg.="<li>Enter your Question</li>";
		}
		
	
 		//Ausgabe der Fehlermeldung oder Versand der email
		
		if(strlen($error_msg)>0)
		{
       		//Eines der Felder wurde nicht korrekt ausgefuellt 
       		echo "<p>your question wasnot sent because<p><ul>";
       		echo $error_msg;
       		echo "</ul><p>Click to<a href=\\"javascript:history.back(1)\\">back</a> and fill everything</p><br/>";
       	}
		else{
       		//Alle Felder ausgefuellt - eMail generieren
       		$recipient="test@test.at";
       		$subject="A Question from your website";
       		$header="From: " . $email . "\\r\
";
			
			
       		$mail_body ="The Form was filled on  " . date("d.m.Y") . " at " . date("H:i") . "h.\
";
       		$mail_body.="Following inputs were entered:\
\
";
       		$mail_body.="Name: " . $name . "\
";
       		$mail_body.="eMail: " . $email . "\
\
";
       		$mail_body.="Question: " . $anfrage . "\
\
";
       		$mail_body.="\
\
 ---- End of automatically generated mail ----";
			
			
			
		}
		
       		mail($recipient,$subject,$mail_body,$header);
		header('Location: test.html');
		exit;
	
}else{
	header('Location: contact.html');
	exit; 
}
?>

and here is the code for validation - validation works fine


<?php 

//Funktionen
//print "Valiedierungsfunktionen eingefuegt OK";


//CHECK_INPUT
 function check_input($testString)
 {
	 $testString = trim($testString);
	 $testString = stripslashes($testString);
	 $testString = htmlspecialchars($testString);
	 
	 return $testString;
	 
	 

 }
 

  ?>

Please give me a tip what can I also check or where can be the error
Thanks

Welcome to SitePoint… :slight_smile:

What happens? Do you get any error messages? You should always use the full URL in the location header too.


Location: http://www.example.org/home.html

thank you :slight_smile:

I get just white screen instead of thank_you site - no error message
the file in header () is in the same folder as the contact php form

Place the following piece of code before your PHP code, then re-try.


error_reporting(-1);
ini_set('display_errors', true);

It doesn’t matter, whilst it may still work, you should still use the full URL to ensure it works on all platforms.

:slight_smile:

thank you for the answer:)

now i get following error message:

Strict Standards: date() [function.date]: It is not safe to rely on the system’s timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Europe/Berlin’ for ‘CET/1.0/no DST’ instead in /Applications/MAMP/htdocs/a1/contact.php on line 54

Strict Standards: date() [function.date]: It is not safe to rely on the system’s timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Europe/Berlin’ for ‘CET/1.0/no DST’ instead in /Applications/MAMP/htdocs/a1/contact.php on line 54

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/a1/contact.php:54) in /Applications/MAMP/htdocs/a1/contact.php on line 66

line 54 is

$mail_body="Das Formular wurde am " . date("d.m.Y") . " um " . date("H:i") . "h ausgefuehrt.\
";

and line 66 is

header('Location: contact_thanks.html');

can it be that header doesn’t works because of date?

I just deleted all spaces between . and " but it still doesnt works

$mail_body="Das Formular wurde am ".date("d.m.Y")." um ".date("H:i")."h ausgefuehrt.\
";
       		$mail_body.="Folgende Werte wurden eingetragen:\
\
";
       		$mail_body.="Name: ".$name."\
";
       		$mail_body.="eMail: ".$email."\
\
";
       		$mail_body.="Anfrage: ".$anfrage."\
\
";
       		$mail_body.="\
\
 ---- Ende der automatisch generierten eMail ----";

:confused:

Yes, error 1 and 2 cause error 3 by outputting something to the browser.

Comment out the date sections and see if it runs through. If it does then you will probably have to turn the error reporting settings down.

Reference: PHP: date_default_timezone_set - Manual

thanks a lot for your help:-)

wenn I comment date out it works