Hi folks!
So I've put together this CodeIgniter library to encrypt stuff into Base62 and decrypt it back again.
Just before anyone asks, the file is application/libraries/Basecrypt.php
Here's the contents of Basecrypt.php:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Basecrypt
{
function encode($val) {
// can't handle numbers larger than 2^31-1 = 2147483647
$base = 62;
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$str = '';
do {
$i = $val % $base;
$str = $chars[$i] . $str;
$val = ($val - $i) / $base;
} while($val > 0);
return $str;
}
function decode($str) {
$base = 62;
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$len = strlen($str);
$val = 0;
$arr = array_flip(str_split($chars));
for($i = 0; $i < $len; ++$i) {
$val += $arr[$str[$i]] * pow($base, $len-$i-1);
}
return $val;
}
}
/* End of file Basecrypt.php */
/* Location: ./application/libraries/Basecrypt.php */
There's nothing particularly amazing about that. Just encodes and decodes. As far as I can tell, I've followed EVERY rule that CodeIgniter's manual specifies.
Now, just to be 100% sure, I've loaded Basecrypt in both autoload, AND my Controller. Here's where I'm using it:
$id = $this->input->post('id');
echo $this->basecrypt->encrypt($id);
Yep, $id is definitely set. But, nothing happens and I get this error message written to my PHP log:
PHP Fatal error: Call to undefined method CI_Form_validation::encrypt() in /Users/Jack/Sites/mysite.com/system/application/views/code/viewajax.php on line 4
I thought, that's a bit fishy. CI_Form_validation?! I never mentioned that anywhere... did I?
So as a final confirmation I put the two functions encode() and decode() into another custom library I was using (called Awards.php) and called it via $this->awards->encode($id)
- and that worked entirely fine!
To make matters even more confusing, I copy-pasted the entire Awards.php file into Basecrypt.php, only renaming the class declaration, and once again tried calling it through $this->basecrypt->encode($id)
- however, that throws up the same error. Despite no differences between that version of Basecrypt and the version of Awards in which it worked perfectly fine. I've even tried renaming the functions and the class incase something was reserved, but it makes no difference.
Any ideas? Thanks, I really appreciate the help!
Jack