3

My URL is http://www.website.ro/index.php?subiect=10046&pag=1#1006, and i have a form here with no action (<form action="" method="post">).

After submit I'm checking some input fields and if there is any error, I want to redirect. I use this code:

header('Location: /index.php?subiect='.$_GET['subiect'].'&pag='.$_GET['pag']); exit();

But the redirect includes #1006 so my page becomes http://www.website.ro/index.php?subiect=10046&pag=1#1006

I tried to change location in the header with:

header('Location: http://www.google.com'); exit();

The page where takes me is : http://www.google.com#1006

Why does it do this?

I have ob_start() at first line and i tried to deleted it. No effect. The url after redirect stil has #1006.

If my url is http://www.website.ro/index.php?subiect=10046&pag=1 and i use header location i don't have problems, but if the url contains #1006 the url after redirect contains to.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
  • looking at this: http://stackoverflow.com/questions/7338853/php-location-header-ignore-hash it looks like you can't remove the hash from PHP, you have to clear it using javascript first. – kennypu Dec 28 '12 at 23:12

1 Answers1

3

The part after the # is called the fragment identifier and identifies a part of the page, even if the page is retrieved from another URL.

By default most browsers keep the fragment identifier when a 3xx redirect occurs, regardless of what status code is used. See URL Fragment and 302 redirects for several resources about the matter.

You can return an URI that contains a fragment identifier itself (even an empty one):

header('Location: http://www.google.com/#');

Then that one will be used instead, as specified in this draft:

The exception is when a returned URI already has a fragment identifier. In that case the original fragment identifier MUST NOT be not added to it.

You could also change the fragment identifier onclick using Javascript.

Community
  • 1
  • 1
AndreKR
  • 32,613
  • 18
  • 106
  • 168