zeckdude
zeckdude

Reputation: 16163

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.

Upvotes: 3

Views: 6687

Answers (7)

murze
murze

Reputation: 4103

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

Upvotes: 0

Darbio
Darbio

Reputation: 11418

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.

Upvotes: 0

leepowers
leepowers

Reputation: 38318

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

Upvotes: 2

Jamescun
Jamescun

Reputation: 693

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

Upvotes: 0

zerkms
zerkms

Reputation: 254944

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

Upvotes: 3

Topher Fangio
Topher Fangio

Reputation: 20667

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

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

Some info from the manual.

Upvotes: 0

St. John Johnson
St. John Johnson

Reputation: 6660

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!

Upvotes: 1

Related Questions