Solved

joomla homepage redirect

Posted on 2013-10-25
13
459 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:flynny
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
sorry for the delay.

I've updated as you requested.
0
 
LVL 1

Accepted Solution

by:
kirblam earned 500 total points
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Welcome Friends, This is the tutorial for how to create new simple module in joomla 1.5. In the module has two important files. One is XML file and another file is PHP controller file. First you need to create the XML file. (CODE) …
Have you ever wanted to use that sweet jQuery gallery plugin on your Joomla site but couldn't get it working due to jQuery conflicts or your lack of knowledge about what's under the Joomla hood? This article will step you through the process of impl…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

15 Experts available now in Live!

Get 1:1 Help Now