PHP Header(Location:)重定向是否需要指向以“”开头的网站;http://”;


Does a PHP Header(Location:) redirect need to point to a website starting with "http://"?

我正在为我的一个客户端进行一些web开发,但遇到了一个障碍。

**以下是发生的情况:**

-我们的网站上有多份相同的联系表格,但我们希望每个表格在完成后重定向到不同的登录页,以跟踪转化率。

-在这个例子中,我正在处理about页面。当用户提交更多信息请求时,该页面最好重定向到一个唯一的感谢页面(例如,www.sample.com/thankyou about.html);然而,事实并非如此。

-电子邮件会被完美地发送出去,但重定向永远不会发生。没有抛出任何错误——什么都没有。

在我显示代码之前,以下是我自己尝试解决的问题:

-我在表单将输入发送到的.php代码下面创建了一个HTML代码体。据我所知,没有发生重定向,所以我显然还没有看到它。

-我使用了"Header(位置:www.sample.com/thankyou about.html)"的调用,但这也没有任何作用。

-我尝试过使用Javascript的"frame.location"方法,但似乎没有比php更进一步的尝试。

-我已经将源页面和目标页面保存为.php页面,以避免使用.html扩展是问题的根源;同样的结果。

实际上,我唯一能想到的就是连接类型不满足Header()调用的要求。但这不会输出一条错误消息吗?

这是有问题的联系表格:

 <form id="contactForm" class="get-in-touch contact-form light" action="thank-you-about.php" method="post">
                        <input type="hidden" id="contact_nonce" name="contact_nonce" value="68592e213c"/><input type="hidden" name="" value="/"/>
                        <input type="hidden" name="contact-form-value" value="1"/>
                        <div class="iconic-input">
                            <input class="name" type="text" name="name" placeholder="Name*">
                            <i class="icons icon-user-1"></i>
                        </div>
                        <div class="iconic-input">
                            <input class="email" type="text" name="email" placeholder="Email*">
                            <i class="icons icon-email"></i>
                        </div>
                        <textarea class="msg" name="msg" placeholder="Message"></textarea>
                        <input type="submit" value="Send Message">
                        <div class="iconic-button">
                            <input type="reset" value="Clear">
                            <i class="icons icon-cancel-circle-1"></i>
                        </div>
                    </form>

这是当前的.php文件,thank-you-about.hp:

    <?php
// Define some constants
define( "RECIPIENT_NAME", "Sample" );
define( "RECIPIENT_EMAIL", "writewoodcopy@gmail.com" );
define( "EMAIL_SUBJECT", "New request from site sample webpage" );
// Read the form values
$success = false;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^'.'-'' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^'.'-'_'@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$message = isset( $_POST['msg'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['msg'] ) : "";
$messageInterests = "";
$subject = "";
$currentURL = isset($_POST['current-URL'])? $_POST['current-URL'] : "";
//Parse checkboxes, include them in message body
$interestArray = $_POST['interest'];
if(!empty($interestArray)){
    $messageInterests .= "INFORMATION OF INTEREST:   ";
    $N = count($interestArray);
    for($i = 0; $i < $N; $i++)
    {
        $messageInterests .= "'r'n" . $interestArray[$i];
    }
    $subject .= $messageInterests . "'r'n 'r'n";
}
else {
    $subject = "GENERAL INQUIRY 'r'n 'r'n";
}
$subject .= $message;
$message = $subject;
// If all values exist, send the email
if ( $senderName && $senderEmail && EMAIL_SUBJECT && $message ) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: " . $senderName . " <" . $senderEmail . ">";
  mail( $recipient, EMAIL_SUBJECT, $message, $headers );
  header("Location: www.sample.com/thank-you-about.html"); /* Redirect browser */
}
?>

编辑::在任何人询问之前,我知道没有从这个特定的联系表格中解析复选框。它们在我的客户服务页面上很有用,而一般查询则由关于页面、联系我们页面和主页处理。

第二版::所以,我解决了绝对URL问题;然而,重定向问题仍然存在。谢谢大家这么耐心!

PHP头(位置:)重定向是否需要指向网站以"http://"开头?

是的,如果您需要重定向到不同域上的文件,则必须指定完全绝对URL,这包括协议(httphttps等),基于此,此不起作用:

header("Location: www.sample.com/thank-you-about.html");

用途:

header("Location: http://www.sample.com/thank-you-about.html");

如果您需要重定向到同一域上的文件,您可以使用相对路径:

header("Location: thank-you-about.html"); //thank-you-about.html is located on the same dir as the current script

header("Location: /thank-you-about.html"); //thank-you-about.html is located on the root dir of the domain

header()用于发送原始HTTP报头。请参阅»HTTP/1.1有关HTTP标头的详细信息的规范。

阅读有关php header()函数的更多信息。

我的建议是将重定向参数作为动作属性的一部分传递,即

     ...action="sendEmail.php?redirect=thank-you-about" method="post">

然后在服务器端读取并相应地重定向。

 // do your sending email logic here, then redirect:
 $r = $_GET['redirect];
 switch($r){
   case: 'thank-you-about':
     header("Location: /thank-you-about.html"); die();
     // it might be tempting to go 'header("Location: /$r")' directly, but this would introduce quite a security vulnerability.
   break;
   case: 'otherpage':
    header("Location: /thank-you-other.html"); die();
   break;
   default:
      header("Location: /thank-you-default.html"); die();
   break;
  }