在Joomla中执行PHP脚本! cms一次 - 基于地理位置的重定向 [英] Execute PHP script inside Joomla! cms once - geo based redirection

查看:103
本文介绍了在Joomla中执行PHP脚本! cms一次 - 基于地理位置的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想要在Joomla内执行PHP脚本的最佳方法!

如果用户不是来自X国家(我已经拥有一个数据库和脚本)

我找到了这个扩展,但我想做我自己的东西 http:// bit.ly/daenzU



1)如何为每位访客执行一次脚本?使用Cookies?

2)我将脚本放入Joomla!模板,我会修改模板/ XXX / index.php

3)基于2),Joomla!每次加载页面时加载模板/ XXX / index.php,所以我必须避免重定向脚本为用户执行两次

感谢您的想法和建议

解决方案

不要修改模板,这样做会诀窍,但不是正确的地方。

我建议您创建一个插件,请参阅 Joomla文档插件。这里是事件的事件执行顺序

创建一个系统插件并实现 onAfterInitialise 方法。在该方法中添加您的所有代码。



为防止每个用户设置用户状态两次执行脚本,查看各州的Joomla文档。你也可以使用session $ session = JFactory :: getSession()请参阅文档



这是您的插件的代码...



<$ p ('_JEXEC')或者死('Restricted access'); $ p $ //没有直接访问
defined

jimport('joomla.plugin.plugin');

类plgMyPlugin扩展JPlugin {

//
公共函数__construct(){
//您的代码
}

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

//
$ state = $ app-> getUserStateFromRequest(plgMyPlugin.is_processed,'is_processed',null);
if(!$ state){
//你的代码在这里
// ....

//设置Steate以防止执行
$ app-> setUserState(plgMyPlugin.is_processed,1);
}


}
}


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

解决方案

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


    }
}

这篇关于在Joomla中执行PHP脚本! cms一次 - 基于地理位置的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆