Welcome Guest, Not a member yet? Register   Sign In
Passing a web address to a controller in the URI
#1

[eluser]Unknown[/eluser]
I'm working on a little app where users can submit URLs. I want to make it easy to submit URLs while on other websites, e.g. with a "Digg this" type button, or a bookmarklet like the one for Delicious.

Normally I'd just put the URL as a GET variable (e.g. http://myapp.com/save_link?url=http://myurl.com) but CodeIgniter disables $_GET . I tried putting in the URL as a URI segment, but unencoded forward slashes confuse CodeIgniter as to which argument is which. Encoded forward slashes make it choke with "Object not found!" errors.

Is there a way around this?

I did find
Optionally enable standard query string based URLs
but I don't know what to do with the pre_system hook described there.
#2

[eluser]webscriptz.be[/eluser]
Have you considered looking in the user guide, routing is the thing you need Ashera
#3

[eluser]gunter[/eluser]
from the uri class:
$this->uri->uri_string()

Returns a string with the complete URI. For example, if this is your full URL:
http://example.com/index.php/news/local/345

The function would return this:
/news/local/345

so in you case if the uri looks like this: http://example.com/index.php/controller/...hateverher....
then the function would return /controller/function/www.xy.com/lalala.php/&jjj=djjdjwhateverher
so you only have to delete /controller/function/ to get your uri...
but this solution works of course only if you donĀ“t have any other segment parameters after the passed url
#4

[eluser]internut[/eluser]
Glad I found this! In the future I was wondering how I would handle this. Good stuff. Test later.
#5

[eluser]Unknown[/eluser]
I've gotten closer to what I want, but it's still not quite right.

One thing is that I want to allow the passed-in URLs to have GET strings, e.g. if someone's saving a search result or a page from a site whose CMS uses GET strings. So I had to add '?' '=' '&' and '+' to the 'permitted_uri_chars' setting in config.php .

One thing is that the double slashes in e.g. 'http://' get collapsed to a single slash in uri_string, so to have usable URLs the second slash will have to be added back.

This is what I'm doing right now, assuming a URI string of the form '/bookmarks/add_bookmark/My Search/http://www.google.com/search?hl=en&q=search' :

Code:
function add_bookmark()
{
    $uri_string = $this->uri->uri_string();
    preg_match('/\/bookmarks\/add_bookmark\/(.*)\/((http|https|ftp)\:\/(.*))/', $uri_string, $matches);
        
    $data = Array();
    $data['title'] = $matches[1];
    $data['url'] = $matches[3] . '://' . $matches[4];
        
    $this->load->view('savebookmark_view', $data);
}

Only the '?' needs to be URI-encoded as ? in order to access the query string of the submitted URL. The forward slashes '/' must NOT be encoded or the page will give you an "Object not found!" error.

$matches[2] holds the URL passed, but with only one slash in 'http://' so it's not really usable.

I think there's probably a security issue with what I'm doing but I don't know enough about web security to find it.
#6

[eluser]Phil Sturgeon[/eluser]
Option A
A crazy un-tested theoretical solution.

Code:
fucntion add_bookmark(){
$url_parts =& func_get_params();
$url = implode('/', $url_parts);
}

CodeIgniter explodes URL's on each /. This will take anything after the method name and join it together.

Option B
A boring but clean and known-tested-working solution.

Enable query strings and just use $this->input->get();




Theme © iAndrew 2016 - Forum software by © MyBB