如何在Code Igniter Community Auth PHP中使用用户名和密码检查登录凭据?

时间:2017-10-02 07:44:29

标签: php codeigniter

我无法在 Code Igniter - Community Auth 的文档中找到它,如何使用简单的功能检查用户凭据。我想调用一个函数。

示例: checkIfValid('username','mypassword123')

        $uid = array_key_exists('uid', $_POST) ? (empty($_POST['uid']) ? '' : $_POST['uid']) : '';
        $pwd = array_key_exists('pwd', $_POST) ? (empty($_POST['pwd']) ? '' : $_POST['pwd']) : '';

        if ($uid == '' || $pwd == '') { echo $error_response; return; };

        $auth_model = $this->authentication->auth_model;
        // HOW CAN I LOGIN WITH BOTH USERNAME AND PASSWORD LIKE CALLING A FUNCTION? THIS CODE ONLY LOGS IN USER UID ONLY
        $auth_data = $this->{$auth_model}->get_auth_data($uid);

        if ($auth_data) {
            $this->authentication->maintain_state($auth_data);
            echo 'login';
            return;
        }

2 个答案:

答案 0 :(得分:1)

在您的控制器上验证在此处进行

        $this->load->library('authentication');

        $res = $this->authentication->login( $requirement, $user_string, $passwd );   // requirement is user level

        if ($res)
        {
            echo "User authenticated"; // or do something here
        }
        else
        {
            print_r($res); // or do something what ever
        }

答案 1 :(得分:0)

我不确定但是试试这个。

    // Get normal authentication data using username or password
    if( $auth_data = $this->{$auth_model}->get_auth_data( $uid,$pwd ) )
    {
        /**
         * If redirect param exists, user redirected there.
         * This is entirely optional, and can be removed if 
         * no redirect is desired.
         */
        $_GET['redirect'] = 'somewhere';
        $this->authentication->redirect_after_login();

        // Set auth related session / cookies
        $this->authentication->maintain_state( $auth_data );
    }
}
else
{
    echo 'Example requires that you set a username or password.';
}
相关问题