views:

65

answers:

7

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.

+1  A: 

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!
St. John Johnson
A: 

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

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

Some info from the manual.

Topher Fangio
A: 
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
zerkms
I'd be a little wary -- might it be possible for an attacker to craft a value of the `Host` header, or a URI that might do something a little unexpected when shoved directly into a `Location` header? Might be a good idea to at least `filter_var()`...
Frank Farmer
@Frank good thinking, but according to the feedback to my question http://stackoverflow.com/questions/2209894/sanitation-for-url-used-in-header-location seemingly not an issue.
Pekka
@Frank Farmer: i can't imagine any really evil data here.
zerkms
A: 
$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.

Jamza
+1  A: 

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).

pygorex1
you can of course do this without needing the browser by adding it to the (e.g.) onLoad event of an element
JD
A: 

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.
JD
A: 

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

murze