How to pass a URL as a parameter of a controller's method in codeigniter

19,470

Solution 1

In Codeigniter controllers, each method argument comes from the URL separated by a / slash. http://example.com

There are a few different ways to piece together the the arguments into one string:

public function mydata($link)
{
    // URL: http://example.com/mysite/mydata/many/unknown/arguments

    // Ways to get the string "many/unknown/arguments"
    echo implode('/', func_get_args());
    echo ltrim($this->uri->uri_string(), '/');
}

However:

In your case, the double slash // may be lost using either of those methods because it will be condensed to one in the URL. In fact, I'm surprised that a URL like:

http://example.com/mydata/http://abc.com

...didn't trigger Codeigniter's "The URI contains disallowed chatacters" error. I'd suggest you use query strings for this task to avoid all these problems:

http://example.com/mydata/?url=http://abc.com

public function mydata()
{
    $link = $this->input->get('url');
    echo $link;
}

Solution 2

if you want to pass url as parameters then use

urlencode(base64_encode($str))

ie:

$url=urlencode(base64_encode('http://stackoverflow.com/questions/9585034'));
echo $url

result:

aHR0cDovL3N0YWNrb3ZlcmZsb3cuY29tL3F1ZXN0aW9ucy85NTg1MDM0

then you call:

http://example.com/mydata/aHR0cDovL3N0YWNrb3ZlcmZsb3cuY29tL3F1ZXN0aW9ucy85NTg1MDM0

and in your controller

public function mydata($link)
{

$link=base64_decode(urldecode($link));
...
...
...

you have an encoder/decoder here:

http://www.base64decode.org/

Solution 3

Aside from the issue of whether you should be passing a URL in a URL think about how you are passing it:

example.com/theparameter/

but your URL will actually look like

example.com/http://..../

See where you're going wrong yet? The CodeIgniter framework takes the parameter out of the URL, delimited by slashes. So your function is working exactly as it should.

If this is how you must do it then URL encode your parameter before passing it.

Solution 4

You can try this. It worked fr me. "encode" the value before passing

$value = str_replace('=', '-', str_replace('/', '_', base64_encode($album)));

"decode" the value after receiving

$value = base64_decode(str_replace('-', '=', str_replace('_', '/', $value)));

reference: https://forum.codeigniter.com/printthread.php?tid=40607

Share:
19,470
Peeyush
Author by

Peeyush

#Code,#Developer,#Traveller,#ShutterBug

Updated on June 18, 2022

Comments

  • Peeyush
    Peeyush almost 2 years

    I have a Codeigniter controller which takes a full URL as the first argument, but the passed URL inside my controller only is only showing http:

    public function mydata($link)
    {
       echo $link; //then it show only http: rather than the full url http://abc.com
    }
    

    How can i solve this issue?

  • Jon
    Jon almost 9 years
    I like these two methods in conjunction because base64 can have forward slashes in them, and urlencode can leave dots, which will cause CI to think the param is a file and cause a 404. These two methods together remove dots and forward slashes. Nice find!
  • JuanitoMint
    JuanitoMint almost 9 years
    Intresting, can you provide and url that gives you invalid url characters? (I though urlencode handle that)...now I'm worried.