I'm customizing a module in Drupal and in that module I'd like to refresh the current page right after an if statement, how can I refresh the current page in PHP
Asked
Active
Viewed 4.3k times
4 Answers
6
You can't in PHP if you've already outputted something (since all headers
must be sent before output begins).
If you haven't outputted:
header('Location:: current_page_url');
or
header("Refresh:0");
Better way is to do it through Javascript with the window
object
$window.location.reload();

q.Then
- 2,743
- 1
- 21
- 31
-
How can I add that js in my php code? should I make a js file and add my js to it? – Gus Sep 06 '15 at 04:54
-
3
Php offers the following function to invoke a page refresh
header("Refresh:0");
If you require a page redirect you can use the following.
header("Refresh:0; url=redirect_page.php");

WeeniehuahuaXD
- 852
- 3
- 10
- 30
-
1Refresh is not a valid HTTP header even though it is implemented in most browsers. There is a standard way also which should be used, which is the Location header. – Sami Kuhmonen Sep 06 '15 at 05:05
0
I'm surprised nobody mentioned the "Drupal Way":
drupal_goto(current_path());
This exact scenario is tested in modules/simpletest/tests/common_test.module
function common_test_init()
:
/**
* Implements hook_init().
*/
function common_test_init() {
if (variable_get('common_test_redirect_current_path', FALSE)) {
drupal_goto(current_path());
}
if (variable_get('common_test_link_to_current_path', FALSE)) {
drupal_set_message(l('link which should point to the current path', current_path()));
}
}
-
this would fail with query parameters. `example.com/q?a=b` will redirect to `example.com/q` – Semra Dec 16 '22 at 22:13