Allow any character in URL
I want to deliver an URL using Post in HTML/PHP with codeIgniter. So I output my URL's BASE64-Encoded in a hidden field because the URL is crawled from Googles response Page and nearly any character can be in the URL. As I'll only use my website on a local server (it's a SEO tool for my company), I want to enable any character in POST data. I already tried it with $config['permitted_uri_chars'] = '';
, but that did not work. I got a 'Disallowed Key Characters' error because the Base64 string contains a =
.
I'm using CodeIgniter 2
Edit:
Fivell's answer was right, thanks. So, for interested people, I took the code from Fivell's link above and wrote a small class for CodeIgniter. To do this, add a file named Base64
to your application/libraries folder and insert this code:
<?php
class Base64 {
function url_encode($url) {
$data = base64_encode($url开发者_C百科);
$data = str_replace(array('+','/','='),array('-','_',''),$data);
return $data;
}
function url_decode($url) {
$data = str_replace(array('-','_'),array('+','/'),$url);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
return base64_decode($data);
}
}
?>
then, you can use it in any controller by loading the library via $this->load->library('base64);
and en- or decrypt the url via $this->base64->url_encode($url)
or $this->base64->url_decode($url)
http://www.php.net/manual/ru/function.base64-encode.php#63543
I think that the problem is standart base64 encoding/decoding in php which is not url safe
This is not a codeigniter issue, base64 is not safe by for POST transfer. I use this pair of function to safely encode/decode the POST data.
/**
*
* Safely encrypts data for POST transport
* URL issues
* transforms + to spaces
* / are param value separators
* = are param value separators
*
* we process the string on reverse
* @param string $string
*/
function my_urlsafe_b64encode($string)
{
$data = base64_encode($string);
$data = str_replace(array('+','/','='),array('-','_','.'),$data);
return $data;
}
/**
*
* The encoding string had to be specially encoded to be transported
* over the HTTP
*
* The reverse function has to be executed on the client
*
* @param string $string
*/
private function urlsafe_b64decode($string)
{
$data = str_replace(array('-','_','.'),array('+','/','='),$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
return base64_decode($data);
}
精彩评论