Welcome Guest, Not a member yet? Register   Sign In
Why is my custom library failing, with a really weird error message?
#1

[eluser]JackWH[/eluser]
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:

Code:
<?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:

Code:
$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:
Quote: 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
#2

[eluser]WanWizard[/eluser]
Must be something with the way you load it.

If I create the library using the code above, create a new controller with this in the index method:
Code:
$this->load->library('Basecrypt');
$id = 7676424131321;
var_dump($id);
var_dump($this->basecrypt->encode($id));

It produces
Code:
int 7676424131321

string '2b99XlKF' (length=8)

No errors whatsoever.
#3

[eluser]JackWH[/eluser]
That's what I thought...:S

So, what's wrong with the way I'm loading it?

I'm calling it from a view file called from a controller, Code.php:

Code:
Class Code extends Controller {
    
    function __construct()
    {
    parent::__construct();
        $this->load->helper('form','url');
        $this->load->library('form_validation','yourls','basecrypt');
      }

You can see Basecrypt is being loaded. Also, basecrypt is in the Autoload.php config file too...

My view file simply runs it as I described in the first post.

So - what's going wrong? Sad
#4

[eluser]Hernando[/eluser]
[quote author="Jack W-H" date="1277864875"]Hi folks!

Code:
$id = $this->input->post('id');
echo $this->basecrypt->encrypt($id);

[/quote]

encrypt() or encode()?
#5

[eluser]JackWH[/eluser]
Sorry, that really was a typo. I'm using encode() Smile
#6

[eluser]vitoco[/eluser]
you are using
Code:
$this->load->library();
like an autoload array , but actually, from the userguide ( LOAD - LIBRARY )
Code:
$this->load->library('class_name', $config, 'object name');

so when you do this
Code:
$this->load->library('form_validation','yourls','basecrypt');
you're saying, the instance of the 'form_validation' class will be created with 'yourls' params and will be named 'basecrypt'...

The proper way will be ( assuming that 'yourls' it's also a library )
Code:
$this->load->library('form_validation');
$this->load->library('yourls');
$this->load->library('basecrypt');

Saludos
#7

[eluser]JackWH[/eluser]
Seriously?!

Wow... I wish I'd known that from the start, as I've been doing it that way in my entire webapp... ?! If this is really the case, I have no idea how it's still functioning as expected with no other errors...

Thanks, vitoco - I'll give it a try and report back!

Jack
#8

[eluser]WanWizard[/eluser]
Sometimes it can be worth to RTFM... Smile
#9

[eluser]GSV Sleeper Service[/eluser]
I have nothing to add to this other than "Hi Jack, from a fellow NMUG subscriber"




Theme © iAndrew 2016 - Forum software by © MyBB