[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 533
  • Last Modified:

joomla homepage redirect

Hi all,

on a joomla installation after a user logs into the site i redirect to a particular page.

Now if for any reason the user closes the browser and then goes to the home page (i.e. www.mysite.com) and they are still logged in, I want to redirect to the same page.

if they are not logged in then remain at the home root page.

is this possible?
0
flynny
Asked:
flynny
  • 8
  • 5
1 Solution
 
kirblamCommented:
I think you can do this with a system plugin.

You can easily verify whether a user is either a Guest or an actually logged-in User in Joomla.

I believe you can check whether the requested page is / or the home page designated in Main Menu. Then you'd issue a redirect.

The logic would be something like:
$user = JFactory::getUser();

// we only care if the user is logged in
if ($user->id) {
//check for home page
     $menu = $app->getMenu();
     if ($menu->getActive() == $menu->getDefault()){
        $app = JFactory::getApplication();
        $app->redirect('/whatever.html');
     }
}

*this code is totally not tested
0
 
flynnyAuthor Commented:
ok i added the following to my header in my index template file but its not working?

$user =& JFactory::getUser();
					$menu = $app->getMenu();

					if($menu->getActive() == $menu->getDefault() && $user->id)
					{
							
						$app = JFactory::getApplication();
        					$app->redirect('www.google.co.uk');
					}

Open in new window


am i doing something wrong here?

its joomla 2.5 i am running by the way
0
 
kirblamCommented:
ok, I was thinking of a system plugin but if you're not used to coding extensions, you may be able to get away with putting it directly into the template's index.php.

I will attempt to get it running myself in 2.5.x and I'll post it here. Can't give you a definite time for when I'll have it though.

regards,
K
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
kirblamCommented:
ok, just tested this on 2.5.x. Original block I posted was close but I forgot to instantiate $app early enough.

You only need the protocol in the redirect if it's going offsite. If not, you can get away with "/whatever"

$user = JFactory::getUser();

//var_dump($user);

// only redirect logged in users
if ($user->id) {
	$app = JFactory::getApplication();
	$menu = $app->getMenu();

        // only redirect homepage
	if($menu->getActive() == $menu->getDefault() && $user->id)
	{
		$app = JFactory::getApplication();
		$app->redirect('http://www.google.co.uk');
	}
}

Open in new window


Please let me know if this works for you.

Regards
K
0
 
flynnyAuthor Commented:
Kirblam,


first of all many thanks for taking the time out to code this.

I have added this code tot he top of my page, so i have the following;

<?php
defined('_JEXEC') or die;

/* The following line gets the application object for things like displaying the site name */
$app = JFactory::getApplication();

$option = JRequest::getVar('option', null);
$view = JRequest::getVar('view', null);
$task = JRequest::getVar('task', null);
$layout = JRequest::getVar('layout', null);
$pageclass =  & $app->getParams('com_content');

				$user = JFactory::getUser();

				// only redirect logged in users
				if ($user->id) {
					$app = JFactory::getApplication();
					$menu = $app->getMenu();

				        // only redirect homepage
					if($menu->getActive() == $menu->getDefault() && $user->id)
					{		
						$app = JFactory::getApplication();
						$app->redirect('http://www.google.co.uk');
					}
				}

?>

Open in new window

(just to mention ths link will be internal I just used www.google.com as an example and a test)

however it is still not working. If I am logged in and try to go to www.mydomain.com then I am redirected to a error page?

(the site is running a 3rd party extension could this be causing it because the signup page is normally on the homepage).

Finally, regarding adding as a plugin I am not familiar with Joomla but will this cause the code the be triggered earlier and may fix the issue?
0
 
kirblamCommented:
ok, I am slightly stumped because it's working on my machine.

however, I notice there are too many declarations of $app (which shouldn't be a problem, just messy...)

try:
<?php
defined('_JEXEC') or die;

/* The following line gets the application object for things like displaying the site name */
$app = JFactory::getApplication();

$option = JRequest::getVar('option', null);
$view = JRequest::getVar('view', null);
$task = JRequest::getVar('task', null);
$layout = JRequest::getVar('layout', null);
$pageclass =  & $app->getParams('com_content');

$user = JFactory::getUser();

// only redirect logged in users
if ($user->id) {
    $menu = $app->getMenu();

    // only redirect homepage
    if($menu->getActive() == $menu->getDefault() && $user->id) {		
	$app->redirect('http://www.google.co.uk');
    }
}

?>

Open in new window


If it's messing up when you're logged in, we know it's probably at least getting correctly through the first conditional. We need more information on the error message. Do you have a custom error page or just the default joomla page? The latter would be better.
0
 
flynnyAuthor Commented:
Hi,

Many thanks for the reply.

I using a custom error page from the 3rd party component.

the url is here

www.asianrishta.co.uk

login:mflynn pass:mflynn

as you can see if you go to the link login, and then try to go back to www.asianrishta.co.uk this triggers the error page.

now the strange thing is I have a conditional login check on the main logo link.

		<?php 

	
					if ($user->guest) {
				?>
					<a href="http://www.asianrishta.co.uk">
             			<img src='<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/images/logo.png'></img>
                	</a>
                <?php
					} else {
				?>
					<a href="http://www.asianrishta.co.uk/index.php?Itemid=125">
             			<img src='<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/images/logo.png'></img>
               		 </a>
        		<?php } ?>

Open in new window


which is working fine. any ideas? could it be the plugin catching the error before our code for the redirect?
0
 
kirblamCommented:
Hey, at first glance it *appears* the code may be working and that the target page might have an issue.

"This section is not available."

Did the code work when you were redirecting to http://www.google.co.uk? If you didn't try that, please replace the redirect with http://www.google.co.uk and see what happens - or just let me know what the results were in the first place.

Also, I did a search on this error message and saw a site called "Flirt n Seek". I'm wondering if this is an error message in some component that you're both using. It may be that you're not completely finished configuring your dating component (total guess at the moment).

To test, we can choose a less complicated page for the redirect, like
/index.php/help-and-support
so change that redirect line to:
$app->redirect('http://www.google.co.uk');

Sorry, those are a couple steps to try in rapid succession.

Thanks
K
0
 
flynnyAuthor Commented:
Hi thanks for this


Did the code work when you were redirecting to http://www.google.co.uk? If you didn't try that, please replace the redirect with http://www.google.co.uk and see what happens - or just let me know what the results were in the first place.

not sure what you mean here (sorry)

I tried changing the redirect to;

$app->redirect('/index.php?Itemid=125');

Open in new window


(which I know works from the conditional link on the main logo).

not sure about the other site, but we have a 3rd party module installed for the dating site.  Could coding as a plugin allow our code to be triggered before the components?
0
 
kirblamCommented:
Hi -

Specifically I need you to try:

$app->redirect('/index.php/help-and-support');

Open in new window


thx
K
0
 
kirblamCommented:
Also, if it's done as a plugin, it's possible that you could intercept the traffic before any other extension. It's more involved and someone would have to look at the other extensions on your site

K
0
 
flynnyAuthor Commented:
sorry for the delay.

I've updated as you requested.
0
 
kirblamCommented:
Hi flynny,

I'm stumped at the moment. I know that code above can work because I'm actually running it in 2.5.9.

I think your idea that there's some kind of conflict between another redirect may be correct. I noticed that the homepage redirects to http://www.asianrishta.co.uk/index.php/component/lovefactory/signup?Itemid=101

Ideally, I believe you'd want the homepage to already be the lovefactory signup so there'd be no redirect to http://www.asianrishta.co.uk/index.php/component/lovefactory/signup?Itemid=101, it would simply have that content at /index.php.

That, in turn, would probably solve your homepage redirect issue but that's conjecture. Also, I don't know everything about your site so maybe you need that redirect on the homepage.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now