0

I want anonymous users to always see /node/1 but authenticated users to see /myview/uid where uid is the current user's id.

I don't want to use Rules, but would prefer a code solution.

1
  • I'm curious, why do you not want to use Rules? either way you will be adding a new module to the pile.
    – Geoff
    Commented Mar 22, 2015 at 21:23

2 Answers 2

0

You can use hook_user_login function to achieve this.

function custom_module_user_login(&$edit, $account){
  if($acount->uid > 0) {
          $redirection = 'myview/'.$acount->uid;  
  } else{
      $redirection = 'node/1';
  }

  // Unless there is already a redirection going, or the user is trying to reset his password, we redirect to $redirection.

  if (!is_null($redirection) && (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset')){
    $_GET['destination'] = $redirection; // Should we use $edit['redirect'] instead..?
    $GLOBALS['destination'] = $redirection; // Should we use $edit['redirect'] instead..?
  }

}

For more info check here

1
  • since this only runs on a user logging in, how does it address the anonymous aspect of the question?
    – Geoff
    Commented Mar 22, 2015 at 21:21
0

In my case I wanted anonymous users to always see the login page which happens to be the front page and authenticated users to be redirected to /dashboard. My solution is thus:

  1. Create theme template file page--front.tpl.php ( You should aready have page.tpl.php).

  2. Add the following code to the top of the page:

    if($logged_in){
        drupal_goto('dashboard');
    }
    

In your case you can just add the following code on the top of page.tpl.php and it should work just fine.

if($logged_in && request_path() != 'myview/uid'){
    drupal_goto('myview/uid');
}
if(!$logged_in && request_path() != 'node/1'){
    drupal_goto('node/1');
}    

This will redirect all authenticated users to /myview/uid and all anonymous users to node/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.