2013-05-30 87 views
1

我有一个提交功能,但我试图弄清楚为什么我扁平化提交功能,并做出更小的功能,为什么我得到一个无限循环后jquery ajax调用提交时函数被调用。展平代码创建无限循环

public function form_is_valid() 
{ 
    /* Set validation rules for post data */ 
    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[6]|max_length[12]|regex_match[/[a-z0-9]/]'); 
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[6]|max_length[12]|regex_match[/[a-z0-9]/]'); 
    $this->form_validation->set_rules('remember', 'Remember Me', 'trim|xss_clean|integer'); 

    /* Form validation passed */ 
    return $this->form_validation->run(); 
} 

public function is_user_locked($user_data) 
{ 
    if ($user_data->lock_date !== '0000-00-00 00:00:00') 
    { 
    /* User is locked out */ 

    if (strtotime(gmdate('Y-m-d H:i:s', time())) < strtotime($user_data->lock_date)) 
    { 
    /* User is still locked out */ 
    return TRUE; 
    } 
    else 
    { 
    /* User is no longer locked out */ 
    return FALSE; 
    } 

    } 
} 

public function check_user_status($user_data) 
{ 
    /* Match user status */ 
    switch ($user_data->user_status_id) 
    { 
    case 1: 
    $this->output('Sorry you must verify your account before logging in!', 'Account Unverified', 'Error'); 
    break; 
    case 3: 
    $this->output('Your account has been suspended!', 'Account Suspended', 'Error'); 
    break; 
    case 4: 
    $this->output('Your account has been suspended!', 'Account Banned', 'Error'); 
    break; 
    case 5: 
    $this->output('Your account has been deleted!', 'Account Deleted', 'Error'); 
    break; 
    default: 
    return; 
    } 
} 

public function output($message, $title, $status = 'Success') 
{ 
    switch ($status) 
    { 
    case 'Error': 
    array('status' => 'Error'); 
    break; 
    case 'Notice': 
    array('status' => 'Notice'); 
    break; 
    case 'Success': 
    array('status' => 'Success'); 
    break; 
    } 
    echo json_encode($status, $title, $message); 
} 

public function start_user_session() 
{ 
    /* Start session with user id and clear previous failed login attempts */ 
    $this->session->set_userdata('uid', $user_data->user_id); 
    $this->session->unset_userdata('failed_logins'); 
    $this->users_model->insert_session($user_data->user_id, gmdate('Y-m-d H:i:s', time())); 
    return; 
} 

public function submit($post_username = NULL, $post_password = NULL) 
{ 
    if (!$this->form_is_valid()) 
    { 
    echo 'test'; 
    die(); 
    $this->output('The form did not validate successfully!', 'Form Not Validated', 'Error'); 
    } 

    /* Post values from login form */ 
    $post_username = $this->input->post('username'); 
    $post_password = $this->input->post('password'); 

    /* Test to see value of posted login form */ 
    //echo '<pre>'; 
    //var_dump($post_username); 
    //var_dump($post_password); 
    //echo '</pre>'; 
    //die(); 

    /* Get user data from post username value */ 
    $user_data = $this->users_model->get_by('username', $post_username); 

    /* Test to see value of $user_data */ 
    //echo '<pre>'; 
    //var_dump($user_data); 
    //echo '</pre>'; 
    //die(); 

    if (count($user_data) == 0) 
    { 
    /* User was not found in database */ 
    $this->output('The user was not found in the database!', 'User Not Found', 'Error'); 
    } 

    /* User was found in database */ 

    if ($this->is_user_locked($user_data->lock_date)) 
    { 
    /* User is locked from logging in from too many failed attempts */ 
    $this->output('This user account is currently locked!', 'Account Locked', 'Error'); 
    } 
    else 
    { 
    /* User can be unlocked and form be resubmitted */ 
    $this->users_model->unlock_user($user_data->user_id); 
    $this->submit($post_username, $post_password); 
    return FALSE; 
    } 

    /* User is unlocked from logging in */ 

    if ($user_data->user_status_id != 2) 
    { 
    /* User has a status that is not allowed to proceed */ 
    $this->user_status_message($user_data->user_status_id); 
    } 

    /* User is registered and validated */ 

    $regenerated_post_password = $this->genfunc->reGenPassHash($post_password, $user_data->password_hash); 

    $failed_logins = $this->session->userdata('failed_logins'); 

    if ($regenerated_post_password !== $user_data->password) 
    { 
    /* Password from login from does not match user stored password */ 

    if ($failed_logins == 0) 
    { 
    /* First time user has not entered username and password successfully */ 
    $this->session->set_userdata('failed_logins', 1); 
    $this->users_model->increase_login_attempt($this->input->ip_address(), $post_username, gmdate('Y-m-d H:i:s', time())); 
    $this->output('Incorrect username and password credentials!', 'Incorrect Login Credentials', 'Error'); 
    } 

    /* User has atleast one failed login attempt for the current session */ 

    if ($failed_logins !== 4) 
    { 
    /* User has a few more chances to get password right */ 
    $failed_logins++; 
    $this->session->set_userdata('failed_logins', $failed_logins); 
    $this->users_model->increase_login_attempt($this->input->ip_address(), $post_username, gmdate('Y-m-d H:i:s', time())); 
    $this->output('Incorrect username and password credentials!', 'Incorrect Login Credentials', 'Error'); 
    } 

    $this->users_model->lock_out_user($user_data->user_id, gmdate('Y-m-d H:i:s', time()+(60*15))); 
    //$this->functions_model->send_email('maximum_failed_login_attempts_exceeded', $user_data->email_address, $user_data) 
    $this->output('Your account is currently locked, we apologize for the inconvienence. You must wait 15 minutes before you can log in again! An email was sent to the owner of this account! Forgotten your username or password? <a href="forgotusername">Forgot Username</a> or <a href="forgotpassword">Forgot Password</a>', 'Account Locked', 'Error'); 

    } 

    /* Password from login form matches user stored password and user may login */ 

    $this->output('Successful login! Sending you to the dashboard!', 'Login Sucessful', 'Success'); 
} 

任何想法?

+0

这是否只发生在'AJAX'?您是否尝试过定期“表格 - 发布”+分析数据,并在您怀疑“正在执行此操作的行中回应某些值?检查所有参数(如果它们在通过所需功能“接收”后相同,则发送给功能)。 – Kyslik

回答

2

刚刚通过你的代码我可以看到,在你的提交函数中,你再次调用提交函数,这导致了无限的递归行为。

所以必须有在unlock_user()函数users_model模型的问题。

if ($this->is_user_locked($user_data->lock_date)) 
    { 
    /* User is locked from logging in from too many failed attempts */ 
    $this->output('This user account is currently locked!', 'Account Locked', 'Error'); 
    } 
    else 
    { 
    /* User can be unlocked and form be resubmitted */ 
    $this->users_model->unlock_user($user_data->user_id); 
    $this->submit($post_username, $post_password); // <- here 
    return FALSE; 
    }