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

ぐ巨炮叔叔 提交于 2019-12-10 00:25:37

问题


Hi
I'm trying to figure the best way to execute a PHP script inside Joomla!
I want to redirect users if they are not from X country (already have a database and the script)
I found this extension but I want to make my own stuff http://bit.ly/daenzU

1) How could I execute the script once for each visitor? Working with Cookies?
2) As I'll put the script inside Joomla! template, I'll modify templates/XXX/index.php
3) Based on 2), Joomla! loads templates/XXX/index.php each time a page loads, so I have to avoid redirection script to be executed twice for an user

Thanks in advance for your ideas and suggestions


回答1:


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 );
        }


    }
}



回答2:


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.



来源:https://stackoverflow.com/questions/3897522/execute-php-script-inside-joomla-cms-once-geo-based-redirection

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