Thursday 12 July 2012

drupal_goto() | drupal_get_destination() | code sample


One pretty common requirement while building a Drupal module is to redirect a non-logged in user to the login form and from there when the user logs-in redirect him back to the page the user was trying to access before being redirected to the login page.

The first step here is to redirect the user to the login page : This is achieved by drupal_goto();

Point to note here is that, when on the login form if the URL has a query string variable as destination = 'some node' , then on login the user will be taken to the node specified as the destination in the query string.

Now here is a simple code to redirect user to a login page with the current page as destination so that when logged in user is taken to the page where he originally intended to visits.

drupal_goto( 'user/login',drupal_get_destination() );

Not redirecting to originally requested page is kind of annoying to the user and may result in loss of traffic. So its a good practice to have.

No comments:

Post a Comment