开发者

How do I redirect to the current page in php?

How do I use the header redirect to make it redirect to the开发者_高级运维 current page?

EDIT: I am trying to make it reload the currently shown page in the browser.


header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);


EDIT: I am trying to make it reload the currently shown page in the browser.

PHP by itself can't force a page refresh. You'll need to use javascript:

<input type="button" onclick="window.location.reload(true)" value="Reload It" />

The .reload(true) bit instructs the browser to do a hard refresh (i.e., fetch a new copy of the web page from the server).


I think you need to give us a better understanding of the question. But from what I can tell, you are looking for this:

 header("Location: ".$url);
 exit(1); // Needed!


You can use the following at the very beginning of mypage.php:

header('Location: /mypage.php');

Some info from the manual.


$url = 'mypage.php';
header('Location: ' . $url);
die('<a href="' . $url . '">Click Here</a> if you are not redirected.');

Redirects to mypage.php. If that fails, the user is given a message and link to the redirected page.


You can also do this in HTML with no PHP at all... Simple, but not always suited to your needs (Meta will work EVERY time the page loads)

<meta http-equiv="refresh" content="10;url=http://www.yoursite.com/yourpage.htm" />

Where:

  1. content = seconds to redirect
  2. url = the page you want to redirect to.


If you are rewriting your url you can use $_SERVER['REDIRECT_URL'] to get the current page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜