2016-05-01 31 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