PHP无法检查变量

PHP无法检查变量,php,Php,我的PHP脚本有问题,它检查以下3个变量代码: $auth邮件作者 $subj邮件主题 $text发送邮件消息 表格: 注意:我使用GET方法是因为某些奇怪的原因POST方法不起作用 <div id="contact_form"> <form method="get" name="contact" action="home.php">

我的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代码中SEND方法下面的注释解释了这个问题


你们有什么建议吗?

应该很简单。你的说法是错误的。但事实并非如此:因为是真的,但空虚才是真的。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);
只需在false分支中添加出口:

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语句之后添加exit。

设置重定向头之后,您需要停止脚本执行。否则,它将继续发送邮件,并在将任何头发送到浏览器之前设置新的重定向头

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();
}

请注意,如果$location==true是一种反模式;最好使用布尔值true和false而不是字符串。

字符串的计算结果将始终为true,即使它是空的。这正是我们使用空而不是isset检查字符串的原因。还有几件事:

您应该使用POST来提交电子邮件。 在验证输入之前,您可能应该检查表单是否已实际提交。 您应该创建并显示特定的错误消息,告诉用户他们没有完成哪些必填字段。 您应该对输入运行一些仔细的验证例程,以避免使用您的电子邮件表单发送垃圾邮件和恶意软件。
执行var_dump$_GET并查看方法的值,然后查看enable post data reading标志。也许这会对后期发行有所帮助。你的答案可能对他不起作用,他认为这是reddit?在你添加解决实际问题的出口之前,你被否决了。字符串比较繁琐,但==true不是布尔值true。这一部分应该可以正常工作。请注意,在任何情况下,字符串都不能与布尔值true进行比较,因此您的观点是没有意义的。@juhana lol这是真的,我没有注意到OP用引号括起来了true。。但是OP使用的是==而不是===因此它不是严格的类型比较。不管怎样,这都是错误的做法,OP很可能不知道空字符串的计算结果为true。我知道我的代码非常糟糕,但我仍在学习PHP。谢谢你的提示:没问题!如果你需要更多的帮助,请告诉我。
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();
}