Welcome Guest, Not a member yet? Register   Sign In
Trouble with custom library, results in blank page being rendered.
#1

[eluser]DavidHopkins[/eluser]
Hello all.

Firstly i am new to CodeIgniter and after watching a fair few tutorial videos am trying to create my own application. I am a capable PHP coder but just having a few teething problems with CI.

I am trying to create a custom library where i can chuck a load of functions that i intend on using throughout my program.

My code looks like this:
Filename - CoreFunctions.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CoreFunctions {

public function  __construct() {
  parent::CI_Session();
  
  $CI =& get_instance();
  $CI->load->helper('url');
}


    public function checkLoginAccess() {
    
  if($this->session->userdata('logged_in')) {
   echo "You are logged in";
  }
  else {
   redirect('login');
  }
    }


}
?>

I then load this libary in the autoload function.

When i then refresh my page i get a white page no matter what. But if i remove the construct it is fine but the function doesnt work.

Any help would be great Smile

Thanks

David Hopkins
#2

[eluser]Sire[/eluser]
Your class doesn't extend from another class, but has a "parent::CI_Session();" in the __construct.
#3

[eluser]DavidHopkins[/eluser]
Thank you for the reply Smile

I have now changed my construct to this

Code:
public function __construct() {
  
  $this->CI =& get_instance();
  $this->CI->load->helper('url');
  $this->CI->load->helper('session');
}

However now when i go to the page that calls a function in this class i get the following error

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: CoreFunctions::$session

Filename: libraries/CoreFunctions.php

Line Number: 18

But if i take the load helper session out i get this error

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: CoreFunctions::$session

Filename: libraries/CoreFunctions.php

Line Number: 18

Thanks

David Hopkins
#4

[eluser]Sire[/eluser]
I believe session is a library, not helper. $this->CI->load->library('session') ?
#5

[eluser]DavidHopkins[/eluser]
Thanks for your replies. All sorted now Smile

Just a little bit off topic im also having some troubles with session data. Could you take a little look at this please?

My function looks like this

Code:
function checkLogin() {
  
  $this->load->library('form_validation');
  $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|xxs_clean');
  $this->form_validation->set_rules('password', 'Password', 'trim|required|xxs_clean');
  
  extract($_POST);
  
  if($this->form_validation->run()==False) {
   $this->session->set_flashdata('login_error_email', $email);
   $this->load->view('login_view');
  }
  else {
   $fname = $this->Login_model->processLogin($email, $password);
  
   if(! $fname) {
    $this->session->set_flashdata('login_error', TRUE);
    $this->session->set_flashdata('login_error_email', $email);
    redirect('login');
   }
   else {
    $this->session->set_userdata('loggedIn', TRUE);
    $this->session->set_userdata('fname', $fname);
    redirect('login/main');
   }
  }
  
}
The session is not bringing back data as i would expect. When the validation is not correct. I can output the error message on my page fine. However the $this->session->set_flashdata('login_error_email', $email); session dosent output anything on my page. However if i change $this->load->view('login_view'); to redirect('login'); the session shows the data, but the form validaiton messages dont appear then.

Any suggestions ?

Many Thanks

David Hopkins




Theme © iAndrew 2016 - Forum software by © MyBB