php php刷新当前页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4221116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php refresh current page?
提问by Hailwood
I have a page which,
我有一个页面,
If a variable is set(in a session) it will do an action, then it unsets the session.
如果设置了变量(在会话中),它将执行一个操作,然后取消设置会话。
Now it has to refresh itself.
现在它必须自我刷新。
This is where i am stuck.
这就是我被困的地方。
Is there a way to get the exact url to the current page?
有没有办法获得当前页面的确切网址?
or is there a function to do this?
或者是否有一个功能可以做到这一点?
so header('location: ???');
所以 header('location: ???');
回答by Byron Whitlock
header('Location: '.$_SERVER['REQUEST_URI']);
回答by Fernando
PHP refresh current page
PHP刷新当前页面
With PHP code:
使用 PHP 代码:
<?php
$secondsWait = 1;
header("Refresh:$secondsWait");
echo date('Y-m-d H:i:s');
?>
Note: Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
注意:请记住,header() 必须在发送任何实际输出之前调用,无论是通过普通的 HTML 标记、文件中的空行还是来自 PHP。
if you send any output, you can use javascript:
如果您发送任何输出,则可以使用 javascript:
<?php
echo date('Y-m-d H:i:s');
echo '<script type="text/javascript">location.reload(true);</script>';
?>
When this method receives a true value as argument, it will cause the page to always be reloaded from the server. If it is false or not specified, the browser may reload the page from its cache.
当这个方法接收到一个真值作为参数时,它会导致页面总是从服务器重新加载。如果为 false 或未指定,浏览器可能会从其缓存中重新加载页面。
Or you can explicitly use "meta refresh" (with pure html):
或者您可以明确使用“元刷新”(使用纯 html):
<?php
$secondsWait = 1;
echo date('Y-m-d H:i:s');
echo '<meta http-equiv="refresh" content="'.$secondsWait.'">';
?>
Greetings and good code,
问候和良好的代码,
回答by casablanca
$_SERVER['REQUEST_URI']
should work.
$_SERVER['REQUEST_URI']
应该管用。
回答by Lead Developer
header('Location: '.$_SERVER['PHP_SELF']);
will also work
也会工作
回答by Lead Developer
Another elegant one is
另一个优雅的是
header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
exit;