I have searched around (SO and Google) and can't find any proper explanation of how other people handle this situation.
In my application I am using PHP/Apache as a backend and Ben Alman's jQuery BBQ plugin for hashtag browser history. User logins are handled solely by the PHP server end using POST from a login page and state maintained using server side sessions. The problem I have is that if a user visits a URL with a fragment (hash) and they are not logged in they are redirected to the HTTPS login page. This works well in every situation except where the URL contains fragment hash. The same situation would be the case for any redirect initiated from the server side. It's not like javascript can be used in this situation to preserve the hashtag before the redirect as page load never completes before the redirect.
I realise that the fragments will be lost during the redirect (as it's client side only) and is never seen by the server end but how does everyone else handle this situation and preserve the URL originally visited so that the user can end up on the correct page after login or redirect?
Any advice appreciated. Thanks.
Populate a hidden input with the fragment using JavaScript.
<input type="hidden" name="fragment" id="fragment">
<script>
if(window.location.hash){
document.getElementById("#fragment").value=window.location.hash;
}
</script>
Then on the back end, you can redirect like this:
if(isset($_POST['fragment'])) {
$fragment = '#'.$_POST['fragment'];
} else {
$fragment = '';
}
header('Location: http://website/path'.$fragment);
exit;
You should probably urlencode the fragment.