2016-05-01 119 views
-1

我正在为我的一个客户做一些网络开发,并且遇到了障碍。PHP头(位置:)重定向需要指向以“http://”开头的网站吗?

**这里是发生了什么事情:**

- 我们有相同的联系表格上我们网站的多个副本,但我们希望每个,以便跟踪转换率完成后重定向到一个不同的着陆页。

- 在这个例子中,我正在处理about页面。当用户提交他们的请求以获取更多信息时,该页面将理想地重定向到唯一的感谢页面(例如,www.sample.com/thank-you-about.html);但是,事实并非如此。

- 电子邮件将完美发送,但重定向永远不会发生。没有错误抛出 - 没有。

在我告诉代码,这是我为了解决它在我自己已经试过:

-I've创建的窗体发送.PHP代码下的HTML代码体它的输入。据我所知,没有重定向正在发生,所以我显然还没有看到它。

- 我使用过“Header(Location:www.sample.com/thank-you-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文件,感谢任您about.php:

<?php 

// Define some constants 
define("RECIPIENT_NAME", "Sample"); 
define("RECIPIENT_EMAIL", "[email protected]"); 
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 */ 

} 

?> 

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

SECOND EDIT ::所以,我修复了绝对的URL问题;但是,重定向问题仍然存在。谢谢大家,如此耐心!

+1

没有比这更好的地方开始比手动http://php.net/manual/en/function.header.php –

+1

没有通过'http://',它是作为一个相对URL处理,所以会正在寻找像'http:// example.com/www.sample.com/thank-you-about.html'这样的东西' –

+0

在这种情况下,如果没有找到相对URL,是不是会输出错误或至少重定向到404页面? – user3579647

回答

4

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标头的更多信息,请参阅»HTTP/1.1 specification

了解更多关于phpheader()的功能。

+0

我认为这个问题更多的是“为什么”而不是提供修复。 –

+0

再次查看整个问题。你会发现* frame.location‘方法“我已经使用JavaScript的尝试’,它似乎并没有得到任何进一步的比PHP的尝试。 -I've保存的源页面和目的地页面.PHP页面在使用.html扩展名处于问题根源的可能性之外;相同的结果。“* - 所以这里有更深层次的东西。 –

+0

“header()”调用发生在表单发布到的.php文件中,如果有意义的话。这是我第一次使用这种编码进行竞赛,所以我不会责怪你与我的方法混淆。 ;) – user3579647

0

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

 ...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; 
    } 
+0

'$ _POST ['redirect']'不存在。你正在寻找'$ r = $ _GET ['重定向];' – Marcus

+0

没错。我的错。 –

+0

嗨@RidIculous,感谢您的提交。这是一个很棒的主意,但不幸的是我仍然遇到同样的问题。一旦这一切结束,我一定会实施! – user3579647