开发者

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

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 scr开发者_StackOverflow中文版ipt to be executed twice for an user

Thanks in advance for your ideas and suggestions


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.


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


    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜