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
minichickenAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
hernst42Connect With a Mentor Commented:
You can try this:

instead of header(..) :

?>
<html><head>
<meta http-equiv="refresh" content="1;url=my_file.php?submit=1#anchor" />
</head>
</html>
<?php
die(1);
0
 
suresh_aspCommented:
Try this

header("Location : my_file.php?submit=".htmlentities(urlencode("1#anchor")));
0
 
hernst42Commented:
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.

0
Get your problem seen by more experts

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

 
minichickenAuthor Commented:
Hi

Thanks for your responses.

suresh_asp >> yours didnt work :(

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

Thanks
0
 
Boris AranovichSenior Software EngineerCommented:
why not  content="0;url=my_file.php?submit=1#anchor" (notice the 0 instead of 1)?
0
 
Boris AranovichSenior Software EngineerCommented:
By the way, WHAT problems is the header() redirection causing with the # ?
0
 
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....
0
 
suresh_aspConnect With a Mentor Commented:
Check this now
I got it
There shouln't be space after location

header("Location:my_file.php?submit=".htmlentities(urlencode("1#anchor")));
0
 
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:

<script>location='myfile.php?submit=1#anchor';</script>

Q: Is the redirection is really to the SAME page (just supposed to jump to the anchor), or a different page with some anchor?
0
 
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
\n
<html>......
0
 
minichickenAuthor Commented:
it should redirect to a different page and jump to that anchor position... So it not on the same page....
0
 
Boris AranovichSenior Software EngineerCommented:
Well, then, as I said, either try with hernst42's <meta refresh> or with javascript location='...'
0
 
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 :)
0
All Courses

From novice to tech pro — start learning today.