Execute PHP script inside Joomla! cms once - geo based redirection

旧巷老猫 提交于 2019-12-04 20:43:30

DO NOT modify the template, this will do the trick but is not the right place.

I advise you to create a plug-in, see Joomla docs on plug-ins. Here is event execution order of events.

Create a system plugin and implement onAfterInitialise method. In that method add all of your code.

To prevent the execution of script twice for each user set the user state, see Joomla documentation for states. You can also use session $session = JFactory::getSession(), see documentation.

Here is code... for your plug-in.

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgMyPlugin extends JPlugin {

    //
    public function __construct(){
        //  your code here
    }

    //
    public function onAfterInitialise(){
        $app = JFactory::getApplication();

        //
        $state = $app->getUserStateFromRequest( "plgMyPlugin.is_processed", 'is_processed', null );
        if (!$state){
            //  your code here
            //  ....

            //  Set the Steate to prevent from execution
            $app->setUserState( "plgMyPlugin.is_processed", 1 );
        }


    }
}

Just remember that, in Joomla 3.x, (according to the docs) in order to check an information about the user before the 'Login' event, you need to create you plugin in the 'authentication' context. That is, you need to have your plugin at 'root_to_joomla/plugins/authentication/myplugin/myplugin.php'.

Also, your plugin should be a class named PlgAuthenticationMyplugin, it shold extend the base plugin class 'JPlugin' and should have a public method named 'onUserAuthenticate'.

<?php
...
class PlgAuthenticationMyplugin extends JPlugin {
...
    public function onUserAuthenticate($credentials, $options, &$response)
    {
        //your code here (check the users location or whatever)
    }
....

If you want to do that after the Login event, your plugin should be at the user context, at root_to_joomla/plugins/user/myplugin/myplugin.php. And should have a public method 'onUserLogin'.

<?php
class PlgUserMyplugin extends JPLugin {
...
    public function onUserLogin($user, $options)
    {
        //your test goes here
    }
...

You can see all other User related events here.

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