Jump to content

php redirect automatically to the previous page


Recommended Posts


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:



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?



Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?



Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.