简体   繁体   English

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

[英]Does a PHP Header(Location:) redirect need to point to a website starting with “http://”?

I'm doing a bit of web development for a client of mine, and I've run into a roadblock. 我做了一些Web开发的我的一个客户,我也遇到了障碍。

**Here's what's going on: ** **这是怎么回事:**

-We have multiple copies of the same contact form on our website, but we want each to redirect to a different landing page upon completion in order to track conversion rates. -我们的网站上有多个相同联系方式的副本,但我们希望每个副本在完成后都重定向到另一个目标页面,以便跟踪转化率。

-In this example, I'm working on the about page. -在这个例子中,我正在“关于”页面上工作。 When a user submits their request for more information, the page would ideally redirect to a unique Thank-You page (eg, www.sample.com/thank-you-about.html); 当用户提交更多信息的请求时,理想情况下,该页面将重定向到唯一的“谢谢”页面(例如,www.sample.com / thank-you-about.html); however, it's not. 但是,事实并非如此。

-The email will get sent off perfectly, but the redirection will never take place. -电子邮件将被完美发送,但是重定向将永远不会发生。 No errors are thrown - nothing. 没有引发任何错误-没有任何错误。

BEFORE I show the code, here's what I've tried in order to solve it on my own: 在我告诉代码,这是我为了解决它在我自己已经试过:

-I've created a body of HTML code underneath the .php code that the form sends its input to. -I've创建的HTML代码的主体.php为代码的形式发送其输入到下方。 To my knowledge, no redirect is taking place, so I've obviously not seen it yet. 据我所知,没有重定向发生,所以我显然还没有看到它。

-I've used the call "Header(Location:www.sample.com/thank-you-about.html)", but that's not doing anything either. -我已经使用了“ Header(Location:www.sample.com/thank-you-about.html)”这个电话,但是那也不做任何事情。

-I've tried using Javascript's "frame.location" method, and it doesn't seem to get any further than the php attempt. -我已经尝试使用Javascript的“ frame.location”方法,而且它似乎比php尝试得更多。

-I've saved the source page and destination page as .php pages on the off-chance that using an .html extension is at the root of the problem; -我已经将源页面和目标页面另存为.php页面,而使用.html扩展名是问题的根源; same outcome. 同样的结果。

Really, the only thing left that I can think of is the connection type not satisfying the requirements for a Header() call. 确实,我唯一能想到的就是连接类型不满足Header()调用的要求。 But wouldn't that output an error message? 但是那不会输出一条错误消息吗?

Here's the contact form in question: 这是有问题的联系表格:

 <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>

Here's the current .php file, thank-you-about.php: 这是当前的.php文件,谢谢-about.php:

    <?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 */

}

?>

EDIT:: Before anyone asks, I know that no checkboxes are being parsed from this particular contact form. 编辑::有人问之前,我知道没有复选框被从该特定的联系表格解析。 They come in handy on my client's service pages, while general inquiries are handled by the about page, contact us page, and homepage. 它们在我客户的服务页面上派上用场,而常规查询则由“关于”页面,“与我们联系”页面和主页处理。

SECOND EDIT:: So, I fixed the absolute URL problem; 第二个编辑::所以,我固定的绝对URL的问题; however, the redirect problem is still persisting. 然而,重定向问题仍然持续。 Thank you all for being so patient! 谢谢大家这么耐心!

Does a PHP Header(Location:) redirect need to point to a website starting with “http://”? PHP标头(位置:)重定向是否需要指向以“ http://”开头的网站?

Yes, if you need to redirect to a file on a different domain , you'll have to specify the full absolute URL , this includes the protocol ( http , https , etc.), based on that, this doesn't work : 是的,如果您需要重定向到另一个域上的文件,则必须指定完整的绝对URL ,其中包括协议( httphttps等),在此基础上,这是行不通的

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

use: 采用:

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

If you need to redirect to a file on the same domain you can use a relative path: 如果您需要重定向到同一域上的文件,则可以使用相对路径:

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

or 要么

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

header() is used to send a raw HTTP header. header()用于发送原始HTTP标头。 See the » HTTP/1.1 specification for more information on HTTP headers. 有关HTTP标头的更多信息,请参见» HTTP / 1.1规范

Read more about the php header() function. 阅读有关php header()函数的更多信息。

My suggestion would be to pass the redirect parameter as part of the action attribute, ie 我的建议是将redirect参数作为action属性的一部分传递,即

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

Then read it out server side and redirect accordingly. 然后从服务器端读出并进行相应的重定向。

 // 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;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM