Solved

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

Posted on 2004-10-25
1,531 Views
Last Modified: 2012-08-13
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 :)
0
Question by:minichicken
    13 Comments
     
    LVL 2

    Expert Comment

    by:suresh_asp
    Try this

    header("Location : my_file.php?submit=".htmlentities(urlencode("1#anchor")));
    0
     
    LVL 48

    Expert Comment

    by:hernst42
    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
     
    LVL 12

    Author Comment

    by:minichicken
    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
     
    LVL 48

    Accepted Solution

    by:
    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
     
    LVL 3

    Expert Comment

    by:Boris Aranovich
    why not  content="0;url=my_file.php?submit=1#anchor" (notice the 0 instead of 1)?
    0
     
    LVL 3

    Expert Comment

    by:Boris Aranovich
    By the way, WHAT problems is the header() redirection causing with the # ?
    0
     
    LVL 12

    Author Comment

    by:minichicken
    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
     
    LVL 2

    Assisted Solution

    by:suresh_asp
    Check this now
    I got it
    There shouln't be space after location

    header("Location:my_file.php?submit=".htmlentities(urlencode("1#anchor")));
    0
     
    LVL 3

    Assisted Solution

    by:Boris Aranovich
    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
     
    LVL 3

    Expert Comment

    by:Boris Aranovich
    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
     
    LVL 12

    Author Comment

    by:minichicken
    it should redirect to a different page and jump to that anchor position... So it not on the same page....
    0
     
    LVL 3

    Expert Comment

    by:Boris Aranovich
    Well, then, as I said, either try with hernst42's <meta refresh> or with javascript location='...'
    0
     
    LVL 12

    Author Comment

    by:minichicken
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now