Right now i want to restrict all anonymous users to a specific page. I know you can do this with rules by setting the condition if they are anonymous have them redirected to another page. But how can this be done via code? Also I need to add exceptions to the redirect as I dont want users redirected out of user/login page, but everywhere else.

1 Answer 1


I think something like below should work.

function modulename_init() {
  global $user;
  $args = arg();
  if(!user->uid && ($args[0] == 'user' && $args[1] == 'login')) {

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.