[英]Redirect to previous page with php
提交按钮位于contact-us.html
,代码位于contact-us.php
。
我试图使用header('Location: example');
没有运气...(我也尝试使用页面的URL: header('Location: http://www.example.com/contact-us.html');
)
有什么想法吗?
<?php
if($_POST["submit"]) {
$recipient="example@gmail.com";
$subject="Form to email message";
$sender=$_POST["firstname"];
$senderlast=$_POST["lastname"];
$senderemail=$_POST["senderemail"];
$message=$_POST["message"];
$mailBody="First Name: $sender\nLast Name: $senderlast\nEmail: $senderemail\n\nMessage: $message";
header('Location: /contact-us.html'); /*Something Wrong?*/
mail($recipient, $subject, $mailBody, "From: $sender <$senderemail>")
}
您是否尝试过寻找引荐来源? PHP具有此功能。
header('Location: ' . $_SERVER['HTTP_REFERER'])
这意味着,如果它来自cnotact.html,它将看起来它是如何到达并返回的。 如果您在另一页上有第二个联系表,则易于使用。
我需要补充一点,尽管这可能无法通过安全页面起作用。 (因此,如果您通过https运行),则可能会劫持标头(尤其是在浏览器未发送请求的情况下)。
这是我的处理方式:
我建议使用$_SESSION
这样存储以前的网址:
page1.php
<?php
$_SESSION['previous'] = 'http://'. $_SERVER[HTTP_HOST]. $_SERVER[REQUEST_URI];
page2.php
<?php
if ($_SESSION['previous'] !== '') {
header('Location: '. $_SESSION['previous']);
}
$_SESSION
的工作方式类似于Cookie,但效果要好得多。 由于仅使用数组,因此更易于使用-可以在此处找到更多信息: http : //uk1.php.net/manual/en/reserved.variables.session.php
您确定具有submit
名称属性的按钮吗? 如果是,也许试试这个:
<?php
if($_POST["submit"]) {
$recipient="example@gmail.com";
$subject="Form to email message";
$sender=$_POST["firstname"];
$senderlast=$_POST["lastname"];
$senderemail=$_POST["senderemail"];
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header .= 'From: $sender <$senderemail>' . "\r\n";
$message=$_POST["message"];
$mailBody="First Name: $sender\nLast Name: $senderlast\nEmail: $senderemail\n\nMessage: $message";
if(mail($recipient, $subject, $mailBody, $header)){
header('Location:contact-us.html'); /*Something Wrong?*/
}
}
如果没有,那么您的代码将永远不会进入if块
尝试使用JavaScript window.location
重定向注意:wrap window.location.href('contact-us.html');
进入脚本标签
例如:
echo "<script>window.location.href('contact-us.html');</script>";
header('Location: contact-us.html'); /*Something Wrong?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.