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.
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.
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
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:
Create theme template file page--front.tpl.php
( You should aready have page.tpl.php
).
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