We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

grab url segments

Development and Programming

chris thacker's avatar
chris thacker
112 posts
7 years ago
chris thacker's avatar chris thacker

I have a template that is of type XML, though I’ve tried RSS too. I’m looking for a way to pass in key/value pairs to drive logic within the template and then display the desired data. How can I do this?

I did find this older post but the returned data using the example php is only “index.php”.

Example URL: example.com/abc?name=Joe&state=CA

I tried this too and nothing is returned:

<?php
    foreach($_GET as $key => $value)
        echo $key . " : " . $value;
?>

Any help would be appreciated! Thanks

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones

$_GET should be available in any PHP context, if your URL has a query string. Where are you running this PHP, and have you verified that your code is indeed executing? This would be best in a plugin most likely, but better advice would need some more real-world info about what you’re trying to accomplish.

       
chris thacker's avatar
chris thacker
112 posts
7 years ago
chris thacker's avatar chris thacker

> Where are you running this PHP, and have you verified that your code is indeed executing?

I’m running it in a template. I’ve tried using both ‘template type’ of xml and rss.

If I run the basic php in a separate index.php page, outside of EE, it properly displays the key/value pairs.

example.com/index.php?one=1&two=2

<?php
    foreach($_GET as $key => $value)
        echo $key . " : " . $value;
?>

… this displays the url parameters. (“one : 1two : 2”)

When I put this in a template, it doesn’t display the key/value pairs:

using myExpressionEngineURL.com/feed?one=1&two=2

<body>
<item>
<?php
    foreach($_GET as $key => $value)
        echo $key . " : " . $value;
?>
</item>
</body>

displays:

<body>
<item></item>
</body>

This…

<item>
<?php
    echo $_SERVER['PHP_SELF'];
?>
</item>

displays this…

<item>
/index.php</item>

This…

<item>
<?php
print_r($_GET);
?>
</item>

displays this…

<item>
Array
(
)
</item>

so it seems that it’s not getting anything after the question mark.

> but better advice would need some more real-world info about what you’re trying to accomplish

I am using a template to create an rss/xml feed of content. I’m trying to use url parameters to determine what content to display in the feed. The above example is just me trying to use basic php to see if it’ll work at all.

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones

For your PHP itself, that works fine in all contexts I can think of. I would make sure that you don’t have any .htaccess in play that is spoiling your query string variables, and are not running any extensions that would otherwise modify it. If that doesn’t help, dump the entire $_SERVER array. There is nothing natively occurring in ExpressionEngine that would empty out the _GET global.

I’m trying to use url parameters to determine what content to display in the feed. The above example is just me trying to use basic php to see if it’ll work at all.

I’m looking for something more specific. I’d like to know how your implementation fits into a real-world scenario, that way I can help with a general approach, rather than be so focused on a specific implementation, e.g. figuring out why GET is empty in your PHP enabled template. The best solution for you may have nothing to do with the fixing the issue at hand.

       
chris thacker's avatar
chris thacker
112 posts
7 years ago
chris thacker's avatar chris thacker

I think you’re right regarding the htaccess file affecting this. I am able to parse REQUEST_URI but it’s just more work.

I’m investigating using an EE template to create an rss/xml feed that is consumed by other software. This other software may have different requirements for the feed contents. (no html, character length, formatting, etc)

I’m thinking that by using parameters (url.com/feed?html=no&charLength=100) and using logic in the template, I can create a custom feed export based on the key/value pair instead of creating multiple templates for each scenario.

Thanks

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones

Ok, thanks Chris. Sounds like a great use for a simple plugin, which will let you control the logic in the template without needing to enable PHP (which has both performance and security concerns). Plugins a very simple to make, you just need a class to handle your tags, following this pattern:

{exp:class:method}

And an addon.setup.php file.

Discover anything between .htaccess or installed ExpressionEngine extensions at play?

       
chris thacker's avatar
chris thacker
112 posts
7 years ago
chris thacker's avatar chris thacker

The htaccess file contains this… Does anything stand out? (note that I changed the system folder name and ‘abc’ subfolder name)

RewriteEngine On
RewriteBase /

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) abc/$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ abc/index.php?/$1 [L]

As far as plugins, we have a bunch installed. I’m not the developer of the site, I’m just playing with this to not add extra work on to the developer’s plate. So I can’t speak to how all the plugins are used.

channel_images char_limit freeform low_random low_replace mo_variables safecracker_file strip_html super_search tagstripper

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones

RewriteRule ^(.*)$ abc/index.php?/$1 [L]

That question mark means that all front-end requests are being treated as query strings. On properly configured servers (except for nginx), this isn’t necessary, but would mean when you access example.com/some/template?foo=bar, you’re really accessing example.com/index.php?/some/template?foo=bar, and that could be the problem depending on your version of ExpressionEngine with certain config options. What version are you running and does your config file have a uri_protocol value set?

Side note: if you have Mo Variables installed, it has generic access to _GET, though you need to make sure you’re on the latest version for security, and you’d still want to validate/sanitize before using in your template. A bespoke plugin to create variables specifically for your dynamic feed generator would still be better, but you could get by with what you have already if you needed to, in other words.

       
chris thacker's avatar
chris thacker
112 posts
7 years ago
chris thacker's avatar chris thacker

This particular version of EE is 2.9 and uri_protocol is set to QUERY_STRING. The server is Windows IIS 8.5.

Yes, we’re going to update to EE 4 this year. 😉

We have a bunch of EE sites and licenses and it’ll happen over time.

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones

Yep, that sounds likely to be the problem. I’d try updating to the latest v2, switching your uri_protocol to AUTO and see if that fixes it. Can’t help you much further as ExpressionEngine 2 hasn’t been supported for quite some time. If that doesn’t get you going, you can try debugging the issue in system/codeigniter/system/core/URI.php, you might stumble on it.

If it were me, I’d update to v4 before building any new features for a site, especially if custom code is involved. You’re only adding on to technical debt, and your future self will not be so happy with your current self. 😉

       
chris thacker's avatar
chris thacker
112 posts
7 years ago
chris thacker's avatar chris thacker

ok, thanks for your help

       
chris thacker's avatar
chris thacker
112 posts
7 years ago
chris thacker's avatar chris thacker

by the way, changing uri_protocol to ‘auto’ breaks some of the site.

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones

Did you update to the latest v2 before doing that?

       
chris thacker's avatar
chris thacker
112 posts
7 years ago
chris thacker's avatar chris thacker

I didn’t… I’ll work with the developer to get it to the latest version 2. I thought our test server was on 2.11.9 already but it’s not.

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones

Yeah there was at least one known bug fixed after 2.9.0 related directly to the uri_protocol options.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.