1

The URL::previous() function is always returning my base URL.
Has somebody encountered this issue as well?

guidsen
  • 2,333
  • 6
  • 34
  • 55

2 Answers2

1

The URL::previous() method uses the HTTP_REFERER Header.
However this header isn't reliable since the browser sends it (or the browser doesn't).
More information on that topic

So either your browser doesn't send the (correct) referer header or you maybe are entering the URL manually (in which case there is no previous URL at all)

Community
  • 1
  • 1
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
0

This is a known problem with URL::previous(), due to inconsistent usage of HTTP_REFERER across browsers (Chrome, in particular, likes to ignore it). You can handle this behavior manually with a bit of a workaround, by storing the current URL in a Session variable before redirecting, then retrieving it (and clearing it) from Session when it's time to redirect back. You can see an implementation of that at http://gist.github.com/msurguy/5158026.

The downside of this approach is that you will get incorrect behavior if the user has multiple tabs open while viewing your site, since only one URL will be stored in the Session variable.

To make this as accurate as possible, you could use a both the built-in URL::previous(), if it's available, or else the Session variable workaround as a fallback if it's not. Just check the value of Request::header('referer') and if it is empty or contains your root URL, then use the fallback stored in the Session variable.

damiani
  • 7,071
  • 2
  • 23
  • 24