Welcome Guest, Not a member yet? Register   Sign In
Object persistence effect (kind of...), don't know exactly how is the PHP behavior on this
#1

[eluser]janogarcia[/eluser]
Hi,

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:

· DISPLAY
· VALIDATION
· 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.


THE PROBLEM:

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:

Code:
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);

THE QUESTION:

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

THE CODE:

VIEW

Code:
<html>
<head>
<title>Login</title>
</head>

<body>

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

<?php if( $this->validation->error_string ): ?>
<ul>
    &lt;?php echo $this->validation->error_string; ?&gt;
</ul>
&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;/form&gt;

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

&lt;/body>
&lt;/html&gt;

CONTROLLER

Code:
&lt;?php

class Login extends Controller {

    function Login()
    {
        parent::Controller();
        
        $this->load->library('debug');
    }
    
    function index()
    {
        $this->usuario();
    }
    
    // >>> FORMULARIO usuario_login
    
    //DISPLAY
    function usuario()
    {
        $this->load->library('validation');        
        
        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;
        }
        else
        {
            $data['email'] = '';
            $data['clave'] = '';
        }
        
        $this->load->view('login', $data);
    }
    
    //VALIDATION
    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";
            $this->validation->set_rules($rules);
            
            //Delimitadores de errores
            $this->validation->set_error_delimiters('<li>', '</li>');
            
            //Repoblar campos (repopulate)
            $fields['email'] = "EMAIL";
            $fields['clave'] = "CLAVE";
            $this->validation->set_fields($fields);
            
            //Ejecutamos validación
            if($this->validation->run() == TRUE)
            {
                //redirect('somewhere', 'location'); or call $this->usuario_some_action();
            }
            else
            {
                $this->usuario();
            }
        }
        else
        {
            //Si no se ha enviado el formulario lo mostramos vacío
            redirect('login', 'location');
        }
    }
    
    //VALIDATION CALLBACKS
    //
    //Not accesible via the URL | http://ellislab.com/codeigniter/user-guide/general/controllers.html#private
    //Preferably in models | http://codeigniter.com/wiki/MY_Validation_-_Callbacks_into_Models
    function _usuario_login($clave)
    {
        $email = $this->validation->email;
        
        if ( $this->auth->try_login( array('email'=>$email,'clave'=>$clave) ) )
        {
            return TRUE;
        }
        else
        {
            $this->validation->set_message('_usuario_login', 'No existe ningún usuario con el email y clave especificada.');
            return FALSE;
        }
    }

    //ACTION
    
    // <<&lt; FORMULARIO usuario_login
}
?&gt;

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB