PHP>> header("Location : my_file.php?submit=1#anchor");

Hello everyone

How do I redirect in PHP, using header();?? The thing is I am trying to redirect to an anchor of a page.

here the header();

header("Location : my_file.php?submit=1#anchor");

The "#" seems to be causing problems.

Thanks :)
LVL 12
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

hernst42Connect With a Mentor Commented:
You can try this:

instead of header(..) :

<meta http-equiv="refresh" content="1;url=my_file.php?submit=1#anchor" />
Try this

header("Location : my_file.php?submit=".htmlentities(urlencode("1#anchor")));
AFAIK when using Location the # is not allowed (only absolute URIs should be used)

if you want to use an anchor you might do it via a meta-refresh tag. There the anchor is allowed.

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

minichickenAuthor Commented:

Thanks for your responses.

suresh_asp >> yours didnt work :(

hernst42 >> can you please show me how do i do that with meta-refresh.

Boris AranovichSenior Software EngineerCommented:
why not  content="0;url=my_file.php?submit=1#anchor" (notice the 0 instead of 1)?
Boris AranovichSenior Software EngineerCommented:
By the way, WHAT problems is the header() redirection causing with the # ?
minichickenAuthor Commented:
if i use header(); with # to point to the anchor on another page, it won't work..... i mean it doesnt redirect at all....
suresh_aspConnect With a Mentor Commented:
Check this now
I got it
There shouln't be space after location

Boris AranovichConnect With a Mentor Senior Software EngineerCommented:
Oh... Well, it can be due to the fact that the anchor is really client-sided thingie, and not the part of the URL for the server. Though, IMHO, it should work...
Use either what hernst42 said, or something like this:


Q: Is the redirection is really to the SAME page (just supposed to jump to the anchor), or a different page with some anchor?
Boris AranovichSenior Software EngineerCommented:
suresh_asp, There SHOULD be a space.
Headers are formatter in this manner:

Headername: the_value_of_it\n
SomeOtherHeader: another_value\n
minichickenAuthor Commented:
it should redirect to a different page and jump to that anchor position... So it not on the same page....
Boris AranovichSenior Software EngineerCommented:
Well, then, as I said, either try with hernst42's <meta refresh> or with javascript location='...'
minichickenAuthor Commented:
Thank you all for your inputs.

It was not the # anchor problem, it the max exec time problem, so i increased it to 5 min and it works fine now.
I try to send out 3 emails on that page, so it takes a bit of time.

Some feedback:

suresh_asp  >> The space after location does not make a difference.
hernst42 >> header(); does support # anchors, your provided solution should work (meta refresh)
Nomaed >> You provided solution should work (javascript redirect)

Thanks :)
All Courses

From novice to tech pro — start learning today.