php表头重定向无法正常工作

时间:2012-08-07 21:41:53

标签: php forms redirect header location

试图在我的标题命令中找出我的重定向不起作用。表单发送信息,但从不重定向到登录页面。我仍然用PHP代码在耳朵后面湿透,所以请对我的无知感到不满。 HH

<?php
// Get email address
require_once 'config.php';
// Ensures no one loads page and does simple spam check
if( isset($_POST['name']) && empty($_POST['spam-check']) ) {    
    // Declare our $errors variable we will be using later to store any errors
    $error = '';    
    // Setup our basic variables
    $input_name = strip_tags($_POST['name']);
    $input_email = strip_tags($_POST['email']);
    $input_subject = strip_tags($_POST['subject']); 
    // We'll check and see if any of the required fields are empty
    if( strlen($input_name) < 2 ) $error['name'] = 'Please enter your name.';
    // Make sure the email is valid
    if( !filter_var($input_email, FILTER_VALIDATE_EMAIL) ) $error['email'] = 'Please enter a valid email address.';
    // Set a subject & check if custom subject exist
    $subject = "Message from $input_name";
    if( $input_subject ) $subject .= ": $input_subject";    
    $message = "$input_message\n";
    $message .= "\n---\nThis email was sent by contact form.";
    // Now check to see if there are any errors 
    if( !$error ) {
        // No errors, send mail using conditional to ensure it was sent
        if( mail($your_email_address, $subject, $message, "From: $input_email") ) {
            echo '<p class="success">Your email has been sent!</p>';
        } 
        else {
            echo '<p class="error">There was a problem sending your email!</p>';
        }       
    } 
    else {      
        // Errors were found, output all errors to the user
        $response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
        $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
        $response .= (isset($error['message'])) ? $error['message'] . "<br /> \n" : null;
        echo "<p class='error'>$response</p>";      
    }   
} 
else {
    die('Direct access to this page is not allowed.');
    header("Location: http://jjco.energy526.com/Introduction.aspx");
    exit;

}

3 个答案:

答案 0 :(得分:7)

在调用标头之前使用die意味着您永远不会执行header调用。

答案 1 :(得分:2)

模( '...');输出消息并终止当前脚本。

答案 2 :(得分:1)

您的代码执行以die命令结束,因此不会执行此行后面的行作为您的标题代码(重定向)

相关问题