Solved

joomla homepage redirect

Posted on 2013-10-25
13
463 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add data page to Joomla 1 298
Mobile friendly menus in Joomla 4 118
Update Joomla 3.3.6 to Joomla 3.4 in MAMP Pro 3.5 2 109
Please explain this CSS 5 86
What you will need: Any version of Photoshop or similar product (examples in Photoshop) Internet Access FTP Access to your joomla install or ability to upload images within joomla administrator backend Create Image: Lets start off with cre…
This is our step by step to setting up and configuring a Joomla Install on a Windows 2003 Server. This is the perfect solution for an Intranet Website using Joomla. What you need (only use the following versions) : A fresh copy of Joomla http…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now