2011-09-27 64 views
2

我想使用帶有codeIgniter的HTML/PHP中的Post來提供URL。所以我輸出我的URL的BASE64編碼在一個隱藏的領域,因爲該URL從谷歌響應頁面爬網,幾乎任何字符都可以在URL中。因爲我只會在本地服務器上使用我的網站(這是我公司的SEO工具),所以我想在POST數據中啓用任何字符。我已經用$config['permitted_uri_chars'] = '';試過了,但沒有奏效。由於Base64字符串包含=,因此我收到了「不允許的密鑰字符」錯誤。允許URL中的任何字符

我用笨2

編輯:

Fivell的答案是正確的,謝謝。因此,對於感興趣的人,我從上面的Fivell鏈接中獲取了代碼,併爲CodeIgniter寫了一個小類。通過$this->load->library('base64);

<?php 

class Base64 { 

    function url_encode($url) { 
     $data = base64_encode($url); 
     $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); 
    } 

} 

?> 

然後,你可以在任何控制器使用它通過加載庫和恩或通過解密的網址:要做到這一點,添加一個名爲Base64到應用程序/庫文件夾,並插入該代碼$this->base64->url_encode($url)$this->base64->url_decode($url)

回答

1

這不是codeigniter問題,對於POST傳輸,base64不安全。我使用這對函數來安全地編碼/解碼POST數據。

/** 
* 
* 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); 
}