[英]Does a PHP Header(Location:) redirect need to point to a website starting with “http://”?
我做了一些Web开发的我的一个客户,我也遇到了障碍。
**这是怎么回事:**
-我们的网站上有多个相同联系方式的副本,但我们希望每个副本在完成后都重定向到另一个目标页面,以便跟踪转化率。
-在这个例子中,我正在“关于”页面上工作。 当用户提交更多信息的请求时,理想情况下,该页面将重定向到唯一的“谢谢”页面(例如,www.sample.com / thank-you-about.html); 但是,事实并非如此。
-电子邮件将被完美发送,但是重定向将永远不会发生。 没有引发任何错误-没有任何错误。
在我告诉代码,这是我为了解决它在我自己已经试过:
-I've创建的HTML代码的主体.php为代码的形式发送其输入到下方。 据我所知,没有重定向发生,所以我显然还没有看到它。
-我已经使用了“ 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", "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 ,其中包括协议( http
, https
等),在此基础上,这是行不通的 :
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规范 。
阅读有关php
header()函数的更多信息。
我的建议是将redirect参数作为action属性的一部分传递,即
...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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.