Jump to content

php redirect automatically to the previous page


kamal213

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:

 


<?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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.