ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Simple trick to access $_GET and $_POST vars in templates, without PHP

September 24, 2010 4:40pm

Subscribe [2]
  • #1 / Sep 24, 2010 4:40pm

    Brian Litzinger

    711 posts

    I created a plugin called Parameters to do just this, but here is an even simpler trick for EE2 (the same will work in 1.6 too, just assign it in your path.php file)

    In the root index.php file, update the $assign_to_config variable to the following:

    $req_uri = $_SERVER['REQUEST_URI'];
        
        $assign_to_config['global_vars'] = array(
            'disabled_params' => 'trackbacks|pagination',
            'disabled_params_strict' => 'trackbacks|pagination|member_data|category_fields',
            'disabled_params_all' => 'trackbacks|pagination|member_data|category_fields|categories|custom_fields',
            'get:category' => preg_match("~category=(.*)$~", $req_uri, $matches) ? $matches[1] : NULL,
            'get:year' => preg_match("~year=(.*)$~", $req_uri, $matches) ? $matches[1] : NULL,
            'get:month' => preg_match("~month=(.*)$~", $req_uri, $matches) ? $matches[1] : NULL,
        ); // This array must be associative

    Now you use {get:myvar} anywhere in your templates, even in template tags or conditionals, e.g. {if get:myvar}

    The only side effect to this is you have to know what $_GET variables you’re expecting. I haven’t found a way to create a loop to get any GET or POST var and make it work, b/c if you put {get:foobar} into the template, and $_POST[‘foobar’] is not present, then the template will just display {get:foobar} and not parse it as a blank/false variable.

    Note that since we’re going directly to the REQUEST_URI this will work with any server configuration, and you don’t have to worry about that pesky .htaccess rule.

  • #2 / Jan 20, 2011 6:39am

    LucPestille

    146 posts

    Is there a conflict with using LG’s .htaccess generator and this method? Posting to a template, I’m unable to get the {post:xxx} variable out, using your Parameters plugin, direct from php, or the above. I assume it’s all connected somehow…

  • #3 / Jan 20, 2011 9:08am

    Brian Litzinger

    711 posts

    I don’t use LG generator, so I wouldn’t know if there are any conflicts. I use a very simple, almost stock .htaccess file.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases