Peeyush
Peeyush

Reputation: 4828

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

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?

Upvotes: 10

Views: 20579

Answers (5)

Rashmy
Rashmy

Reputation: 125

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

Upvotes: 1

Jon
Jon

Reputation: 2246

I did like @user72740's until I discovered that it can still produce characters not permitted by CI like %.

What I ended up doing is converting the segment string into a hex, them back.

So I created a MY_URI that extended CI_URI and added these methods:

/**
* Segmentize
*
* Makes URI segments,  CI "segment proof"
* Removes dots and forwardslash leaving ONLY hex characters
* Allows to pass "anything" as a CI URI segment and coresponding function param
*
* @access   public
* @return   string
*/
public function segmentize($segment){
    if(empty($segment)){
        return '';
    }
    return bin2hex($segment);
}

/**
* Desegmentize
*
* @access   public
* @return   string
*/
public function desegmentize($segment){
    if(empty($segment)){
        return '';
    }
    return $this->hex2bin($segment);
}

/**
* hex2bin
*
* PHP 5.3 version of 5.4 native hex2bin
*
* @access   public
* @return   string
*/
public function hex2bin($hex) {
    $n = strlen($hex);
    $bin = '';
    $i = 0;

    while($i < $n){
        $a = substr($hex, $i, 2);
        $c = pack('H*', $a);
        if ($i == 0){
            $bin = $c;
        }
        else {
            $bin .= $c;
        }
        $i += 2;
    }

    return $bin;
}

Then used $this->uri->segmentize($url) to create the segment string and $this->uri->desegmentize($this->input->post('url', true)) to get it back into readable format.

Thus

https://www.example.com/somewhere/over/the/rainbow

becomes

68747470733a2f2f7777772e6d79736974652e636f6d2f736f6d6577686572652f6f7665722f7468652f7261696e626f77

and back.

I am sure there is a better way, like a base_convert() implementation, because this way the string can get arbitrarily long. But now I dont have to worry about = signs and padding, etc.

Upvotes: 0

JuanitoMint
JuanitoMint

Reputation: 511

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/

Upvotes: 11

No Results Found
No Results Found

Reputation: 102745

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;
}

Upvotes: 9

Josh
Josh

Reputation: 3475

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.

Upvotes: 1

Related Questions