vincent vincent - 4 months ago 10
PHP Question

How can I get the path of the previous url?

I was wondering how can I get the path of the previous url?

Has someone may enlighten me.

Answer

If you want to return the last response (the previous page where you have been) just do this:

return $this->redirect($request->headers->get('referer'));

If you want to get just the path do this:

$request->headers->get('referer');

If you have been e.g. in

www.example.com/a and the you go to www.example.com/a/b

If you call the referer from "B" it will be: www.example.com/a

NOTE: Be careful with it because the header could be changed by the final users