Redirect users to last visited page after login [RESOLVED]

Hi,

I’m trying to redirect the user to the last visited page, if that page was on the site domain. The code below correctly returns to true/false depending on the previous URL on the login page… However, it does not work using the gform_user_registration_login_redirect_url - probably because $_SERVER[‘HTTP_REFERER’] returns NULL, when the function runs.

Do you have any suggestions, so I can redirect users to the last visited page?

add_filter( 'gform_user_registration_login_redirect_url', 'gf_redirect_user_role', 10, 2 );
	function gf_redirect_user_role( $login_redirect, $sign_on ) {
		GFCommon::log_debug(  __METHOD__ . '(): Running...' );
		if ( ! in_array( 'administrator', $sign_on->roles ) ){
			// get last page url 
			$last_page = $_SERVER['HTTP_REFERER'];
			$profile_url = get_permalink(71);
			
			// IF LAST PAGE WAS FROM KC-NITRA, redirect to last page. Else if go to profile, else go to homepage
			if ( $last_page ) {
				if ( ( strpos($last_page, 'kc-nitra') !== false ) or strpos($last_page, 'kreativnecentrumnitra') !== false ) {
					$login_redirect = $last_page;
				}
			} else if ($profile_url) {
				$login_redirect = get_permalink(71);
			} else {
				$login_redirect = get_home_url();
			}
			
			GFCommon::log_debug(  __METHOD__ . '(): Redirecting to ' . $login_redirect );
		}
		return $login_redirect;
	}

Hello. What is logged when you do this at the beginning of your function (right before you try to set the $last_page)?:

GFCommon::log_debug( __METHOD__ . '(): SERVER var => ' . print_r( $_SERVER, true ) );

Hello @chrishajer ,

here’s the log.

2022-03-12 20:47:38.186819 - DEBUG --> gf_redirect_user_role(): SERVER var => Array
(
    [SERVER_SOFTWARE] => Apache/2.2.34 (Unix) mod_wsgi/3.5 Python/2.7.13 PHP/7.4.2 mod_ssl/2.2.34 OpenSSL/1.0.2o DAV/2 mod_fastcgi/mod_fastcgi-SNAP-0910052141 mod_perl/2.0.11 Perl/v5.24.0
    [REQUEST_URI] => /kc-nitra/login/
    [REDIRECT_HTTP_AUTHORIZATION] => 
    [REDIRECT_STATUS] => 200
    [HTTP_AUTHORIZATION] => 
    [HTTP_HOST] => localhost
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [HTTP_ACCEPT_LANGUAGE] => en-GB,en;q=0.9
    [CONTENT_TYPE] => application/x-www-form-urlencoded
    [HTTP_ORIGIN] => http://localhost
    [HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15
    [HTTP_CONNECTION] => keep-alive
    [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
    [HTTP_REFERER] => http://localhost/kc-nitra/login/
    [CONTENT_LENGTH] => 302
    [HTTP_COOKIE] => pll_language=sk
    [PATH] => /usr/bin:/bin:/usr/sbin:/sbin
    [SERVER_SIGNATURE] => 
    [SERVER_NAME] => localhost
    [SERVER_ADDR] => ::1
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => ::1
    [DOCUMENT_ROOT] => /Applications/MAMP/htdocs
    [SERVER_ADMIN] => you@example.com
    [SCRIPT_FILENAME] => /Applications/MAMP/htdocs/kc-nitra/index.php
    [REMOTE_PORT] => 54954
    [REDIRECT_URL] => /kc-nitra/login/
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => POST
    [QUERY_STRING] => 
    [SCRIPT_NAME] => /kc-nitra/index.php
    [PHP_SELF] => /kc-nitra/index.php
    [REQUEST_TIME_FLOAT] => 1647114456.261
    [REQUEST_TIME] => 1647114456
    [argv] => Array
        (
        )

    [argc] => 0
)
 
2022-03-12 20:47:38.187200 - DEBUG --> gf_redirect_user_role(): Redirecting to http://localhost/kc-nitra/login/

I’ve managed to get this working with a help from the Facebook group. I’ve passed the $last_page variable into the login shortcode.

1 Like

Thank you for the update.