kamal213 Posted December 5, 2011 Share Posted December 5, 2011 Hi guys, Is it possible to make a page automatically redirect to the previous page a user was in once they submit a form? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252504-php-redirect-automatically-to-the-previous-page/ Share on other sites More sharing options...
Drongo_III Posted December 5, 2011 Share Posted December 5, 2011 You could change header location on the page that processes your form data. So once it's done all of the processing (and assuming it doesn't output anything to the page) do: <?php header('Location: http//www.YourFormPageLink.com'); ?> And that'll redirect them anywhere you want. Alternatively if you don't know precisely what page they originated from then you could grab the url of the form page and set it as part a session variable or post it with the form. Then use that data in your header location to send them back to that link. Hi guys, Is it possible to make a page automatically redirect to the previous page a user was in once they submit a form? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252504-php-redirect-automatically-to-the-previous-page/#findComment-1294570 Share on other sites More sharing options...
kamal213 Posted December 5, 2011 Author Share Posted December 5, 2011 Thanks for your reply, I don't know exactly what page they originated from. Could you give me an example who I would see if a url variable is set from the form? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252504-php-redirect-automatically-to-the-previous-page/#findComment-1294576 Share on other sites More sharing options...
SergeiSS Posted December 5, 2011 Share Posted December 5, 2011 kamal213 - if you like to know the url of the form, that you come from, check variable $_SERVER['HTTP_REFERER']. But be careful! This variable could be wrong, because browser can change it before sending to a server. BTW, you may also use header( 'Refresh:.....') in order to show something at this page before real redirecting. Quote Link to comment https://forums.phpfreaks.com/topic/252504-php-redirect-automatically-to-the-previous-page/#findComment-1294586 Share on other sites More sharing options...
kamal213 Posted December 5, 2011 Author Share Posted December 5, 2011 Thanks @SergeiSS, It works perfectly! Is the also a way to redirect to an even lower level i.e to the page before the form page? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252504-php-redirect-automatically-to-the-previous-page/#findComment-1294607 Share on other sites More sharing options...
Drongo_III Posted December 5, 2011 Share Posted December 5, 2011 You would probably need to start a session to track the user if you wanted to do that. Then have some logic to determine the page visited before the form page. At least that would be my first guess at how to do it. You'd essentially just keep overwriting the same session variable for every page unless the user is on the form page where it would pull the page variable instead of overwriting it- which would at that point be set to the last page visited. Although someone might suggest a better way of doing it. Thanks @SergeiSS, It works perfectly! Is the also a way to redirect to an even lower level i.e to the page before the form page? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252504-php-redirect-automatically-to-the-previous-page/#findComment-1294616 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.