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.