36

How can I redirect to the same page using PHP?

For example, locally my web address is:

http://localhost/myweb/index.php

How can I redirect within my website to another page, say:

header("Location: clients.php");

I know this might be wrong, but do I really need to put the whole thing? What if later it is not http://localhost/?

Is there a way to do something like this? Also, I have a lot of code and then at the end after it is done processing some code... I am attempting to redirect using that. Is that OK?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user710502
  • 11,181
  • 29
  • 106
  • 161

9 Answers9

97

My preferred method for reloading the same page is $_SERVER['PHP_SELF']

header('Location: '.$_SERVER['PHP_SELF']);
die;

Don't forget to die or exit after your header();

Edit: (Thanks @RafaelBarros )

If the query string is also necessary, use

header('Location:'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
die;

Edit: (thanks @HugoDelsing)

When htaccess url manipulation is in play the value of $_SERVER['PHP_SELF'] may take you to the wrong place. In that case the correct url data will be in $_SERVER['REQUEST_URI'] for your redirect, which can look like Nabil's answer below:

header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
exit;

You can also use $_SERVER[REQUEST_URI] to assign the correct value to $_SERVER['PHP_SELF'] if desired. This can help if you use a redirect function heavily and you don't want to change it. Just set the correct vale in your request handler like this:

$_SERVER['PHP_SELF'] = 'https://sample.com/controller/etc';
Syntax Error
  • 4,475
  • 2
  • 22
  • 33
  • 8
    If the query string is also necessary, use `header('location:'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']); exit;`. – Rafael Barros Apr 23 '14 at 13:39
  • 2
    @RafaelBarros Thanks for that, I've edited my answer to include your suggestion. – Syntax Error Apr 25 '14 at 18:49
  • 4
    If you use .htaccess to redirect URL's to a single file, this will redirect to that single file and not the URL in the browser. – Hugo Delsing Nov 02 '17 at 08:19
  • I have problem with ampersand. Solution: `header('Location:'. $_SERVER['PHP_SELF'].'?'. str_replace("&","&",$_SERVER['QUERY_STRING']));die;` – houssam Jan 04 '19 at 10:33
  • @HugoDelsing If you're redirecting to a request handler, you can set `$_SERVER['PHP_SELF'] = 'whatever you want';` I usually do this in the request handler itself, with pieces parsed out from some manual config values and `$_SERVER['REQUEST_URI']` (split between each / via regex). This lets me use my redirect code from above without worry and also gives me granular control over my friendly url format. So when htaccess url manipulation is in play use `$_SERVER['REQUEST_URI']` which you can use to assign the correct value to $_SERVER['PHP_SELF'] if desired. – Syntax Error Jan 04 '19 at 16:21
  • @SyntaxError `PHP_SELF` has a function and `REQUEST_URI` has a function. I would never alter server variables, because it would be very hard to track in the future why that value is not what you expect. – Hugo Delsing Feb 21 '19 at 06:21
  • @HugoDelsing Your use of .htaccess in your previous comment gave PHP_SELF a value that you didn't expect. Setting PHP_SELF was suggested as a remedy for cases like that so that it does reflect what you expect it to be. However your point is valid and I agree you shouldn't do things like this haphazardly. – Syntax Error Feb 28 '19 at 17:57
  • @SyntaxError `PHP_SELF` is exactly what it it should be when you use .htaccess. The script that is being executed. It was not intended to reflect the URL you requested. – Hugo Delsing Mar 01 '19 at 08:47
  • @HugoDelsing OK Hugo you don't have to do it this way. Good luck! – Syntax Error Mar 17 '19 at 22:23
  • PHP_SELF is dangerous and insufficient, the entire answer is inconsistent and riddled with petty details and off topic remarks, making it hard to follow, least to implement. But who cares of the only purpose of answers on SO is to boost one's ego. – Your Common Sense Jun 14 '22 at 17:36
  • @your-common-sense You're welcome to post a different answer with your reasoning, and it will rise in visibility if experts agree with you. I'm surprised you didn't already do that since this sounds like it's something you're passionate about. You should not completely change an existing answer because you disagree with it and that's why your edit was rolled back. – Syntax Error Jun 29 '22 at 18:19
19

Another elegant one is

header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
exit;
Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58
12

There are a number of different $_SERVER (docs) properties that return information about the current page, but my preferred method is to use $_SERVER['HTTP_HOST']:

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

where $location is the path after the domain, starting with /.

Nicole
  • 32,841
  • 11
  • 75
  • 101
  • I i am not sure why it is not redirecting, maybe i did it wrong? $location= '/index.php'; header("Location: " . "http://" . $_SERVER['HTTP_HOST'] .$location); – user710502 Nov 15 '11 at 03:21
  • 1
    For debugging, simply output that string with `echo` instead of `header`. If it is correct, then something else is wrong (headers must be the **first** output on the page, for example.) – Nicole Nov 15 '11 at 03:24
10

To really be universal, I'm using this:

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' 
    || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
header('Location: '.$protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit;

I like $_SERVER['REQUEST_URI'] because it respects mod_rewrite and/or any GET variables.

https detection from https://stackoverflow.com/a/2886224/947370

DigiLive
  • 1,093
  • 1
  • 11
  • 28
squarecandy
  • 4,894
  • 3
  • 34
  • 45
5

A quick easy approach if you are not concerned about query params:

header("location: ./");
digout
  • 4,041
  • 1
  • 31
  • 38
2
header('Location: '.$_SERVER['PHP_SELF']);  

will also work

Mahendra Jella
  • 5,450
  • 1
  • 33
  • 38
2

Simple line below works just fine:

header("Location: ?");
Sergey
  • 344
  • 2
  • 12
1

I just tried using header("Location: "); (without any value) and it redirected to the current page.

  • 1
    Providing a destination is old school as a blank url in Internet Explorer would redirect the user to the root of the current folder like "Location: ./". Modern webkit browsers does not. – tim Feb 22 '22 at 22:16
-2

I use correctly in localhost:

header('0');