Weird # at end of URL when redirecting between pages

Issue

Problem I’m having is a #f1 is being passed between all of my pages, the #f1 refers to my login form eg 127.0.0.1/#f1 displays login form using div tags. Form posts to itself for validation then is redirected to 127.0.0.1/_af/act_login.php with session variables to login user using die(header(location:)); exit(). But that URL will also have #f1 at end eg 127.0.0.1/_af/act_login.php#f1. Then when I redirect from that page back to 127.0.0.1 it carries it over again eg 127.0.0.1/#f1 bring up the login form again instead of just my home page. Not sure what I’m doing wrong? or where to look to fine answer, any help appreciated. Thank you.

Here’s snippets of the code:

To bring up login form: <a href="#f1"><li>Login</li></a>

Login form:

<div id="f1">
    <div id="loginForm">
        <h3>Login</h3>
        <form name="loginForm" action="" method="post">
            <input type="hidden" name="form" value="Login" />
            <div>Email:</div>
            <div><input type="text" name="email" value="" /></div>
            <div id="newLine"></div>
            <div>Password:</div>
            <div><input type="password" name="password" /></div>
            <div id="newLine"></div>
            <div class="submitButton"><input type="submit" value="SUBMIT" /></div>
        </form>
    </div> </div>

PHP once validation complete to redirect:

die(header("Location: _af/act_login.php"));
    exit();
}

PHP to redirect back once log entries made and log in complete:

die(header('Location: http://127.0.0.1'));
    exit();

Solution

The fragment identifier (#f1) is copied to the new location because you are using a relative URI-Reference. You need to use an absolute URI-Reference:

header ('Location: http://127.0.0.1/_af/act_login.php');

This behaviour is detailed in RFC 7231:

7.1.2.  Location
 Location = URI-reference

The field value consists of a single URI-reference. When it has the
form of a relative reference ([RFC3986], Section 4.2), the final
value is computed by resolving it against the effective request URI
([RFC3986], Section 5).

...

If the Location value provided in a 3xx (Redirection) response does
not have a fragment component, a user agent MUST process the
redirection as if the value inherits the fragment component of the
URI reference used to generate the request target (i.e., the
redirection inherits the original reference's fragment, if any).

References

Answered By – Nisse Engström

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published