2014-01-11 31 views
0

我是Codeigniter的新手,我的問題是,我的管理面板在本地主機上工作正常,但不在服務器上,在服務器上登錄時,並在重定向到登錄頁面後再次。 這是我的代碼。

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 */ 
+0

你得到的任何錯誤/警告? –

+0

發佈您的本地主機和服務器的php.ini會話設置。很有可能你的配置不同,所以服務器不會保持會話或正確創建會話。 –

+0

@JensonMJohn否 –

回答

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思想。嘗試將其添加到confg文件夾中的自動加載器。

+0

我在哪裏添加此代碼? 在dashboard.php或admin.php中,請指導我 –

+0

我在那裏更新了一些。我認爲你的主要問題是,你的重定向 ex沒有base_url。重定向(BASE_URL( '管理員/')); – Sparatan117

相關問題