Removing section from URI then continuing to controller/method

Hi all!

I need to append a key:value pair to my URI:


id:1234 could be on the end of any URI on the site (it's a replacement for the querystring). It's always going to be on the end, but I'll not know WHERE it is in the URI, so it might be

So, my question is, how do I remove it from the URI, process it and continue on my way?

I thought about using a pre controller hook, strip it from the URI, process it (I need to set the ID in the cookie and do some other tracking) and then set whatever variable CI needs to know which controller/method to run. When I use get_instance() in a pre-controller hook, I get nothing! Even if I did get something, I don't know what the vars are that I need.

Anyone got any ideas?



[eluser]Michael Wales[/eluser]
Why are you wanting to remove it from the URI? Seems like a bit of obscurity to me and we all know obscurity != security.

I would simply count the number of segments on my URI, retrieve the value of the last segment, and call it good - no reason to hide it (your users are going to see it anyways).

Hi Walesmd.

Thanks for the reply. I did think about doing that, but I'll never know whats before the key:value pair. There might be a controller, method and params, or not or a combination. So CI won't know what to do.

I'm not fussed about whether the pair appears in the URL or not, I just need to be able to remove them from the URL so that CI knows where to route, as with out the pair, the URI would be well formed and CI would know what to do.


With the second URI, CI thinks the key:value pair is param1. I don't want to have to write something into every method to deal with this, so I thought I would work it out first, process the pair and then strip from the URL so CI knows what to do.



$segs = $this->uri->segment_array();
$your_last_segment = array_pop($segs);

this should give you the last segment

[eluser]Michael Wales[/eluser]
What does this id:1234 represent, maybe we can suggest a better way to go about this?


Thanks, but what happens to CI routing in this example?


Basically, there is a legacy system that directs traffic to any page on the site, and it will contain ?id=1234 as a query string, I need to be able to deal with that no matter where it appears. It may also contain other key/value pairs in the QS, but I'll deal with that later.

the first line puts the segments into an array
first example (pseudo code)

second example:

then array_pop($segs) returns the last element of $segs...
read in the php manual about array_pop
so if your segment is always the last one then array_pop would be my choice

Perhaps you could setup some mod_rewrite rules in Apache to rewrite the legacy id to your new CI structure.


I get it now - sorry, was being dumb. How do I then put those segments, minus the one I've popped off, back into the CI routing structure, so that key:value doesn't futz with the routing, BEFORE CI starts to route? That, I think, is the hard part!

:ahhh: don´t panic, Mike!

there are two almost similar functions:
Returns a string with the complete URI. For example, if this is your full URL:

This function is identical to the previous one, except that it returns the re-routed URI in the event you are using CodeIgniter's URI Routing feature.

I think $this->uri->uri_string() will bring you the original uri string.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.