CodeIgniter Forums
URL as query segment - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: URL as query segment (/thread-20088.html)



URL as query segment - El Forum - 06-27-2009

[eluser]d910qf[/eluser]
Hi all,

I've done some reading and searching but not found an adequate solution yet.

I need to be able to pass a full URL (which could contain any of ? & / : etc) to a controller as myapp.com/controller/function/<encodedurlhere>

I have tried to both urlencode and base64_encode the url before passing it but neither is working - urlencode get's decoded before the function gets called it seems and base64_encode seems to work but the function doesn't like it.

I've read you can use CodeIgniters internal encrypt class to do it but this is for people outside of the site to request thumbnail images of the URL so that is not an option (it has to be a standard php function).

Does anyone have any ideas? I can't even find out how you can pass a query paramater to a function like myapp.com/controller/function/?url=encodedurlhere (without using myapp.com?c=asdas&m=sdfsdf&url=asdfsadf which would be a last resort).

Cheers,

Chris.


URL as query segment - El Forum - 06-27-2009

[eluser]BnoL[/eluser]
How about using POST method? Smile


URL as query segment - El Forum - 06-27-2009

[eluser]BnoL[/eluser]
I got an idea. You can check if it works for you Smile

Firstly, you have to define a route for your URL:

for example: http://localhost/get_thumbnail/{url_here}

so, if the request URL is: http://www.some-domain.com/index.php?adad=asdads&sadasd=gvddf for example.

the URL to your site must be: http://localhost/get_thumbnail/http://www.some-domain.com/index.php?adad=asdads&sadasd=gvddf

and now we'll get the request URL:

Code:
if($this->uri->segment(1) == "get_thumbnail") // if the 1st segment is "get_thumbnail"
        {
            $url = "";
            for($i=2;$i<=$this->uri->total_segments();$i++)
            {
                $url .= $this->uri->slash_segment($i,'leading');
            }
            
            $url = $url."?".$_SERVER['QUERY_STRING'];
            
            if(strstr($url,"/http:/")) // rebuild for long URL, ex: http://www.some-domain.com
                echo str_replace('/http:/','http://',$url); // or return...
            elseif(strstr($url,"/www.")) // without "http://", ex: www.some-domain.com
                echo "http://".str_replace('/www.','www.',$url); // or return...
        }

I've tested, and it works fine for me Smile

There are might be better solutions =]. Storm your brain! Tongue


URL as query segment - El Forum - 06-27-2009

[eluser]slowgary[/eluser]
But that code makes some assumptions, such as the existence of a question mark in the url. Couldn't you just enable query strings in CodeIgniter and then choose a delimiter to wrap the URL in? Something like:
Code:
http://myapp.com/controller/get_thumbnail/{http://www.somedomain.com/index.php?234aqwf=asdf&acvd=q23f}
Then you could just use a regex on the full URL in your get_thumbnail function to extract the string between the curly braces {}. Or if curly braces don't work for you, maybe something like this would work:
Code:
http://myapp.com/controller/get_thumbnail/http://www.somedomain.com/index.php?234aqwf=asdf&acvd=q23f/get_thumbnail
Then you could use 'get_thumbnail' to just read between the lines.


URL as query segment - El Forum - 06-28-2009

[eluser]BnoL[/eluser]
How about this?
Code:
echo end(explode('/get_thumbnail/',$_SERVER['REDIRECT_URL']))."?".$_SERVER['QUERY_STRING'];
much easier, isn't it?


URL as query segment - El Forum - 06-28-2009

[eluser]d910qf[/eluser]
Hi - thanks for the replies. For the following:

http://myapp.com/controller/get_thumbnail/{http://www.somedomain.com/index.php?234aqwf=asdf&acvd=q23f}

How do I access the content between { }? I have enabled query strings in the config but I am not clear how using the above example would work?

Chris.


URL as query segment - El Forum - 06-28-2009

[eluser]d910qf[/eluser]
I should probably explain! The reason why the url (whether encoded or not) needs to be there is that the idea is that people can embed thumbnails in this way:

<img src="http://myapp.com/thumbnail/urlgoeshere/" />

I may add extra parameters for height, width, format etc. I can't believe this is so tricky to accomplish - is there any way to disable CodeIgniters automatic urldecoding on a per controller or function basis?


URL as query segment - El Forum - 06-28-2009

[eluser]d910qf[/eluser]
Answer found at http://ellislab.com/forums/viewthread/120460/#601577

set $config['uri_protocol'] = "PATH_INFO";

and then in your controller

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

now you can have url like http://myapp.com/thumb/generate/?url=<encodedurl>&size=123&format=jpg

and all the values are available in the $get_array array.

Simple!