【发布时间】:2010-01-20 15:23:45
【问题描述】:
我正在使用 PHP 脚本进行表单验证。我最初在 2007 年编写了这段代码,但现在它停止了工作,我一直在试图找出原因。
代码如下:
<?php
$error_msg = '';
// Only Validate Form when it is submitted
if (isset($formSubmit)) {
if (!isset($_SESSION["First_Name"])) {
$get_mbr_id = urlencode ($_POST["GetMbrID"]);
$_SESSION["MemberID"] = $get_mbr_id;
}
if (!headers_sent()) {
header ("Location: mywebsite.com");
exit (0);
}
}
if (isset($formExit)) {
if (!headers_sent()) {
header ('Location: mywebsiteexit.com');
exit (0);
}
}
?>
<html><head></head><body>
<form name="select_action" method="POST" action="select_action">
<br>
<center>
<input type="submit" name="formSubmit" value="Next">
<input type="reset" name="fieldReset" value="Reset">
<input type="submit" name="formExit" value="Cancel">
</center>
</form></body></html>
如果 HTML 表单代码存在,则标头重定向不起作用。
但是,如果我删除 HTML 表单代码,将 if(isset(formSubmit)) 语句更改为 if(!isset(formSubmit)),则标题重定向将起作用。
我无法弄清楚导致header() 重定向不发生的表单代码发生了什么。
任何帮助将不胜感激!
【问题讨论】:
-
您是否尝试过打印
$formSubmit到var_dump()之类的内容?听起来您(或您的主机)禁用了register_globals设置。 -
这不会解决您的问题,但无论如何,HTTP 的 RFC 声明 Location 标头需要一个完整的 URL,例如
http://www.example.com/。 -
+1 zneak。不仅仅是学术问题;浏览器使用相对 URL 会做出意想不到的事情,当然,如果您期望
Location: mywebsiteexit.com实际访问站点http://mywebsiteexit.com/,那将永远不会发生。