Reputation: 555
There are a few solutions to redirect if user is not logged in. What if I do not wish to redirect to login page but to main domain webpage
So here this is easy, drupal goto brings us to domain/user/login, but i want redirect to go to domain. Of course, drupal_goto(''); causes an error!
global $user;
if ($user->uid == 0 && arg(0) != 'user' && arg(1) != 'login'){
drupal_goto('user/login');
}
(this is placed in theme template.php file under function phptemplate_preprocess_page(&$vars) )
Upvotes: 0
Views: 449
Reputation: 6791
Well the easiest way is to add the destination parameter to the log out link.
So if your link looks like example.com/logout
change it to example.com/logout&destination=node/1
. After you logout, Drupal will automatically redirect you to the destination. Generating the links is very easy as well, something like this:
<?php if ($user->uid) : ?>
<?php print l(t('log out'), 'logout', array('query' => drupal_get_destination())); ?>
<?php print l($user->name, 'user'); ?>
<?php else : ?>
<?php print l(t('log in'), 'user', array('query' => drupal_get_destination())); ?>
<?php endif; ?>
Update 12.03.2013: For more advanced options regarding the redirects you can install this module: LoginToboggan
Upvotes: 1