March 24, 2012

Passing URL in Codeigniter URL segment

Question by vivek

I want to pass a url like http://example.com/test?a=1&b=2 in url segment of codeigniter.

I’m trying to pass something like this http://myurl.com/abc/http://example.com/test?a=1&b=2 and get the “http://example.com/test?a=1&b=2” url. What should be the best way to do this?

Answer by Valeh Hajiyev

Set your URI protocol to REQUEST_URI in application/config/config.php , like this:

$config['uri_protocol'] = 'REQUEST_URI';

then use GET method:

$this->input->get('a');

EDIT:

Since http://example.com/test?a=1&b=2 is not encoded URL, it isn’t possible. So first, I would encode URL with urlencode function like this:

urlencode('http://example.com/test?a=1&b=2');

it returns something like: http%3A%2F%2Fexample.com%2Ftest%3Fa%3D1%26b%3D2

So I would pass the URL like this:

http://myurl.com/?url=http%3A%2F%2Fexample.com%2Ftest%3Fa%3D1%26b%3D2

then get an example URL with GET method.

$this->input->get('url');

Answer by Starx

Use this technique to get the URL

$url = "http://example.com/test?a=1&b=2";
$segments = array($controller, $action, $url);
echo site_url($segments);

// or create a anchor link
echo anchor($segments, "click me");

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!