页面重定向到PHP而不是html页面

时间:2016-10-12 02:05:11

标签: php html css

所以这是我的PHP,你可以看到我将它重定向到我的服务器/thanks.html中的文件,但是当我提交我的表单时,它重定向到PHP文件而不是我的HTML文件。不确定发生了什么,我知道不要使用回声。

    

<?php
/* Set e-mail recipient */
$myemail  = "brettkessler1@gmail.com";

/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject  = check_input($_POST['subject'], "Write a subject");
$email    = check_input($_POST['email']);
$website  = check_input($_POST['website']);
$likeit   = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
    $website = '';
}

/* Let's prepare the message for the e-mail */
$message = "BRANDUO MESSAGE!!

Your contact form has been submitted by:

Name: $yourname
E-mail: $email
URL: $website

Like the website? $likeit
How did he/she find it? $how_find

Comments:
$comments

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header("Location:/thanks.html");
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

如果thanks.html与此php文件位于同一目录中,请尝试从thanks.html的infront中删除“/”。

即尝试使用标题(“位置:thanks.html”);

答案 1 :(得分:0)

这是PHP header reference

  

注意:   大多数现代客户端接受相对URI作为»Location:的参数,但是一些较旧的客户端需要绝对URI,包括方案,主机名和绝对路径。您通常可以使用$ _SERVER ['HTTP_HOST'],$ _SERVER ['PHP_SELF']和dirname()自己创建一个相对的URI:

而且,这是他们给出的示例的简化版本......

 /* Redirect to a different page in the current directory that was requested */
 $host  = $_SERVER['HTTP_HOST'];
 $extra = 'mypage.php';
 header("Location: http://$host/$extra");
 exit;