登录Codeigniter后如何修复页面重定向

时间:2019-04-21 10:23:20

标签: php codeigniter

我编写了一个可以在我的本地主机上完美运行的代码,但是当应用程序继续循环回到登录页面时,即使使用正确的登录凭据也无法成功登录。

由于该应用程序在本地运行良好,所以我认为我的问题将是php / apache版本或/和配置问题。我在.htaccess文件中尝试了几种设置,但均未成功。该应用程序已使用codeigniter框架版本2.2.0编写。

登录控制器

    function ajax_login()
    {

        $response = array();
        $url = $this->get_url();

        if (strpos($url, 'students')  !== FALSE)
        {

            $mat_no = $_POST["mat_no"];

        }else
        {
            $email = $_POST["email"];
        }

        //Recieving post input of email, password from ajax request
        //$email        = $_POST["email"];
        $password   = $_POST["password"];
        $response['submitted_data'] = $_POST;
        //$login_status = $this->validate_login_student( $mat_no ,  $password );
        //Validating login
        $url = $this->get_url();
        if (strpos($url, 'students') !== FALSE)
        {
            $login_status = $this->validate_login_student( $mat_no ,  $password );
        }else
        {
            $login_status = $this->validate_login( $email ,  $password );
        }


        $response['login_status'] = $login_status;
        if ($login_status == 'success') {

            $response['redirect_url'] = '';

        }

        //Replying ajax request with validation response
        echo json_encode($response);
    }

  function logout()
    {
        $acctype = $this->session->userdata('login_type');
        if ($acctype == 'admin')
        {
            $acctype = 'webadmin';
        }elseif ($acctype == "student"){
            $acctype = 'students';
        }elseif ($acctype == "teacher"){
            $acctype = 'teachers';
        }
        elseif ($acctype == "parent"){
            $acctype = 'parents';
        }
        $this->session->unset_userdata();
        $this->session->sess_destroy();
        $this->session->set_flashdata('logout_notification', 'logged_out');
        redirect(base_url().$acctype , 'refresh');
    }

routes.php

$route['default_controller'] = "login";
$route['404_override'] = '';

$route['students'] = "login";
$route['teachers'] = "login";
$route['parents'] = "login";
$route['webadmin'] = "login";

js ajax处理程序


var segment = '';
                    if (/students/.test(window.location.href))
                    {
                        segment = 'students/';
                    }
$.ajax({
                        url: baseurl + segment + 'index.php?login/ajax_login',
                        method: 'POST',
                        dataType: 'json',
                        data: formdata,

                        error: function()
                        {

                            alert("An error occoured!");
                        },
                        success: function(response)
                        {
                            // Login status [success|invalid]
                            var login_status = response.login_status;
                            var redirect_url = baseurl;

                            if(response.redirect_url && response.redirect_url.length)
                            {
                                redirect_url = response.redirect_url;
                            }

                            window.location.href = redirect_url;
                            // Form is fully completed, we update the percentage
                            neonLogin.setPercentage(100);


                            // We will give some time for the animation to finish, then execute the following procedures
                            setTimeout(function()
                            {
                                // If login is invalid, we store the
                                if(login_status == 'invalid')
                                {
                                    $(".login-page").removeClass('logging-in');
                                    neonLogin.resetProgressBar(true);
                                }
                                else
                                if(login_status == 'success')
                                {
                                    // Redirect to login page
                                    setTimeout(function()
                                    {
                                        var redirect_url = baseurl + segment;

                                        if(response.redirect_url && response.redirect_url.length)
                                        {
                                            redirect_url = response.redirect_url;
                                        }

                                        window.location.href = redirect_url;
                                    }, 400);
                                }

                            }, 1000);
                        }
                    });

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


成功登录后,应用程序应重定向到仪表板

0 个答案:

没有答案