Welcome Guest, Not a member yet? Register   Sign In
Hooks doesn't works on private hosting
#1

[eluser]viher[/eluser]
Hello everyone,

I have an application made with CodeIgniter 2, this app works perfectly in my localhost, but when i have uploaded it into my private server (production server), i have the following errors:

Quote:A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: controllers/home.php

Line Number: 15
A PHP Error was encountered

Severity: Warning

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

Filename: libraries/Session.php

Line Number: 675
A PHP Error was encountered

Severity: Warning

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

Filename: helpers/url_helper.php

Line Number: 542

So i thought my problems come from the check login hook.

The hook code:

app/config/hooks.php

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files.  Please see the user guide for info:
|
| http://codeigniter.com/user_guide/general/hooks.html
|
*/

$hook['post_controller_constructor'][] = array(
                                        'class'    => 'user_control_hook',
                                        'function' => 'check_login',
                                        'filename' => 'user_control_hook.php',
                                        'filepath' => 'hooks'
                                        );

$hook['post_controller_constructor'][] = array(
                                        'class'    => 'user_control_hook',
                                        'function' => 'languages',
                                        'filename' => 'user_control_hook.php',
                                        'filepath' => 'hooks'
                                        );
/* End of file hooks.php */
/* Location: ./application/config/hooks.php */

app/hooks/user_control_hook.php

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

    class user_control_hook
    {
        
        private $exec = true;
        private $dontExec = array('Activation'); //Controllers where check_login is disabled.
                
        function __construct() {
            $this->ci =& get_instance();
            $this->ci->load->library("user_control");
            
            $ci_name = get_class($this->ci);
            if(in_array($ci_name,$this->dontExec))
                $this->exec = false;
        }
        
        /*
         * Checks if user is logged.
         */
        public function check_login()
        {
            if($this->exec)
                $this->ci->user_control->isLoggedRedirect();
        }
        
        /*
         * Auto load the application language.
         */
        public function languages()
        {
            $this->ci->load->library("lang_control");
            $this->ci->lang_control->load();
            $this->ci->lang_control->load_lang_files(array('buttons','strings'));
        }
                
    }

?>

Which could be the problem?
Thank you!
#2

[eluser]Tpojka[/eluser]
Seems that problem starts in home controller?
#3

[eluser]viher[/eluser]
[quote author="Tpojka" date="1387755937"]Seems that problem starts in home controller?[/quote]

controllers/home.php

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

class home extends CI_Controller {
    
        var $user;
        var $user_groups;
        var $user_perm;
        var $languages_list;
    
        function __construct()
        {
            parent::__construct();
            //user data
            $this->user = $this->session->userdata("user_data");
            $this->user_groups = $this->user_control->get_user_groups($this->user['ID']);  //LINE 15, WHEN ERROR DISPLAYS
            $this->user_perm = $this->user_control->get_group_perm($this->user_groups);
            //isAdmin
            $this->isAdmin = $this->user_control->isAdmin($this->user);
            //Languages list
            $this->languages_list = $this->lang_control->get_lang_list();
        }

The get_user_groups() method in libraries/User_control.php is a simple query to database:

Code:
/*
         * Gets the groups which have relationship with the user.
         * Return @array.
         */
        public function get_user_groups($id_user)
        {
            $this->ci->db->select('A.FK_USER, A.FK_GROUP, B.FK_CATEGORY, B.ACCESS, B.EDIT, B.DEL');
            $this->ci->db->from('TDR_GROUPS_REL_USERS A');
            $this->ci->db->join('TDR_GROUPS_REL_CATEGORIES B','A.FK_GROUP = B.FK_GROUP','left');
            $this->ci->db->where('A.FK_USER',$id_user);
            $sql = $this->ci->db->get();
            return $sql->result_array();
        }

Like i said in first post, this works fine on my localhost, and i have the same class construct in other application controllers.

Thank you!
#4

[eluser]Karman de Lange[/eluser]
You referencing a Array element that does not exist (row 15)

Make sure by using the profiller that you actually have session data or do
Code:
var_dump($this->session->userdata("user_data")
before line 15 and check if anything is returned

It could be that your login system don't work properly on production (perms issue, cookies conflicts etc) and your session data returns null which then means your $this->user won't have any elemnts

L:

#5

[eluser]Tpojka[/eluser]
Also,([I don't know if matter this time, but]) you should name your controller with capital letter.
Code:
class Home extends CI_Controller {
#6

[eluser]viher[/eluser]
I tried what you said, but i have find where is the problem, but not the solution.

Like i said on first post, the problem is in the Hook.

Why?. Because when you enter in the website, the Hook is executed before controller is loaded, then if the user is logged (checks the user session data) the system redirects you to the main page, if not, the system redirects you to the login page.

But in production server, if you are not logged, the system prints the error that i wrote in the first message and he doesn't redirects you to the login page.

If you access to the login page directly, the system works fine:

Now when you are logged and you access to the main page, the hook redirects you correctly.

What can i do?

Thank you so much!
#7

[eluser]Tpojka[/eluser]
Declare
Code:
private $ci;
in
class user_control_hook.

// just thinking loud
#8

[eluser]Karman de Lange[/eluser]
Can you try following:

Change :
Code:
$this->user_groups = $this->user_control->get_user_groups($this->user['ID']);
to:
Code:
$this->user_groups = isset($this->user['ID']) ? $this->user_control->get_user_groups($this->user['ID']):null ;
#9

[eluser]viher[/eluser]
[quote author="Karman de Lange" date="1387802311"]Can you try following:

Change :
Code:
$this->user_groups = $this->user_control->get_user_groups($this->user['ID']);
to:
Code:
$this->user_groups = isset($this->user['ID']) ? $this->user_control->get_user_groups($this->user['ID']):null ;
[/quote]

Wow! With this change it works fine!

Thank you so much! And Tpojka too!
#10

[eluser]Karman de Lange[/eluser]
Yup, Welcome to errors and php ;-)

What happening is (my understanding of it): My redirect sets the browser headers with a 301, but now you getting a php error that also trying to set errors (error 500 or something) so you end up with headers already set error (Might be happening the other way around).

So what the line now does is : Check if the user array have the element "id" , if so, then get the groups, otherwise just assign null to the $this->user_groups.

L:




Theme © iAndrew 2016 - Forum software by © MyBB