How to redirect a page on joomla 1.6

We can use the following example to redirect a page if user is logged in or not. The following code can be put on the corresponding components or on module.

$redirect_url = JURI::getInstance()->toString(); //this JURI::getInstance()->toString() gives site url. You can replace it with any of your required url like registration page.
	$user  = & JFactory::getUser();
	If ( $user->id ) {  //if user is logged in 
		//do something
	}  else  {
		$redirect_url  =  str_replace('&','&', $redirect_url); // get rid of any ampersands
		$redirect_url  =  JRoute::_($redirect_url);
		$redirect_url  =  str_replace('&','&', $redirect_url); // get rid of any ampersands again
		$app  = & JFactory::getApplication();
		$app->redirect($redirect_url); //redirect 
	}

Comments

comments

8 Comment

  1. Just wish to say your article is as surprising. The clarity in your post is simply nice and i can assume you are an expert on this subject. Well with your permission allow me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please keep up the gratifying work.

    1. vinay says: Reply

      Thanks. Hope you will like more on future too. You can grab RSS feed from my site.

  2. I was just seeking this info for a while. After 6 hours of continuous Googleing, at last I got it in your site. I wonder what is the Google’s problem that does not rank this kind of informative sites closer to the top. Normally the top web sites are full of garbage.

  3. I admire the useful information and facts you provide inside your posts. Ill bookmark your weblog and also have my children examine up right here usually. Im fairly positive theyll discover a lot of new issues here than anybody else!

  4. anupam says: Reply

    good.
    but no need to get the whole user object (well ok it’d be empty)

    better approach, just check if the user is a guest:


    $isGuest = JFactory::getUser()->guest;

    If ($isGuest) { //if NOT user logged in
    $redirectUrl = JRoute::_("index.php?option=com_users&view=login");
    $app = & JFactory::getApplication();
    $app->redirect($redirectUrl);
    die();
    }

    1. vinay says: Reply

      Hi Anupam,
      Thanks for your comment. Yes we can do. For better about JFactory/getUser, we can follow JFactory/getUser

  5. rao.sampangi says: Reply

    Thank you for the code, it works for me.

  6. Hurrah! In the end I got a web site from where I be capable of truly obtain helpful data regarding my study
    and knowledge.

Leave a Reply