Pedro Posada | Login destination php snippet

Login destination php snippet

Posted on March 17, 2008 - 12:10pm
<< 46 of 50 >>

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.

Submitted by Anonymous on April 24, 2008 - 4:28am.

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.
}
?>

Submitted by Linda (not verified) on December 15, 2009 - 7:19pm.

Thank you for sharing this, I was looking for a simple way to do this without installing yet another module. Even though you posted it long ago, it is still useful today! :)

Post new comment

The content of this field is kept private and will not be shown publicly.
Please solve the math question. This way we will know you are not a robot.