Joomla mod_login redirect using get username

自古美人都是妖i 提交于 2019-12-13 05:12:27

问题


I am building a site using Joomla 3 and would like to redirect each unique user to a specific page when they login from the homepage. For example when they enter their details into the login form and click submit it redirects them to their page with the URL index.php/username

I have found the mod_login/helper.php file but I have no PHP knowledge of how to edit it.

How can I redirect them to a specific page using PHP?

The following code is the mod_login/helper.php file

<?php
/**
 * @package     Joomla.Site
 * @subpackage  mod_login
 *
 * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

/**
 * Helper for mod_login
 *
 * @package     Joomla.Site
 * @subpackage  mod_login
 * @since       1.5
 */
class ModLoginHelper
{
    public static function getReturnURL($params, $type)
    {
        $app    = JFactory::getApplication();
        $router = $app->getRouter();
        $url = null;
        if ($itemid = $params->get($type))
        {
            $db     = JFactory::getDbo();
            $query  = $db->getQuery(true)
                ->select($db->quoteName('link'))
                ->from($db->quoteName('#__menu'))
                ->where($db->quoteName('published') . '=1')
                ->where($db->quoteName('id') . '=' . $db->quote($itemid));

            $db->setQuery($query);
            if ($link = $db->loadResult())
            {
                if ($router->getMode() == JROUTER_MODE_SEF)
                {
                    $url = 'index.php?Itemid='.$itemid;
                }
                else {
                    $url = $link.'&Itemid='.$itemid;
                }
            }
        }
        if (!$url)
        {
            // Stay on the same page
            $uri = clone JURI::getInstance();
            $vars = $router->parse($uri);
            unset($vars['lang']);
            if ($router->getMode() == JROUTER_MODE_SEF)
            {
                if (isset($vars['Itemid']))
                {
                    $itemid = $vars['Itemid'];
                    $menu = $app->getMenu();
                    $item = $menu->getItem($itemid);
                    unset($vars['Itemid']);
                    if (isset($item) && $vars == $item->query)
                    {
                        $url = 'index.php/?Itemid='.$itemid;

                    }
                    else {
                        $url = 'index.php?'.JURI::buildQuery($vars).'&Itemid='.$itemid;
                    }
                }
                else
                {
                    $url = 'index.php?'.JURI::buildQuery($vars);
                }
            }
            else
            {
                $url = 'index.php?'.JURI::buildQuery($vars);
            }
        }

        return base64_encode($url);
    }

    public static function getType()
    {
        $user = JFactory::getUser();
        return (!$user->get('guest')) ? 'logout' : 'login';
    }
}

回答1:


How do you redirect users after a successful login?

You don't need to know PHP to get this done. Please refer the above link.

Read more

http://forum.joomla.org/viewtopic.php?t=729718

How to redirect user after login in Joomla 2.5

Change Redirect URL of Joomla Login Page




回答2:


Try this,

for redirecting after successful login in joomla from any page you can use

<input type="hidden" name="return" value="<?php echo base64_encode("your return url"); ?>" />

This hidden field should found inside your login form some thing like below.

<form action="<?php echo JRoute::_('index.php?option=com_users&task=user.login'); ?>" method="post">

        <fieldset>
            <?php foreach ($this->form->getFieldset('credentials') as $field): ?>
                <?php if (!$field->hidden): ?>
                    <div class="login-fields"><?php echo $field->label; ?>
                    <?php echo $field->input; ?></div>
                <?php endif; ?>
            <?php endforeach; ?>
            <button type="submit" class="button"><?php echo JText::_('JLOGIN'); ?></button>
            <input type="hidden" name="return" value="<?php echo base64_encode($this->params->get('login_redirect_url',$this->form->getValue('return'))); ?>" />
            <?php echo JHtml::_('form.token'); ?>
        </fieldset>
    </form>

You just need to put your return url in the hidden field with base64_encoded joomla will redirect to this url when the login was success.

Hope its helps...




回答3:


A quick ugly fix to redirect to any specific page, or to stay on the same page, from the menu item id.

In the file modules/mod_login/helper.php, class ModLoginHelper, function getReturnURL, using the default joomla login module.

Joomla! 3.2.3 Stable, with EasyPHP (PHP 5.3.5 MySQL 5.1.54-community)

I hope this helps, as a few people had/are having problems with this missing conf option.

Quickly tested, not much reliability, [anew newbie to PHP]

class ModLoginHelper
{
    public static function getReturnURL($params, $type)
    {
        $app    = JFactory::getApplication();
        $router = $app->getRouter();
        $url = null;

         // JT.add:stay on same page (quickfix)
        $stayOnSamePage = false;
        if ($itemid = $params->get($type))
        {
            if($vars['Itemid'] == 101) 
                        // 101 = site default page, to get from the Menu Manager
                        // Can get it programmatically? Worth the fix?
                        // assuming you set the redirect page in the Login module to the site default one
            {
                // stay on the same page
                $stayOnSamePage = true;
            }
        }
        // endOf JT.add: stay on same page


        //JT.rm: if ($itemid = $params->get($type))
        if (($itemid = $params->get($type)) & !$stayOnSamePage) //JT.add
        {


来源:https://stackoverflow.com/questions/18907879/joomla-mod-login-redirect-using-get-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!