Welcome Guest, Not a member yet? Register   Sign In
Sessions - Bug?
#1

[eluser]The Wizard[/eluser]
Hello FriendsSmile

I've discovered this lately,
if you try to use sessions from within a Model,
it can fail sometimes.
So try to do the session checking from within
a controller
.

I hope it helps Smile
#2

[eluser]majidmx[/eluser]
Hi herrkaleun,
could you please provide a sample code which has this failure ? And also the error message you get.
Because it has never happened to me and seems strange.

Take care,
MajiD Fatemian
#3

[eluser]Jonathon Hill[/eluser]
Perhaps the session library has not been initialized yet when you try to access session data in your model?
#4

[eluser]darkhouse[/eluser]
sessions are something my projects use constantly, i found it smart to just autoload the session library. I've never had an issue like that you're describing.
#5

[eluser]The Wizard[/eluser]
no it was always initialized Sad

strange issue.
#6

[eluser]darkhouse[/eluser]
Well, assuming you're loading your models from the controller, and you're autoloading your session library, there's no reason the session data would be available in the controller but not the model.

However, I was thinking about this, and I looked through a bunch of my projects. Rarely have I ever used session data directly in a model. I almost always call my model methods from my controllers like this...
Code:
$someresult = $this->some_model->get_something($this->session->userdata('somevar'));

The only exception I found was some crazy ajax stuff I was doing (which could probably be rewritten to keep session data out of the model). In general, I don't think you should be using session data directly in a model, but that's just my opinion.
#7

[eluser]The Wizard[/eluser]
yeah i later thought that too, cause a very similar function was using session functions from the controller and it was working flawlessly.

now i use it from controller, and its working perfectSmile
#8

[eluser]majidmx[/eluser]
But it should not be the case,
Session Library is like any other library which you can use anywhere in your code as long as it's loaded.
Can you send a piece of code which has the problem ?

Thanks,
MajiD
#9

[eluser]amw_drizz[/eluser]
~Gah Rips hair out of head~

I have this issue!! Worked fine in CI 1.6.2 (before I updated straight to 1.7.0)

This is part of my login script This USED TO WORK but since I updated to 1.7.0 I have not been able to get it to work. I have even tried removing ALL output to the browser and I still get the same error. I am going to convert back to 1.6.2 since that did work. Hope that helps for code samples of causing an error

Error
Code:
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at D:\WebServer\htdocs\xterm\system\application\main_site\controllers\auth.php:18)

Filename: libraries/Session.php

Line Number: 662

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

class User_Control {
    function __construct(){
        $this->load->database();    
        $this->load->library('parser');
        $this->load->helper('url');
        $this->load->library('session');
        $this->load->library('user_agent');
        $this->load->library('validation');
        $this->load->library('encrypt');
        $this->load->library('email');
        //$this->load->library('themechooser');
        $this->load->helper(array('form', 'url'));
    }
    // Auth Segments
    function doLogin(){
        $rules['username'] = "callback_isreg|required|xss_clean";
        $rules['password'] = "required";
        $fields['username'] = "Username";
        $fields['password'] = "Password";
        $this->validation->set_fields($fields);
        $this->validation->set_rules($rules);
        $this->validation->set_error_delimiters('<div id="error" class="error">', '</div>');
        if($this->validation->run() == FALSE ){
            $this->load->view('auth/login');
        }
        else{
            $this->VerifyAuth();
        }
    }
    function VerifyAuth(){
        $post_array = array(
            'username' => $this->input->post('username', TRUE),
            'password' => md5($this->input->post('password')),
        );
        $query = $this->db->query("SELECT * FROM users WHERE username='". $post_array['username'] ."'");
        $row = $query->row_array();
        if($row['password'] !== $post_array['password']){
            /*
            This is/was for debugging to find out why passwords were not set right
            if($this->config->item('debug') == TRUE){
                echo "PWD in DB = ". $row['password'] ."\n";
                echo "PWD Received is ". $post_array['password'] ."\n";
            }
            else{ */
                //show_error('Incorrect Password Try again');
                $data['strike'] = 0;
                $data['limit'] = 0;
                echo $this->parser->parse("auth/invalid",$data,true);
            // }
        }
        elseif($row['active'] == 'no'){
            show_error('Your account is currently disabled,  Please contact the site Admin by <a href="'. site_url('messaging/unreg/admin') .'">Clicking here</a>');
        }
        else{
            $logged_array = array(
                            "username" => $row['username'],
                            "lvl" => $row['lvl'],
                            'uid' => $row['id'],
                            'loggedin' => TRUE,
                            );
            $this->session->set_userdata($logged_array);
            redirect('/members', 'refresh');
        }
    }

Maybe I am too stupid to get it working on 1.6.2 as well. But I do know it did work, but I am reworking the templating system on my site and it died on me.
#10

[eluser]Tom Schlick[/eluser]
you should definatly autoload the database and session libraries (in that order) before anything else. since i have done so i have not had a problem at all with either.

also im pretty sure that instead of having a bunch of lines with
Code:
$this->load->library('parser');
$this->load->library('session');
$this->load->library('user_agent');
$this->load->library('validation');
$this->load->library('encrypt');
$this->load->library('email');

you should use

Code:
$this->load->library(array('parser','session','user_agent','validation','encrypt','email'));

just something to consider




Theme © iAndrew 2016 - Forum software by © MyBB