Welcome Guest, Not a member yet? Register   Sign In
Restored old site but new errors shown
#1

I've restored our old website from a very old backup but now the site won't load up and I'm getting php errors which I've never seen before. It's like menus.php file can't see $this->options->menu_items from main.php. If I create it inside menus.php then the error goes away but no errors are shown and I have no website.

Code Igniter version: 2.1.0.
PHP: 8.0.9

The only thing I can think of is that the site was originally ran on an older version of PHP which might have broken it. I've restored my database and altered the credentials in config/database.php.

It's been a long time since I did any coding so there's so much I can't remember.

If this is in the forum please move it, thanks.

Errors
Code:
[b]Fatal error[/b]: Uncaught Error: Attempt to assign property "menu_items" on null in /home/seedeta1/public_html/application/libraries/menus.php:36 Stack trace: #0 /home/seedeta1/public_html/application/core/MY_Controller.php(9): require_once() #1 /home/seedeta1/public_html/application/controllers/main.php(8): MY_Controller->__construct() #2 /home/seedeta1/public_html/system/core/CodeIgniter.php(308): Main->__construct() #3 /home/seedeta1/public_html/index.php(202): require_once('/home/seedeta1/...') #4 {main} thrown in [b]/home/seedeta1/public_html/application/libraries/menus.php[/b] on line [b]36[/b]


Menus.php

PHP Code:
<?php
//Contruct menu bar. This is used in includes/header.php
//Method = As in controller/method/id in CI urls.

/*  Long term plan: Have a menus db table with all these in. Table will have name, uri (method), parent_id and level(1,2,3).
*  From here we can construct the menu using db queries. Grocery CRUD can then be used to manage that menus table.
*  The DB table may already exist but could need modification.
*/

$second_items=array( array('name'=>'Functional','method'=>'testing/functional'),
                    array('name'=>'Performance','method'=>'testing/performance'),
                    array('name'=>'Accessibility','method'=>'testing/accessibility'),
                    array('name'=>'Usability','method'=>'testing/usability'),
                    array('name'=>'Regression','method'=>'testing/regression'),
                    array('name'=>'Compatibility','method'=>'testing/compatibility') );

$sub_items=array( array('name'=>'Testing','method'=>'testing','sub_items'=>$second_items),
                  array('name'=>'Case Studies','method'=>'case_study'),
                  );

$services_menu = array('name'=>'Services','controller'=>'services','sub_items'=>$sub_items);

$sub_items=array( array('name'=>'Tools','method'=>'tools'),
                  array('name'=>'Cloud','method'=>'cloud'),
                  array('name'=>'Blog','method'=>'blog') );

$technology_menu = array('name'=>'Technology','controller'=>'technology','sub_items'=>$sub_items);

$sub_items=array( array('name'=>'About','method'=>'about'),
                  array('name'=>'Contact','method'=>'contact'),
                  array('name'=>'Careers','method'=>'careers'),
  array('name'=>'Team','method'=>'team') );
  
$company_menu 
=  array('name'=>'Company','controller'=>'company','sub_items'=>$sub_items); 

$this->options->menu_items= array($services_menu,$technology_menu,$company_menu);

$bullet_class=''
$sub_bullet_class=''

Line 36 begins: $this->options->menu_items near the end of the file.

My_Controller:

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
MY_Controller extends CI_Controller
{
var 
$options;
public function 
__construct()
{
parent::__construct();
        require_once('application/libraries/menus.php');
        date_default_timezone_set('Europe/London');
    
$this
->options->page_title='Home page';

$this->options->company $this->company_model->get_company_details(array('c_id'=>1));//Get company details such as address / telephone plus jquery slider options.
}

  /** 
    * _not_selectable 

* @access public 
* @param array values to print. 
* @return true.
*/

  //Not selectable is useful when there are values in a select which cannot be selected as valid option. Second param is a piped string of primary key ids from the task_types db table. $option param = Value from select.
  function _not_selectable($option,$values)
  {
if( ! isset(
$option) )
return 
true;
  
    $not_selectable 
explode('|',$values);
    
    
if(in_array($option,$not_selectable))//Is the value invalid?
    {
      $this->form_validation->set_message('_not_selectable''You selected an invalid option.');
      return false;
    }
    return true;
  }

/** 
* _Valid_Date_Format 

* @access public 
* @param array values to print. 
* @return true.
*/ 
    
  
//Check date format aggainst mysql format, valid dates only, no 30-2-2010.
  function _Valid_Date_Format($date)
  {
if( empty(
$date))
return 
true;  
  
    $converted
=str_replace('/','-',$date);

    if(preg_match("/^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30)))))$/",$converted)===1)
    {
      return TRUE;
    }

    $this->form_validation->set_message('_Valid_Date_Format''The Date entered is invalid.');
    return FALSE;
  }

//Check that end date is after start date.
function _check_start_end_date($start_date,$end_date)
{
        if( empty($start_date))
return 
true;
        
if(strtotime($end_date) > strtotime($start_date))
{
  return true;
}
$this->form_validation->set_message('_check_start_end_date''The start date must be before the end date.');
return 
false;
}

//Callback function
function _valid_email($userEmail)
{
if( empty(
$userEmail))
return 
true;

if(
$this->input->post('email'))
{
$user $this->user_model->GetUsers(array('userEmail' => $userEmail));

if(isset(
$user->userEmail))
{
$this->form_validation->set_message('_valid_email''Your email address is already taken.');
  return false;
}
}
return 
true;//true because no email = good result.
}

//Callback to check username choice aggainst db.
function _valid_username($userName)
{
if(
$this->input->post('username'))
{
$user $this->user_model->GetUsers(array('userName' => $userName));

if(isset(
$user->userName))
{
  $this->form_validation->set_message('_valid_username''Your username is already taken, please choose another.');
  return false;
}
}
return 
true;//true because no email = good result.
}

//Check that passwords match
function _password_match($pass,$pass2)
{
if(
$pass==$this->input->post($pass2))
return 
true;

$this->form_validation->set_message('_password_match''The two passwords must match.');
return 
false;
}
  

//Callback function
function _check_login($userEmail)
{
if(
$this->input->post('password'))
{
$user $this->user_model->get_users(array('userEmail' => $this->input->post('email'), 
'userPassword' => $this->input->post('password')));

if(
$user) return true;
}

$this->form_validation->set_message('_check_login''Your email / password combination is invalid.');
return 
false;
}

function 
_check_email($userEmail)
{
if(
$this->input->post('email'))
{
$user $this->user_model->get_users( array('userEmail' => $this->input->post('email') ));

if(
$user) return true;
}

$this->form_validation->set_message('_check_email''The email entered does not exist in our database.');
return 
false;
}

//Callback to check username choice aggainst db.
function _is_financial($price)
{
if( empty(
$price))
return 
true

if( 
preg_match("/^\d[\d\,\.]+$/",$price) === )
{
  return TRUE;
}
$this->form_validation->set_message('_is_financial''Your price value is invalid.');
return 
FALSE;
}
    
    
/*
    * _project_user_allocation_check checks to see if there is already a link between a project and user. 
    * If there is, return false.
    */
    
    
function _project_user_allocation_check$p_id$u_id )
    {    
        $pu 
$this->project_users_model->get_project_users( array( 'u_id'=>$u_id'p_id'=>$p_id ) );
        
        
if( $pu['num_rows'] > ):
            $this->form_validation->set_message('_project_user_allocation_check''There is already a link between that project and user. Change the user or project to continue or edit the existing project - user allocation.');
            return FALSE;
        endif;
        
        
return TRUE;
    }    
    
  
function email_sent()
{
$this->options->page_title='Email complete';
$this->options->meta_desc 'Email complete';
$this->options->meta_keywords 'Email complete';
$this->load->view('pages/email_sent.php',$this->options);
}

function 
password_reset_complete()
{
$this->options->page_title='Password reset complete';
$this->options->meta_desc 'Password reset complete';
$this->options->meta_keywords 'Password reset complete';
$this->load->view('login/password_reset_view.php',$this->options);
}
  
  
}

/* End of file MY_controller.php */
/* Location: ./application/core/MY_controller.php */ 

Main.php:

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Main extends MY_Controller
{
 var 
$options;
 public function 
__construct()
 {
 
parent::__construct();
    
 $this
->options->page_title='Home page';
 }
  
    
//Functions inherited from core/MY_Controller
    function email_sent() {parent::email_sent();}
    function password_reset_complete() {parent::password_reset_complete();}

 function 
index()
 {
        $this->load->model('post_model');
        
        $this
->load->model('images_model');
        $this->options->images $this->images_model->get_images(array( 'page_id'=>));//Images for jquery slider, 1 for every feed.
          
 $this
->options->pages $this->page_model->get_page( array('uri_name'=>'homepage') );
        
        $this
->load->model('company_model');
        $this->options->company $this->company_model->get_company_details();//Images for jquery slider, 1 for every feed.
        
        
//Load view.
 
$this->load->view('main/main_view',$this->options);
 }
}

/* End of file main.php */
/* Location: ./application/controllers/main.php */ 
Reply
#2

I’m pretty sure CI 2.x is only compatible with PHP 5.6 or lower. I would be surprised if it worked with PHP 7 or 8. Upgrading to CI 4 is mostly a rewrite of your website. But if you don’t want to do this a the moment, you can at least upgrade your website to CI 3.x and run it with PHP 7. There’s not a lot of breaking changes between version 2 and 3. I’m not sure if it will work with PHP 8. It’s an old version that don’t gets updated anymore. But it work well with PHP 7.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#3

(09-06-2021, 02:48 PM)includebeer Wrote: I’m pretty sure CI 2.x is only compatible with PHP 5.6 or lower. I would be surprised if it worked with PHP 7 or 8. Upgrading to CI 4 is mostly a rewrite of your website. But if you don’t want to do this a the moment, you can at least upgrade your website to CI 3.x and run it with PHP 7. There’s not a lot of breaking changes between version 2 and 3. I’m not sure if it will work with PHP 8. It’s an old version that don’t gets updated anymore. But it work well with PHP 7.

That's good information. I might be able to request PHP 5.6 from the web host, or like you say recode it in CI 3 instead. The strange thing is that we were using this website only last year prior to loosing it due to someone not paying the bills. I don't think it was running an old version of PHP.
Reply
#4

It's not compatible with php 8.+ you will need to re-write it in CodeIgniter 4.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 09-08-2021, 05:53 AM by Inquisitor. Edit Reason: removed spacing )

(09-06-2021, 02:48 PM)includebeer Wrote: I’m pretty sure CI 2.x is only compatible with PHP 5.6 or lower. I would be surprised if it worked with PHP 7 or 8. Upgrading to CI 4 is mostly a rewrite of your website. But if you don’t want to do this a the moment, you can at least upgrade your website to CI 3.x and run it with PHP 7. There’s not a lot of breaking changes between version 2 and 3. I’m not sure if it will work with PHP 8. It’s an old version that don’t gets updated anymore. But it work well with PHP 7.

So I've done my upgrade. I read absolutely everything from the upgrade pages especially the 3.0 page: https://codeigniter.com/userguide3/insta...e_300.html
Nothing skipped or left out so I'm positive that I've done the upgrade correctly.

But I'm still getting the same problem where variables passed into the views cannot be seen. I'm not sure about the simplepie error, I might just remove that code for now, but I'm more interested in the menus.php problem and the error in core/Common.php:

Code:
A PHP Error was encountered

Severity: Warning

Message: Creating default object from empty value

Filename: libraries/Menus.php

Line Number: 36
...
Code:
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/seedeta1/public_html/ci3/system/core/Exceptions.php:271)

Filename: core/Common.php

Line Number: 570
...
Code:
An uncaught Exception was encountered

Type: ParseError

Message: syntax error, unexpected 'new' (T_NEW)

Filename: /home/seedeta1/public_html/ci3/application/libraries/Simplepie.php

Line Number: 738

Here is the beginning of my MY_Controller file:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
MY_Controller extends CI_Controller
{
 var 
$options;
 public function 
__construct()
 {
 
parent::__construct();
                require_once('application/libraries/Menus.php'); //Important, imported here.
                date_default_timezone_set('Europe/London');
 
 
$this->options->page_title='Home page';
 
 
$this->options->company $this->Company_model->get_company_details(array('c_id'=>1));//Get company details such as address / telephone plus jquery slider options.
 
}
....... 

So $options has been created and some properties applied. This code used to work fine.

The menus.php has been imported too.

It's still called CI_Controller in CI 3.0+ isn't it?

The menus file where I'm getting the error. Line 36 = the end of the file:

PHP Code:
<?php
//Contruct menu bar. This is used in includes/header.php
//Method = As in controller/method/id in CI urls.

$second_items=array( array('name'=>'Functional','method'=>'testing/functional'),
                    array('name'=>'Performance','method'=>'testing/performance'),
                    array('name'=>'Accessibility','method'=>'testing/accessibility'),
                    array('name'=>'Usability','method'=>'testing/usability'),
                    array('name'=>'Regression','method'=>'testing/regression'),
                    array('name'=>'Compatibility','method'=>'testing/compatibility') );

$sub_items=array( array('name'=>'Testing','method'=>'testing','sub_items'=>$second_items),
                  array('name'=>'Case Studies','method'=>'case_study'),
                  );

$services_menu = array('name'=>'Services','controller'=>'services','sub_items'=>$sub_items);

$sub_items=array( array('name'=>'Tools','method'=>'tools'),
                  array('name'=>'Cloud','method'=>'cloud'),
                  array('name'=>'Blog','method'=>'blog') );

$technology_menu = array('name'=>'Technology','controller'=>'technology','sub_items'=>$sub_items);

$sub_items=array( array('name'=>'About','method'=>'about'),
                  array('name'=>'Contact','method'=>'contact'),
                  array('name'=>'Careers','method'=>'careers'),
 
  array('name'=>'Team','method'=>'team') );
  
$company_menu 
=  array('name'=>'Company','controller'=>'company','sub_items'=>$sub_items); 

$this->options->menu_items= array($services_menu,$technology_menu,$company_menu);

$bullet_class=''
$sub_bullet_class=''

Now my main controller. This is where my processing starts when on the homepage. "var $options" is created here and passed into the view:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Main extends MY_Controller
{
 var 
$options;
 public function 
__construct()
 {
 
parent::__construct();
    
 $this
->options->page_title='Home page';
 }
  
    
//Functions inherited from core/MY_Controller
    function email_sent() {parent::email_sent();}
    function password_reset_complete() {parent::password_reset_complete();}
 
 function 
index()
 {
        $this->load->model('Post_model');
        
        $this
->load->model('Images_model');
        $this->options->images $this->Images_model->get_images(array( 'page_id'=>));//Images for jquery slider, 1 for every feed.
          
 $this
->options->pages $this->Page_model->get_page( array('uri_name'=>'homepage') );
        
        $this
->load->model('Company_model');
        $this->options->company $this->Company_model->get_company_details();//Images for jquery slider, 1 for every feed.
        
        
//Load view.
 
$this->load->view('main/main_view',$this->options);
 }
}

/* End of file main.php */
/* Location: ./application/controllers/main.php */ 

So I'm not sure why menus.php can't see $options when it's created right before it in MY_Controller? Maybe it's some sort of scope issue. As mentioned it used to work fine before restoring the backup.

Now downgraded to PHP 7.4.22.
Reply
#6

I believe this explanation can help you:
https://stackoverflow.com/a/32855849
Anyway, you can use this library to check php compatibility in various versions:
https://github.com/PHPCompatibility/PHPCompatibility
Reply
#7

(09-08-2021, 07:42 PM)kleber Wrote: I believe this explanation can help you:
https://stackoverflow.com/a/32855849
Anyway, you can use this library to check php compatibility in various versions:
https://github.com/PHPCompatibility/PHPCompatibility
Will read this later, thanks.
Reply
#8

Your missing the parent__construct(); in your My_Controller.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

(09-09-2021, 02:06 AM)InsiteFX Wrote: Your missing the parent__construct(); in your My_Controller.

No the parent::__construct(); is shown inside "public function __construct()".
Reply
#10

(This post was last modified: 09-10-2021, 02:40 PM by includebeer.)

I'm not sure why it worked before and now it's not. But it's not good practice to access a variable defined in another file and taking for granted that the variable will exist and be accessible. There may be a setting in php.ini that prevent this behavior and keep the scope to a single file?

But anyway, loading a library with require_once in the constructor, then trying to access $this->options is very bad coding. It would be best to properly load the library, then change this code to a function that return a value and assign that value to $options in MY_Controller class.

...in fact, this may be in the "libraries" folder, but it's really a configuration file and not a library. This look like it was hacked together by someone that knows nothing about CodeIgniter. You better fix this nonsense the right way or you'll end up with a lot of weird bug like this.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB