soycharliente Posted January 19, 2013 Share Posted January 19, 2013 I have a page that contains both a login and registration form. I set the actions on the forms to be index.php#login and index.php#register so that if the form failed, the user would be taken down the form they were just working with. The problem I am running into is that a successful form completion either logs the user in or brings them to a screen letting them know that they need to verify their email and the #login or #register anchor is being included in the next page's URL. So a user visits my /login.php page, successfully logs in to the site, and sees the next page of /page.php#login instead of just /page.php I read though the manual for header() but didn't really see anything relevant to anchors being preserved or how to flush URL information. Is this information being stored somewhere by the browser and I can just purge it from memory? I was able to find this thread on StackOverflow, but my issue here is that I'm not listing the anchor (fragment as they call it) in the redirect. So according to one of the answers, it should get discarded. I've tried structuring the redirect in two different ways to see if anything can be done as well: header('Location: http://example.com/page.php'); header('Location: /page.php'); Any ideas here? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/273358-page-url-keep-anchor-after-header-redirect/ Share on other sites More sharing options...
Muddy_Funster Posted January 21, 2013 Share Posted January 21, 2013 and the code is......? Quote Link to comment https://forums.phpfreaks.com/topic/273358-page-url-keep-anchor-after-header-redirect/#findComment-1407223 Share on other sites More sharing options...
soycharliente Posted January 21, 2013 Author Share Posted January 21, 2013 (edited) Not sure what you're looking for. Edited January 21, 2013 by charlieholder Quote Link to comment https://forums.phpfreaks.com/topic/273358-page-url-keep-anchor-after-header-redirect/#findComment-1407228 Share on other sites More sharing options...
soycharliente Posted January 21, 2013 Author Share Posted January 21, 2013 <?php if ( isset($_POST['login']) && $_POST['login'] == "submit" ) { // do database stuff header('Location: /page.php'); exit(); } if ( isset($_POST['register']) && $_POST['register'] == "submit" ) { // do database stuff header('Location: /index.php?status=verify'); exit(); } ?> <form id="login" action="index.php#login" method="post"> <!-- the form --> <button type="submit" name="login" value="submit">Login</button> </form> <form id="register" action="index.php#register" method="post"> <!-- the form --> <button type="submit" name="register" value="submit">Register</button> </form> Quote Link to comment https://forums.phpfreaks.com/topic/273358-page-url-keep-anchor-after-header-redirect/#findComment-1407229 Share on other sites More sharing options...
Christian F. Posted January 21, 2013 Share Posted January 21, 2013 (edited) Just add the identifier (what you call anchor) to the redirect manually, no need to make it more complex than that. Edited January 21, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/273358-page-url-keep-anchor-after-header-redirect/#findComment-1407232 Share on other sites More sharing options...
soycharliente Posted January 21, 2013 Author Share Posted January 21, 2013 The problem I am running into is that a successful form completion either logs the user in or brings them to a screen letting them know that they need to verify their email and the #login or #register anchor is being included in the next page's URL. So a user visits my /login.php page, successfully logs in to the site, and sees the next page of /page.php#login instead of just /page.php Sorry if it wasn't clear. I actually don't want them. They are being preserved and I do not want them to be preserved. Quote Link to comment https://forums.phpfreaks.com/topic/273358-page-url-keep-anchor-after-header-redirect/#findComment-1407236 Share on other sites More sharing options...
Christian F. Posted January 21, 2013 Share Posted January 21, 2013 (edited) Then remove them from the action attribute of the form tags..? Edited January 21, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/273358-page-url-keep-anchor-after-header-redirect/#findComment-1407239 Share on other sites More sharing options...
soycharliente Posted January 21, 2013 Author Share Posted January 21, 2013 (edited) I set the actions on the forms to be index.php#login and index.php#register so that if the form failed, the user would be taken down the form they were just working with. Ideally I wouldn't want to do that. Unless you know of another way to jump them back down to their form. Edited January 21, 2013 by charlieholder Quote Link to comment https://forums.phpfreaks.com/topic/273358-page-url-keep-anchor-after-header-redirect/#findComment-1407240 Share on other sites More sharing options...
Christian F. Posted January 21, 2013 Share Posted January 21, 2013 (edited) Hmmm... Upon re-reading your thread, I think I understand what your problem is. Though, that said I don't see why it's happening. Will need to do some testing. Added: After some testing it seems as if the browser keeps the identifier and then automatically adds it to the URL, if redirected to the same resource. The way to defeat this is by adding an empty identifier to the redirected URI. In other words: Add a single # to the end of the URIs in your header () calls, and this problem will go away. Edited January 21, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/273358-page-url-keep-anchor-after-header-redirect/#findComment-1407241 Share on other sites More sharing options...
Irate Posted September 29, 2013 Share Posted September 29, 2013 URL rewrite solves the problem. Quote Link to comment https://forums.phpfreaks.com/topic/273358-page-url-keep-anchor-after-header-redirect/#findComment-1451629 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.