-
behnampmdg3 Member
  
-
Posts: 75
Threads: 33
Joined: Jan 2015
Reputation:
-1
01-26-2015, 03:23 AM
(This post was last modified: 01-26-2015, 04:03 AM by behnampmdg3.)
Hi;
Why do I get these error?
1 -
Code: A PHP Error was encountered
Severity: Notice
Message: Undefined variable: CI
Filename: libraries/Generate_token.php
Line Number: 15 ( Line 15 is $CI->validate($token);)
2 - http://grab.by/E7A2
Main controller:
PHP Code: $this->load->library('generate_token'); echo $token = $this->generate_token->generate();
Library:
PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); //Custom error handling class Generate_token { public function __construct() { $CI =& get_instance(); } public function generate() { $raw_random_bytes = mcrypt_create_iv(6, MCRYPT_DEV_URANDOM); $encoded_random_bytes = base64_encode($raw_random_bytes); $token = strtr(trim($encoded_random_bytes, '='), array('+' => '-', '/' => '_')); if($CI->validate($token)==$token) { return true; } } public function validate($token) { $sql = "SELECT id FROM url_tokens WHERE short_url = ?"; $query = $CI->db->query($sql, array($token)); if($query->num_rows==0) { return $token; } else { $CI->generate(); } } }
Cheers
-
stopz Junior Member
 
-
Posts: 23
Threads: 6
Joined: Jan 2015
Reputation:
3
I'm assuming this error means $CI is not an object. Like its not being defined like Code: $CI = New Generate_token();
If you loaded this like Code: $this->load->library('generate_token');
Then i think you might try to Code: $this->generate_token->validate($token);
//Please anybody correct me if i'm wrong. I'm also very new to CI but i'm trying to help as much as i can with my little knowledge of PHP
-
behnampmdg3 Member
  
-
Posts: 75
Threads: 33
Joined: Jan 2015
Reputation:
-1
01-26-2015, 04:21 AM
(This post was last modified: 01-26-2015, 04:22 AM by behnampmdg3.)
(01-26-2015, 04:15 AM)stopz Wrote: I'm assuming this error means $CI is not an object. Like its not being defined like
Code: $CI = New Generate_token();
If you loaded this like
Code: $this->load->library('generate_token');
Then i think you might try to
Code: $this->generate_token->validate($token);
//Please anybody correct me if i'm wrong. I'm also very new to CI but i'm trying to help as much as i can with my little knowledge of PHP
Hey friend;
Doesn't really make sense what said. I want to call generate() not validate().
I am following everything like the manual
-
stopz Junior Member
 
-
Posts: 23
Threads: 6
Joined: Jan 2015
Reputation:
3
try:
Code: $result = $this->generate_token->generate();
-
behnampmdg3 Member
  
-
Posts: 75
Threads: 33
Joined: Jan 2015
Reputation:
-1
(01-26-2015, 04:29 AM)stopz Wrote: try:
Code: $result = $this->generate_token->generate();
That is exactly what I am doing  See post #1
-
behnampmdg3 Member
  
-
Posts: 75
Threads: 33
Joined: Jan 2015
Reputation:
-1
I fixed it but I don't like the solution! I am surprised it works this way!
PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); //Custom error handling class Generate_token { public function generate() { $CI =& get_instance(); $raw_random_bytes = mcrypt_create_iv(6, MCRYPT_DEV_URANDOM); $encoded_random_bytes = base64_encode($raw_random_bytes); $token = strtr(trim($encoded_random_bytes, '='), array('+' => '-', '/' => '_')); if($CI->generate_token->validate($token)==$token) { return $token; } } public function validate($token) { $CI =& get_instance(); $sql = "SELECT id FROM url_tokens WHERE short_url = ?"; $query = $CI->db->query($sql, array($token)); if($query->num_rows==0) { return $token; } else { $CI->generate(); } } }
-
sv3tli0 PHP Dev
     
-
Posts: 421
Threads: 24
Joined: Oct 2014
Reputation:
18
01-26-2015, 04:40 AM
(This post was last modified: 01-26-2015, 04:40 AM by sv3tli0.)
Just a notice...
When you have in constructor : $CI =& get_instance();
You can not reuse this variable into methods, because it is only at constructur..
Instead make:
$this->CI =& get_instance();
And at methods replace $CI with $this->CI
-
behnampmdg3 Member
  
-
Posts: 75
Threads: 33
Joined: Jan 2015
Reputation:
-1
01-26-2015, 05:05 AM
(This post was last modified: 01-26-2015, 05:05 AM by behnampmdg3.)
(01-26-2015, 04:40 AM)sv3tli0 Wrote: Just a notice...
When you have in constructor : $CI =& get_instance();
You can not reuse this variable into methods, because it is only at constructur..
Instead make:
$this->CI =& get_instance();
And at methods replace $CI with $this->CI
Hey;
That's not what the manual says!
PHP Code: $CI =& get_instance();
$CI->load->helper('url'); $CI->load->library('session'); $CI->config->item('base_url'); etc.
-
sv3tli0 PHP Dev
     
-
Posts: 421
Threads: 24
Joined: Oct 2014
Reputation:
18
01-26-2015, 05:23 AM
(This post was last modified: 01-26-2015, 05:35 AM by sv3tli0.)
(01-26-2015, 05:05 AM)behnampmdg3 Wrote: That's not what the manual says!
PHP Code: $CI =& get_instance();
$CI->load->helper('url'); $CI->load->library('session'); $CI->config->item('base_url'); etc.
I am talking about pure PHP nothing related to CI..
Here is working example of your first post class
PHP Code: class Generate_token { public function __construct() { $this->CI =& get_instance(); } public function generate() { $raw_random_bytes = mcrypt_create_iv(6, MCRYPT_DEV_URANDOM); $encoded_random_bytes = base64_encode($raw_random_bytes); $token = strtr(trim($encoded_random_bytes, '='), array('+' => '-', '/' => '_')); if($this->validate($token)==$token) { return true; } } public function validate($token) { $sql = "SELECT id FROM url_tokens WHERE short_url = ?"; $query = $this->CI->db->query($sql, array($token)); if($query->num_rows==0) { return $token; } else { $this->generate(); } } }
When you are in Class you can call methods from the same class using $this->methodname()
When you are in class and want to call method from some class loaded with CI you can use $this->CI->classs->method name() but first inside class constructor you have to set $this->CI to get instance of CI..
|