CodeIgniter Forums
Object persistence effect (kind of...), don't know exactly how is the PHP behavior on this - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Object persistence effect (kind of...), don't know exactly how is the PHP behavior on this (/showthread.php?tid=11077)

Object persistence effect (kind of...), don't know exactly how is the PHP behavior on this - El Forum - 08-25-2008


Here are the view and the controller for a simple login form processing.

I usually split the form processing controller in the following parts/methods:

· ACTION (no action in the posted snippet)

Yes, I could load the validation library in the constructor in order to avoid the following problem... but I'd like to know why this is happening.

Don't know if it is related to some php configuration directive, my development environment, my OS, or something more evident... no clue whatsoever.


OK If I call the DISPLAY method pointing the browser to http://domain.tld/login and then the VALIDATION method, via the form submit button (form action set to http://domain.tld/login/validar_usuario_login) all is executed properly. That is, the validar_usuario_login method is able to access the validation object that was created on the previous request.

ERROR However if I let a large period of inactivity (About 2 hours or so. I haven't been able to measure it yet.) between the initial call to the DISPLAY method and the form submission VALIDATION method the php parser won't be able to locate the validation object and will throw an error:

Fatal error: Call to a member function on a non-object in H:\apachefriends\xampp\htdocs\system\application\controllers\login.php on line 75

The line 75 in the source code refers to the validation object $this->validation->set_rules($rules);


How long are the objects stored by php? I thought that they were cleared after the end of the script execution.





<!-- >>> FORMULARIO usuario_login -->

<?php if( $this->validation->error_string ): ?>
    &lt;?php echo $this->validation->error_string; ?&gt;
&lt;?php endif; ?&gt;

&lt;form method="post" action="&lt;?php echo site_url('login/validar_usuario_login'); ?&gt;"&gt;
    <label for="email">email:</label>
    &lt;?php echo form_input('email', $email, 'maxlength="128"'); ?&gt;
    <br />
    <label for="clave">clave:</label>
    &lt;?php echo form_password('clave', $clave, 'maxlength="8"'); ?&gt;
    <br />
    &lt;input type="submit" name="usuario_login" id="usuario_login" value="Entrar"&gt;

&lt;!-- <<&lt; FORMULARIO usuario_login --&gt;




class Login extends Controller {

    function Login()
    function index()
    // >>> FORMULARIO usuario_login
    function usuario()
        if( $this->input->post('usuario_login') )
            //Se han validado los datos, al menos una vez
            $data['email'] = $this->validation->email;
            $data['clave'] = $this->validation->clave;
            $data['email'] = '';
            $data['clave'] = '';
        $this->load->view('login', $data);
    function validar_usuario_login()
        if( $this->input->post('usuario_login') )
            //Reglas validación
            $rules['email'] = "trim|required|valid_email|max_length[128]";
            $rules['clave'] = "trim|required|alpha_numeric|max_length[8]|callback__usuario_login";
            //Delimitadores de errores
            $this->validation->set_error_delimiters('<li>', '</li>');
            //Repoblar campos (repopulate)
            $fields['email'] = "EMAIL";
            $fields['clave'] = "CLAVE";
            //Ejecutamos validación
            if($this->validation->run() == TRUE)
                //redirect('somewhere', 'location'); or call $this->usuario_some_action();
            //Si no se ha enviado el formulario lo mostramos vacío
            redirect('login', 'location');
    //Not accesible via the URL |
    //Preferably in models |
    function _usuario_login($clave)
        $email = $this->validation->email;
        if ( $this->auth->try_login( array('email'=>$email,'clave'=>$clave) ) )
            return TRUE;
            $this->validation->set_message('_usuario_login', 'No existe ningún usuario con el email y clave especificada.');
            return FALSE;

    // <<&lt; FORMULARIO usuario_login