简体   繁体   English

redirect()函数不起作用

[英]redirect() function doesn't work

I have create a web application using CodeIgniter making at first a login interface. 我已经使用CodeIgniter创建了一个Web应用程序,首先使它成为一个登录界面。 Here are the controller I used but I think something doesn't work but I don't know what.The home page, where the user is granted, it doesn't show.Unfortunately I didn't have a debugger to check what doesn't work. 这是我使用的控制器,但我认为某些操作不起作用,但我不知道该怎么办。首页,没有授予用户权限的页面,但我没有调试器来检查什么不行 maybe there is a problem to handle the session but really I don't know what can be. 可能有一个处理会议的问题,但实际上我不知道该怎么办。 maybe you are smarther than me to find the error 也许您比我更聪明地发现错误


Login Controller 登录控制器

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller 
{
    public function __construct() 
    {
        parent::__construct();
        $this->load->model('User','user'); /* This call the model to retrieve data from db */
    }

    public function index()
    {
        if(!file_exists('application/views/_login.php'))
        {
            show_404();
        }

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

        $this->form_validation->set_error_delimiters('<h4 style="text-align:center;">','</h4>');

        $this->form_validation->set_rules('username','username','trim|required|xss_clean');
        $this->form_validation->set_rules('password','password','trim|required|xss_clean|callback_pass_check');

        if($this->form_validation->run() == FALSE)
        {
            /* Data to pass to view */
            $data['title'] = "User Access";
            $data['author'] = "Salvatore Mazzarino";
            $data['year'] = date('Y');

            $this->load->view('templates/_header',$data);
            $this->load->view('_login',$data);
            $this->load->view('templates/_footer',$data);
        }
        else 
        {   
            redirect('home', 'refresh');            
        }
    }

    public function pass_check($pass)
    {
        $result = $this->user->find_user($this->input->post('username'),$pass);

        if(!empty($result))
        {
            foreach ($result as $row) 
            {
                $session_array = array('id'=> $row->id, 'username'=> $row->username); /* Create a session passing user data */

                $this->session->set_userdata('logged_in', $session_array);
            }
            return TRUE;
        }
        else
        {
            $this->form_validation->set_message('pass_check',"Invalid username or password!</br>Try again, please!");
            return FALSE;
        }
    }
}

/* END OF FILE */

Home Controller 家庭控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Home extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        session_start();
    }

    public function index()
    {
        if($this->session->userdata('logged_in'))
        {            
            $data['title'] = "Management Emergency";
            $data['author'] = "Salvatore Mazzarino";
            $data['year'] = date('Y');

            $this->load->view('templates/_header', $data);
            $this->load->view('_home',$data);
            $this->load->view('templates/_footer',$data);
        }
        else
        {
            redirect('login', 'refresh');
        }
    }

    public function logout()
    {
        $this->session->unset_userdata('logged_in');
        session_destroy();
        redirect('home','refresh');
    }
}

/* END OF FILE */

The model in the login controller works very well so It isn't a problem of query. 登录控制器中的模型运行良好,因此这不是查询问题。 Before adding session everything works but when I added session stopped to worked so I think that can be a problem or redirect() or session 在添加会话之前,一切正常,但是当我添加会话时,它停止了工作,因此我认为这可能是一个问题或redirect()或会话

Home View 主视图

<div data-role = "page">

<div data-role = "header" data-position = "inline">
        <a href="" data-icon = "home" data-iconpos = "notext"></a>
            <?php echo heading($title,1) ?>
        <a href="" data-icon = "info" data-iconpos = "notext" data-transition= "flip"></a>
</div>

<div class = "menu-content">
        <ul data-role = "listview" data-inset="true">
                <li data-role = "list-divider">Emergency Menù</li>
                <li class = "menu-item">
                        <a href="">
                                <div class = "image-wrapper">
                                        <img src="/assets/images/user.png" class = "ui-li-icons" />
                                </div>
                                Add patient
                        </a>
                </li>
                <li class = "menu-item">
                        <a href="#">
                                <div class = "image-wrapper">
                                        <img src="/assets/images/home.png" class = "ui-li-icons" />
                                </div>
                                Show all hospital
                        </a>
                </li>
                <li class = "menu-item">
                        <a href="#">
                                <div class = "image-wrapper">
                                        <img src="/assets/images/favorite.png" class="ui-li-icons" />
                                </div>
                                Find patients
                        </a>
                </li>
                <li class="menu-item">
                        <a href="#">
                                <div class = "image-wrapper">
                                        <img src="/assets/images/email.png" class="ui-li-icons" />
                                </div>
                                Send medical infos
                        </a>
                </li>
        </ul>

Login View 登录视图

<div data-role ="dialog">

<div data-role = "header" data-theme="e">
    <?php echo heading($title,1) ?>
</div>

<div data-role ="content">

    <?php 

        $var = validation_errors();

        if(!empty($var))
        {
            echo form_error('username');
            echo form_error('password');
        }
        else
        {                    
                echo heading('911 - First Aid',2,'style="text-align:center; color:red;"');

                echo form_open('login');
    ?>
                    <div data-role ="fieldcontain" class="ui-hide-label">
                        <label for="username">Username:</label>
                        <input type="text" name="username" id="name" value="" placeholder="Username"/>
                    </div>

                    <div data-role ="fieldcontain" class="ui-hide-label">
                        <label for="password">Password</label>
                        <input type="password" name="password" id="password" value="" placeholder="Password"/>
                    </div>

                    <div data-role ="fieldcontain">
                        <input type="submit" value="Login" data-theme ="b"/>
                    </div>

                </form>

   <?
        }
   ?>

There is a problem with CodeIgniter retaining it's session data after a redirect. 重定向后CodeIgniter保留其会话数据存在问题。 Your if($this->session->userdata('logged_in')) in your home.php controller will evaluate false every time because the session is resetting. 由于会话正在重置,因此您在home.php控制器中的if($this->session->userdata('logged_in'))每次都会评估为false You could use the native php sessions to skip over this problem. 您可以使用本地php会话来跳过此问题。

See: http://codeigniter.com/wiki/Native_session/ . 请参阅: http : //codeigniter.com/wiki/Native_session/ Good luck! 祝好运!

UPDATE 更新

Apparently, this is only true of CodeIgniter 1.7.2 when used with IE6. 显然,仅当与IE6一起使用时,CodeIgniter 1.7.2才适用。 It doesn't affect most browsers. 它不会影响大多数浏览器。

open the application/config/autoload.php file and add the 'url' in the helper array; 打开application / config / autoload.php文件,并在助手数组中添加“ url”;

$autoload['helper'] = array('url');

i hope this will help you to fix the problem. 我希望这将帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM