Part of the EllisLab Network
   
 
encode CI url
Posted: 23 January 2010 05:58 AM   [ Ignore ]  
Summer Student
Total Posts:  11
Joined  11-03-2008

sorry for my bad english…

so, i don’t know if this could ever be usefull for anyone else but me… i found so many answers to my questions and libraries by browsing the forums and the wiki for the past 2 years. so it’s time for me to give something back grin

my goal was to encode the whole CI url, not only a ID or something else passed to a method.
also every link should only be available for a short period of time.
i was thinking about hooks and routing, but ended up with some modifications to CI’s URI class and helpers.

the very basic version of my URI class looks like this:

class MY_URI extends CI_URI{
    
    
public function _fetch_uri_string()
    

        parent
::_fetch_uri_string();
                
        
$url_array =  $this->get_linktrim($this->uri_string"/"), true);
        
        if(
$url_array
        

            $curr_time 
time();
            
$time_space $curr_time $url_array['time'];
    
            if(
$time_space 60*15
                
$url_array['url''welcome/too_late';
        
}
        
        $this
->uri_string $url_array['url'];
    
}
    
    
private function encrypt($ascii)
    
{
        $hex 
'';
        
        for(
$i 0$i strlen($ascii); $i++)
            
$hex .= str_pad(base_convert(ord($ascii[$i]), 1119), 2'0'STR_PAD_LEFT);
        
        return 
$hex;
    
}
    

    
private function decrypt($hex)
    
{
        $ascii 
'';
        
        if (
strlen($hex) % == 1)
            
$hex '0'.$hex;
        
        for(
$i 0$i strlen($hex); $i += 2)
            
$ascii .= chr(base_convert(substr($hex$i2), 1911));
        
        return 
$ascii;
    
}
    

    
public function generate_link($url)
    
{
        
return $this->encryptserialize(array('time' => time(), 'url' => $url)) );
    
}
    

    
public function get_link($url$array false)
    
{
        
if($url == "" || !$url)    
            return 
false;
        
        
$decrypt $this->decrypt($url); 
        
$url_array unserialize($decrypt);
        
        if(!
$array)
            return 
$url_array['url'];
        else return 
$url_array;
    
}
// END MY_URI Class

i also use this helper to extend CI’s standard form and url helper:

function e_anchor($uri ''$title ''$attributes '')
{
    $CI 
=& get_instance();
    
$uri $CI->uri->generate_link($uri);
    return 
anchor($uri$title$attributes);
}
 
function e_redirect($uri ''$method 'location'$http_response_code 302)
{
    $CI 
=& get_instance();
    
$uri $CI->uri->generate_link($uri);
    
redirect($uri$method$http_response_code);
}

function e_form_open($action ''$attributes ''$hidden = array())
{
    $CI 
=& get_instance();
    
$uri $CI->uri->generate_link($uri);
    
    return 
form_open($action$attributes$hidden);
        


a url in your application may look like this:
http://example.com/index.php/6d3m3e3mgjfv3m3g3m26g0f9fdf5263m5iue913e3i3g3e3g30303g3e3nfv3m3f3m26fvg0263ngl

use the helpers above or $this->uri->generate_link(‘controller/method/id’) to generate links in your application. you can work with your controllers and methods as common, all parameters will be passed in the right way, so you won’t have to think about decoding in your controllermethods.

the methods “encrypt” und “decrypt” were posted by someone else in this forum. don’t know where, i’m sorry…

Profile
 
 
Posted: 27 January 2010 05:07 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  11
Joined  11-03-2008

extending CI’s pagination class to generate encoded urls…

class MY_Pagination  extends CI_Pagination 
{
    
function create_links()
    
{
        $output 
parent::create_links();
        return 
preg_replace_callback("/(index.php\/)(.*?)(\"\>)/", array(&$this, 'changetext'), $output);
    }
    
    function changetext(
$txt)
    {    
    
        
$CI = & get_instance();
        return 
$txt[1] . $CI->uri->generate_link($txt[2]) . $txt[3];
    }
Profile
 
 
   
 
 
‹‹ For js_calendar_pi      codeingniter Help ››