PHP:浏览器返回页面

PHP:浏览器返回页面,php,javascript,back,Php,Javascript,Back,我想要这样的东西: <?php if($something) header('Location:javascript:history.go(-2)'); ?> <html><head></head><body> <?php if($something){ ?> <script type="text/javascript"> window.history.go(-2); </script> &

我想要这样的东西:

<?php
if($something)
    header('Location:javascript:history.go(-2)');
?>
<html><head></head><body>
<?php
if($something){
?>
<script type="text/javascript">
window.history.go(-2);
</script>
<?php
}
?>
</body></html>

但是
header('Location:javascript:history.go(-2)')不起作用。还有别的选择吗


我不知道用户刚才所在的页面,因此无法对url进行硬编码。

您不能在
位置
HTTP标头中使用
javascript:
伪协议。事实上,你根本不应该使用它

相反,通过
位置
HTTP头(如果可以)将整个URL发送到您希望用户返回的页面,或者您可以回显
历史记录。back()
,尽管我强烈建议您这样做


作为旁注,发送
位置
HTTP头后,始终退出
。用户代理不必遵循它。

只要他们通过您可以使用的链接到达那里

header("Location: " . $_SERVER['HTTP_REFERER']);

从你的角度来看,我认为你可能在寻找这样的东西:

<?php
if($something)
    header('Location:javascript:history.go(-2)');
?>
<html><head></head><body>
<?php
if($something){
?>
<script type="text/javascript">
window.history.go(-2);
</script>
<?php
}
?>
</body></html>

window.history.go(-2);

根据您的问题。您可以在任何地方使用php脚本中的以下代码

代码:



可能需要使用
isset()
来包装它,并确保它不是空字符串。嗯,我认为对于这种情况,您最好只发送一个包含javascript的空页面。就像迪扎斯的回答谢谢,这很有效。太糟糕了,我又收到了“再次发送post数据?”浏览器提示。没有问题,我想页面中的AJAX组合可能会绕过该提示。例如,浏览器历史记录指向一个GET请求,为了便于示例,该请求以#pagebla&postvars结尾。然后,在加载window.location.hashname的页面扫描时,您就可以看到它应该包含#page bla&postvars。如果知道字符串中存在“postvars”,则可以发布ajax查询,并对结果进行处理。这样你就不会有提示了。