Solved

joomla homepage redirect

Posted on 2013-10-25
13
482 Views
Last Modified: 2013-11-12
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
Comment
Question by:flynny
  • 8
  • 5
13 Comments
 
LVL 1

Expert Comment

by:kirblam
ID: 39600571
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
 

Author Comment

by:flynny
ID: 39600677
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
 
LVL 1

Expert Comment

by:kirblam
ID: 39600869
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:kirblam
ID: 39600945
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
 

Author Comment

by:flynny
ID: 39605211
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
 
LVL 1

Expert Comment

by:kirblam
ID: 39605265
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
 

Author Comment

by:flynny
ID: 39605292
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
 
LVL 1

Expert Comment

by:kirblam
ID: 39605307
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
 

Author Comment

by:flynny
ID: 39605350
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
 
LVL 1

Expert Comment

by:kirblam
ID: 39605408
Hi -

Specifically I need you to try:

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

Open in new window


thx
K
0
 
LVL 1

Expert Comment

by:kirblam
ID: 39605413
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
 

Author Comment

by:flynny
ID: 39605485
sorry for the delay.

I've updated as you requested.
0
 
LVL 1

Accepted Solution

by:
kirblam earned 500 total points
ID: 39605522
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Change Joomla Logo Graphic This tutorial goes over the steps to change the Joomla Logo in the template you are using. For this example we will be assuming you are using the rhuk_milkyway Joomla Template which is installed by default. You will …
In the following article I will try to explain some of the issues I had learning Joomla and I hope this helps all of you beginners out there that love the concept but find it confusing and difficult to manage and configure a Joomla website. Like …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question