How to redirect a user to previous page after successful login? I have added flag links to content pages for anonymous user. When user clicks on that link, the user should be redirected to user login page but after successful login the user should be redirect to last visited page. 

I have used this code of a previously posted thread:

&lt;?php
  global $user;
  if(!$user-&gt;uid) {
  // $flag: bookmark or make_feature, $action: flag, $content_id: $nid.....
  $flag_link = flag_flag_link($flag, $action, $content_id);

  $flag_link = flag_flag_link($flag, $action, $content_id);
  $flag_url = $flag_link['href'];

  //echo implode("/",arg());
    if(isset($flag_link['query']['token'])) {

      $flag_url .= '?'.$flag_link['query']['token'];

    }
  print l(t('SAVE '), 'user/login' , array('query'=&gt; array('destination' =&gt; $flag_url)));
  print l(t('LIST'), 'user/login' , array('query'=&gt; array('destination' =&gt; $flag_url)));
  } else {
    print render($content['links']['flag']);
  }


By using this code, anonymous user can redirect to login page but not to last visited page.
The following implementation of hook_user_login() will override the redirect path set by user_login_submit().

As with the default behavior, setting the destination in the query string overrides this redirect.

&lt;?php

/**
 * Implements hook_user_login().
 */
function mymodule_user_login(&amp;$edit, $account) {
  $edit['redirect'] = $_GET['q'];
}