Welcome Guest, Not a member yet? Register   Sign In
Use slash-segmented URIs and querystrings in the same app
#1

[eluser]Unknown[/eluser]
Greetings igniters,

I needed to use both URI protocols in one app, e.g.
http://example.org/area_51/search?q=grey+OR+insect-like

Here's the code (APPPATH/libraries/MY_URI.php) that worked for me:

class MY_URI extends CI_URI {
function _fetch_uri_string()
{
if (strtoupper($this->config->item('uri_protocol')) != 'AUTO') {
return parent::_fetch_uri_string();
}

$path = (isset($_SERVER['PATH_INFO']))
? $_SERVER['PATH_INFO']
: @getenv('PATH_INFO');

if (trim($path, '/') != '' && $path != "/".SELF)
{
$this->uri_string = $path;
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
$this->uri_string .= '?' . $_SERVER['QUERY_STRING'];
}
return;
}
}

function _explode_segments()
{
$path_pattern = '|(?<=/)[^/?]+|';
$query_pattern = '|(?<=[?&])([^?&=]+)=([^?&=]+)|';

preg_match_all($path_pattern, $this->uri_string, $path_matches);

// munge URL segments to create Greenstone-friendly docOID
if (
$path_matches && count($path_matches[0]) > 1
&& $path_matches[0][1] != 'search'
&& $path_matches[0][1] != 'browse'
) {
// ... must be view action!
while (count($path_matches[0]) > 2) {
// recursively combine first two segments
$next_segment = array_splice($path_matches[0], 2, 1);
$next_segment = $next_segment[0];
$path_matches[0][1] .= '.' . $next_segment;
}
}

if ($path_matches) {
$path_segments = $path_matches[0];
}
else {
$path_segments = array();
}

foreach($path_segments as $val)
{
// Filter segments for security
$val = trim($this->_filter_uri($val));

if ($val != '')
{
$this->segments[] = $val;
}
}
}
}
#2

[eluser]Colin Williams[/eluser]
In config, set uri_protocol to "PATH_INFO" or "REQUEST_URI" and set enable_query_strings to TRUE and enjoy.
#3

[eluser]Bernd von Fintel[/eluser]
[quote author="Colin Williams" date="1245299753"]In config, set uri_protocol to "PATH_INFO" or "REQUEST_URI" and set enable_query_strings to TRUE and enjoy.[/quote]

How does one access the query string values? I can't seem to find them anywhere...

Edit:
I am not sure its something to do with my routes/htaccess:
http://example/controller/function?id=1 - I can't see it
http://example/index.php/controller/function?id=1 - Can see it
#4

[eluser]garymardell[/eluser]
Code:
$this->input->get("whatever");
#5

[eluser]Bernd von Fintel[/eluser]
[quote author="garymardell" date="1245343014"]
Code:
$this->input->get("whatever");
[/quote]
Even when accessing via http://example/index.php/controller/function?id=1 - I can only see via $_REQUEST not $this->input->get

EDIT: My mistake I was editing the wrong config file :red: I had to use 'REQUEST_URI' rather than 'POST_INFO' though
#6

[eluser]Kamape[/eluser]
Great library overload. Keep up the good work :-)
#7

[eluser]Unknown[/eluser]
Heh, go figure, the config settings worked also. If I'd known the forum has so many eyes, I would have posted before writing the hack!

BTW, there's a number of lines of code in the OP that deal with app-specific stuff, sorry I forgot to remove before posting.

--
yitznewton
#8

[eluser]Colin Williams[/eluser]
Quote:Heh, go figure, the config settings worked also.

I've seen so many exotic solutions to a problem that is so damn easy to solve. It's ridiculous.

Quote:I had to use ‘REQUEST_URI’ rather than ‘POST_INFO’ though

How about PATH_INFO
#9

[eluser]krif[/eluser]
How about this:

In config settings, I use PATH_INFO

In my controller, this one line of code enables me to access the query string values:

Code:
parse_str(urldecode($this->input->server('QUERY_STRING')), $get_array);

For security reasons, always use the 'array' parameter with parse_str(), otherwise it directly translates the query string key value pairs to variables (VERY unsecure!)
(http://us.php.net/manual/en/function.parse-str.php)

Now my query string values are nice and properly listed in the $get_array. Like that, I avoid having to change the setting for enable_query_strings.




Theme © iAndrew 2016 - Forum software by © MyBB