1
votes

Is it possible to redirect a user to different pages, using PHP location header, depending on the page he came from?

I have multiple HTML forms all over my website that perform the same deleting action when I click on the submit button. And I also have multiple PHP files for each HTML form where the only difference is the location header url.

header('location:file.php');

Is there a way to have only one PHP file which redirects to the previous page or something?

2
Sure. Check the $_SERVER array for a referrer.Jay Blanchard
Are you using a framework? If so, which one?PJ Bergeron
yes, there's many ways. some more reliable than others.Marc B
use conitional statementsAmit Verma
Thank you for the help @JayBlanchard. It worked :)Gonçalo

2 Answers

1
votes

Nothing prevents you from passing a $string-variable to header, thus redirecting to multiple locations with the same statement. You could use a $_SESSION-Variable to store some parameter that controls where you want to redirect to.

But with all the broadness you asked, it is hard to give more detailed answers...

1
votes

Thanks everyone! Here's my final code:

header('Location: '.$_SERVER['HTTP_REFERER']);