core-bundle/src/Resources/contao/library/Contao/System.php
public static function getReferer($blnEncodeAmpersands=false, $strTable=null)
	{
		/** @var Session $objSession */
		$objSession = static::getContainer()->get('session');

		$ref = Input::get('ref');
		$key = Input::get('popup') ? 'popupReferer' : 'referer';
		$session = $objSession->get($key);

		// Unique referer ID
		if ($ref && isset($session[$ref]))
		{
			$session = $session[$ref];
		}
		elseif (\defined('TL_MODE') && TL_MODE == 'BE' && \is_array($session))
		{
			$session = end($session);
		}

		// Use a specific referer
		if ($strTable != '' && isset($session[$strTable]) && Input::get('act') != 'select')
		{
			$session['current'] = $session[$strTable];
		}

		// Remove parameters helper
		$cleanUrl = function ($url, $params=array('rt', 'ref'))
		{
			if ($url == '' || strpos($url, '?') === false)
			{
				return $url;
			}

			list($path, $query) = explode('?', $url, 2);

			/** @var Query $queryObj */
			$queryObj = new Query($query);
			$queryObj = $queryObj->withoutPairs($params);

			return $path . $queryObj->getUriComponent();
		};

		// Determine current or last
		$strUrl = ($cleanUrl($session['current']) != $cleanUrl(Environment::get('request'))) ? $session['current'] : $session['last'];

		// Remove the "toggle" and "toggle all" parameters
		$return = $cleanUrl($strUrl, array('tg', 'ptg'));

		// Fallback to the generic referer in the front end
		if ($return == '' && \defined('TL_MODE') && TL_MODE == 'FE')
		{
			$return = Environment::get('httpReferer');
		}

		// Fallback to the current URL if there is no referer
		if ($return == '')
		{
			$return = (\defined('TL_MODE') && TL_MODE == 'BE') ? 'contao/main.php' : Environment::get('url');
		}

		// Do not urldecode here!
		return preg_replace('/&(amp;)?/i', ($blnEncodeAmpersands ? '&amp;' : '&'), $return);
	}
public static function getReferer($blnEncodeAmpersands=false, $strTable=null)
	{
		/** @var Session $objSession */
		$objSession = static::getContainer()->get('session');

		$ref = Input::get('ref');
		$key = Input::get('popup') ? 'popupReferer' : 'referer';
		$session = $objSession->get($key);

		// Unique referer ID
		if ($ref && isset($session[$ref]))
		{
			$session = $session[$ref];
		}
		elseif (\defined('TL_MODE') && TL_MODE == 'BE' && \is_array($session))
		{
			$session = end($session);
		}

		// Use a specific referer
		if ($strTable != '' && isset($session[$strTable]) && Input::get('act') != 'select')
		{
			$session['current'] = $session[$strTable];
		}

		// Remove parameters helper
		$cleanUrl = function ($url, $params=array('rt', 'ref'))
		{
			if ($url == '' || strpos($url, '?') === false)
			{
				return $url;
			}

			list($path, $query) = explode('?', $url, 2);

			/** @var Query $queryObj */
			$queryObj = new Query($query);
			$queryObj = $queryObj->withoutPairs($params);

			return $path . $queryObj->getUriComponent();
		};

		// Determine current or last
		$strUrl = ($cleanUrl($session['current']) != $cleanUrl(Environment::get('request'))) ? $session['current'] : $session['last'];

		// Remove the "toggle" and "toggle all" parameters
		$return = $cleanUrl($strUrl, array('tg', 'ptg'));

		// Fallback to the generic referer in the front end
		if ($return == '' && \defined('TL_MODE') && TL_MODE == 'FE')
		{
			$return = Environment::get('httpReferer');
		}

		// Fallback to the current URL if there is no referer
		if ($return == '')
		{
			$return = (\defined('TL_MODE') && TL_MODE == 'BE') ? 'contao/main.php' : Environment::get('url');
		}

		// Do not urldecode here!
		return preg_replace('/&(amp;)?/i', ($blnEncodeAmpersands ? '&amp;' : '&'), $return);
	}
Return the referer URL and optionally encode ampersands

@param boolean $blnEncodeAmpersands If true, ampersands will be encoded
@param string  $strTable            An optional table name

@return string The referer URL