Welcome Guest, Not a member yet? Register   Sign In
The $_GET data silver bullet
#1

[eluser]Xeoncross[/eluser]
One of the problems that is introduced by CodeIgniter is the loss of sloppy coding by just appending values to the URL in the form of $_GET data.

Code:
http://mail.google.com/mail/?shva=1#inbox

CodeIgniter should be congratulated for helping to move the world out of appending sloppy garbage to URL's just because it is faster. Form data should be $_POST and $_GET data should be a nice URL segment passed to the controller method being called.

Quote:Start Rant<<<
Zend and other frameworks got this wrong by making it "key/value/key2/value2/" pairs which means that you have extra junk on your URI string. And all for what? - So the developer knows what key has what value!? We're not stupid. As CI shows, you should set that in the controller method (view($post_id, $title, etc...)). As a programmer I shouldn't rely the URL to guess what is going on. >>>end-rant

But what happens when you have a single URL value that you really want to append to the URI without having CI choke on the URL or complain about bad characters? The answer is to quickly base 64 encode them.

Code:
//Pass complex values though the URL
function base64_url_encode($string=null){
  return strtr(base64_encode($string), '+/=', '-_~');
}

//Recive complex values though the URL
function base64_url_decode($string=null) {
  return base64_decode(strtr($string, '-_~','+/='));
}

//Note the use of strtr because CI doesn't allow the "+/=" chars in URI's

Examples

Now when using DX Auth I can redirect someone to the login page and pass along a URL they should send the user back to when I am done.

I can also use this on my file uploads system to pass system paths though the URL without the '/' or '\' choking up CI as it tries to create URI segments.

Code:
//$path = '/uploads/subfolder/'
$url_safe_path = base54_url_encode($path);

When using page tokens I can use complex symbols without CI messing with them.


And the list goes on and on. I can't think of a single controller that I have built that isn't using this method to add something to the URL - whether a security token or a return_to path.

I recommend that this function be added to CodeIgniter to deal with all the problems that users keep coming up with when trying to add complex data to the URL.




Theme © iAndrew 2016 - Forum software by © MyBB