Login destination php snippet

Categories:

Login destination php snippet

This is a Drupal very simple way of redirecting users after login, based on their user role.

1. Add a new block.
2. Insert the following code into the block body:

<?php
// invokes current user object.
global $user;

// creates an array of role names for this user.
$roles = array_values($user->roles);

// checks if this user belongs to that role and then redirects.
if(in_array("myrole",$roles)){ // redirect all users in role 'myrole' to 'mypage' after login.
 
drupal_goto("mypage");
}
// you can repeat this part for each role.

?>

3. Under "Show block on specific pages" select "Show on only listed pages" and insert "user/*" (without quotes)
4. Hit submit and select any region for that block to show in.

Here is a complete script

Here is a complete script that I found with several conditions based on User ID or ROLE name. The string for the role name much match exactly (case sensitive).

----------

<?php
global $user;
$roles = array_values($user->roles);

if (
$user->uid==1) {
   return
'admin';
}
elseif(
in_array("website administrator",$roles)){
 
drupal_goto("admin");
}
elseif(
in_array("member - leadership",$roles)){
 
drupal_goto(" "); //the black space indicates the site root
}
elseif(
in_array("member",$roles)){
 
drupal_goto("gettingstarted"); //goes to a page with a URL alias of gettingstarted
}
elseif(
in_array("authenticated user",$roles)){
 
drupal_goto("node/1");  //goes to node/1
}
elseif(
in_array("non-member",$roles)){
 
drupal_goto("nonmember"); //goes to the panel with url alias 'nonmember'
}
else {
  return
'node/9'//catchall if nothing else takes for some odd reason.
}
?>

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
3 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.