redirect()函数不起作用

时间:2012-02-14 00:08:19

标签: php codeigniter

我使用CodeIgniter创建了一个Web应用程序,首先是一个登录界面。这是我使用的控制器,但我认为有些东西不起作用,但我不知道是什么。主页,用户被授予的地方,它没有显示。不幸的是我没有调试器来检查什么不是不行。也许有一个问题来处理会议,但我真的不知道可能是什么。也许你比我发现错误


登录控制器

<?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 */

家庭控制器

<?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 */

登录控制器中的模型运行良好,因此不存在查询问题。在添加会话之前一切正常,但是当我添加会话停止工作时,我认为这可能是一个问题或redirect()或会话

主页视图

<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>

登录视图

<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>

   <?
        }
   ?>

2 个答案:

答案 0 :(得分:1)

CodeIgniter在重定向后保留会话数据存在问题。您的if($this->session->userdata('logged_in'))控制器中的home.php每次都会评估false,因为会话正在重置。您可以使用本机php会话来跳过此问题。

请参阅:http://codeigniter.com/wiki/Native_session/。祝你好运!

<强>更新

显然,与IE6一起使用时,这只适用于CodeIgniter 1.7.2。它不会影响大多数浏览器。

答案 1 :(得分:0)

打开 application / config / autoload.php 文件并在帮助程序数组中添加'url';

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

我希望这可以帮助您解决问题。