codeigniter,$ _SESSION和cookie在Localhost上工作正常但在服务器上没有

时间:2014-01-11 07:25:10

标签: php codeigniter session cookies

我是Codeigniter的新手,我的问题是,我的管理面板在localhost上工作正常但在服务器上没有,在我登录时在服务器上,它的Goto仪表板和再次重定向到登录页面之后。 这是我的代码。

dashboard.php

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

    class Dashboard extends CI_Controller {

        function __construct()
        {
        parent::__construct();
             error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
            session_start();
            if (!isset($_SESSION['userid']) || $_SESSION['userid'] < 1){
                $this->session->set_flashdata('errorm','Session timed out. Login again');
                redirect('admin/','refresh');
            }
        }

        function index()
        {
            $data['title'] = "Manage Dashboard";
            $data['main'] = 'admin/main';
            $data['webpagename'] = 'main';
            $this->load->vars($data);
            $this->load->view('admin/template/innermaster'); 
        }

        function logout()
        {
            unset($_SESSION['userid']);
            unset($_SESSION['username']);

            $this->load->helper('cookie');
            delete_cookie("username");
            delete_cookie("password");
            delete_cookie("userid");

            $this->session->set_flashdata('error',"You've been logged out!");
            redirect('admin/','refresh');   
        }

        function subscribe()
        {
            /**
            * form_validation
            */
            $this->form_validation->set_rules('name', 'Name', 'required');
            $this->form_validation->set_rules('email', 'Email',  'required|valid_email');
            $this->form_validation->set_rules('captcha', 'Captcha', 'required');
            if ( $this -> _check_capthca() )
            {
                if ($this->form_validation->run() == FALSE)
                {
                    $this->session->set_flashdata('subscribe_msg', 'All fields are required . Please try again!');
                    redirect('welcome/index');
                }
                else
                {
                    $this->MSubscribers->createSubscriber();
                    $this->session->set_flashdata('subscribe_msg', 'Thanks for subscribing!');
                    redirect('welcome/index','refresh');
                }
            }
            else
            {
                $this->session->set_flashdata('subscribe_msg', 'Enter captcha . Please try again!');
                redirect('welcome/index');
            }
        }

        public function changepassword()
        {
            $data['main'] = 'admin/change_password';
            $data['webpagename'] = '';
            $this->load->vars($data);
            $this->load->view('admin/template/innermaster');
        }

        function forgot_password()
        {
            // $this->model->MAdmins();
            if ($this->input->post('retype_newpass'))
            { 
                $count = $this->MAdmins->verifyPassword(); 
                if($count=='0')
                {
                    $this->session->set_flashdata('errorm', 'Your Old Password does not match');
                    redirect('welcome/forgot_password');
                }
                else
                {
                    $this->MAdmins->change_password();
                    $this->session->set_flashdata('message', 'Your Password has been changed..!');
                    redirect('welcome/verify');
                }
            }
            else
            {
                $data['title'] = 'Forgot Password';
                $data['main'] = 'forgot_password'; 
                $this->load->vars($data);
                $this->load->view('template');
            } 
        }

        public function updatepassword()
        {
            $this->load->helper('cookie');
            $data['main'] = 'admin/change_password';

            $this->load->vars($data);
            $this->load->view('admin/template/innermaster');

            $this->load->model('madmins');
            $flag= $this->madmins->updatepassword();    
            if($flag=="false")
            {
                $this->session->set_flashdata('passmessage','false');
            }
            else
            {
                $this->session->set_flashdata('passmessage','true');
            }
            redirect('/admins/dashboard/changepassword','refresh');
        }

    }

    /* End of file welcome.php */
    /* Location: ./application/controllers/welcome.php */

admin.php的

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


    class Admin extends CI_Controller {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        *       http://example.com/index.php/welcome
        *   - or -  
        *       http://example.com/index.php/welcome/index
        *   - or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
        function __construct()
        {
            parent::__construct();
        error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
            session_start();
        }

        public function index()
        {
            $this->load->helper('cookie');
            if(get_cookie('username') && get_cookie('password') && get_cookie('userid'))
            {
                $_SESSION['userid'] = get_cookie('userid');
                $_SESSION['username'] = get_cookie('username');
                redirect('admins/dashboard','refresh');
            }
            $this->load->view('admin/index');
        }

        public function login()
        {
            $this->load->view('admin/index');
        }

        function verify()
        {
            $this->load->helper('cookie');

            if(get_cookie('username') && get_cookie('password') && get_cookie('userid'))
            {
                $_SESSION['userid'] = get_cookie('userid');
                $_SESSION['username'] = get_cookie('username');

            }

            if ($this->input->post('username'))
            {
                $u = $this->input->post('username');
                $pw = $this->input->post('pass');
                //$this->load->database();

                $this->load->model('MAdmins');
                $this->MAdmins->verifyUser($u,$pw);

                if ($_SESSION['userid'] > 0)
                {   
                    redirect('admins/dashboard','refresh');
                }
                else
                {
                    $this->session->set_flashdata('message','Your login attempt was not successful. Please try again.');
                    redirect('admin/','refresh');
                }
            }
            $data['main'] = 'admin/index';
            $this->load->vars($data);
        }
    }

    /* End of file welcome.php */
    /* Location: ./application/controllers/welcome.php */

1 个答案:

答案 0 :(得分:0)

你为什么这么做? Codeigniter Sessions

// Set the data    
$this->session->set_userdata('username',$value);

//can also be done like this
$array = array('one'=>'one','two'=>'two');
$this->session->set_userdata($array);

// to pull information from the session    
$username = $this->session->userdata('username');

另外,我认为您可能会收到反馈循环,因为您没有在重定向中定义base_url()。你需要加载url helper think。尝试将其添加到confg文件夹中的自动加载器。