Redirect to same page from where it is called

Hi,

Is there a way to redirect to same page from where action was trigerred?

Example - I have page A. On button clicked I called let’s say B.php on some different folder. Now I’m done with the stuff in B.php and want to redirect to A back.

I mean is there a way where I can save the path of caller and reuse it later? I tried $_SERVER[‘REQUEST_URI’] but this provides the path to which it is called (destination).

Also I’m trying to have common logic written in B.php which can be called from any other location. I can write this in A.php and create object for B.php and get the work done. But by doing this I will have to re-write code for some common functionalities.

Thanks!

You could use $_SERVER[‘HTTP_REFERER’], but you can’t really trust that as it’s easily editable.

You could also put the processing code from B.php into A.php, where the bulk of the code remains in B.php as a function. From A.php, call the function in B.php. Once that’s all done, you’re still in A.php and can display it’s contents. No headers needed at all :slight_smile:

If the user goes from A to B by submitting a form then you can do it easily by submitting the url of A in a hidden element:


<input type="hidden" name="redirect" value="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']) ?>" />

Then page B has the original url sent by page A. I often use this technique and it’s very reliable.

If you don’t use forms then you can pass the original url from A to B in the link as a parameter:


$link_to_B = 'pageB.php?redirect=' . urlencode($_SERVER['REQUEST_URI']);

You could also use cookies or sessions to store the url of A but then this won’t work properly when someone has two tabs/windows open at the same time and go through these pages concurrently.

Thanks!