我的PHP脚本有问题,它检查3个变量(下面的代码):
$auth
(邮件作者)
$subj
(邮件主题)
$text
(邮件)
形式:
(注意:我使用了"GET"方法,因为由于某种奇怪的原因,"POST"方法不起作用)
<div id="contact_form">
<form method="get" name="contact" action="home.php">
<input type="hidden"
name="method"
value="send"/>
E-Mail:<br/>
<input type="text"
id="author"
name="author"
class="require input_field"
value=""/>
<br/>
Subject:<br/>
<input type="text"
id="subject"
name="subject"
class="require input_field"
value=""/>
<br/>
Message:<br/>
<textarea id="text"
name="text"
rows="0"
cols="0"
class="required"
value=""></textarea>
<br/>
<input type="submit"
class="submit_btn"
name="submit"
id="submit"
value="Submit" />
</form>
</div>
该表单现在工作正常。
.PHP:
<?php // ||HOME.PHP||
$method = $_GET['method'];
$auth = $_GET['author'];
$subj = $_GET['subject'];
$text = $_GET['text'];
$recv = "mymail@stuff.com";
function redirect($location) {
if($location == "true") {
header("Location: http://mysite.com/home.php?method=result&status=true");
} else {
header("Location: http://mysite.com/home.php?method=result&status=false");
}
}
//...
//Other methods...
//...
//METHOD SEND
if($method == "send") {
//HERE IS THE PROBLEM
//These are apparently not working
//If i leave the form blank and submit it
//these won't redirect to "false" (mail not sent),
//and the script will just continue, send the empty mail
//and redirect to "true" (mail sent)
if(empty($auth)) { redirect(""); }
if(empty($subj)) { redirect(""); }
if(empty($text)) { redirect(""); }
if(!strstr($auth, '@')) { redirect(""); }
if(!strstr($auth, '.')) { redirect(""); }
if(strlen($auth) < 5) { redirect(""); }
if(strlen($subj) < 4) { redirect(""); }
if(strlen($text) < 4) { redirect(""); }
//From here it should work just fine
//As i'm not sure the "RESULT" method (below) is working fine, i
//posted it too.
$auth = "From: " . $auth;
mail($recv,$subj,$text,$auth);
redirect("true");
require("template/footer.html");
exit(0);
}
//METHOD RESULT
if($method == "result") {
$status = $_GET['status'];
if($status == "true") {
echo "mail sent";
} else {
echo "mail not sent";
}
?>
<script language="javascript">
setTimeout("location.href = 'http://adteam.altervista.org/home.php';", 5000);
</script>
<?php
exit(0);
} ?>
这个问题在PHP代码中进行了解释(在"SEND"方法下面的注释中)。
你们有什么建议吗?
设置重定向
标头后,需要停止脚本执行。否则,它只会继续发送邮件,并在将任何标头发送到浏览器之前设置新的重定向标头。
function redirect($location) {
if($location) {
header("Location: http://mysite.com/home.php?method=result&status=true");
} else {
header("Location: http://mysite.com/home.php?method=result&status=false");
}
die();
}
请注意,if( $location == "true" )
是一种反模式;最好使用布尔true
和false
而不是字符串。
应该很容易。你说"是错误的。但它不是:因为"是真的,但空的,这是真的。未设置或指定 false。所以你应该做:
function redirect($location) {
if($location) {
header("Location: http://mysite.com/home.php?method=result&status=true");
exit();
} else {
header("Location: http://mysite.com/home.php?method=result&status=false");
exit();
}
}
And use: redirect(true) / redirect(false);
字符串
的计算结果将始终为 true,即使它为空。这正是我们使用 empty() 而不是 isset() 检查字符串的原因。还有几件事:
- 您应该使用 POST 提交电子邮件。
- 在验证输入之前,您可能应该检查表单是否实际提交。
- 您应该创建并显示特定的错误消息,告诉用户他们未填写哪些必填字段。
- 您应该对输入运行一些仔细的验证例程,以避免您的电子邮件表单用于发送垃圾邮件和恶意软件。
只需在假分支中添加exit
:
function redirect($location) {
if($location == "true") {
header("Location: http://mysite.com/home.php?method=result&status=true");
} else {
header("Location: http://mysite.com/home.php?method=result&status=false");
exit(0); //simply add it here
}
}
header
函数本身不会停止进一步的执行,如果不想在出现问题时发送电子邮件,则必须停止执行任何其他操作。
实际上,您可以简单地在if语句后添加退出。