2015-01-10 88 views
2

遵循CodeIgniter出色的文檔後,我有一個非常簡單的新聞文章系統設置,允許任何人發佈文章,如果他們去新聞/創建。我現在試圖實現一個簡單的登錄解決方案,該解決方案只允許登錄用戶發佈文章,我已經遵循本教程(http://www.iluv2code.com/login-with-codeigniter-php.html),並使其在教程中顯示的工作,但是當出現問題而不是獲取用戶時在他們登錄後登陸私人頁面我希望他們登陸新聞/創建。 我試圖讓這個工作,讓我讓用戶登陸頁面,但頁面然後無法正確呈現留下文章提交表單不存在。這是我相信這是導致該問題(主要是「創建」功能)代碼:CodeIgniter:顯示錶格問題

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
session_start(); 
class News extends CI_Controller { 

public function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('news_model'); 
} 

public function index() 
{ 
    $data['news'] = $this->news_model->get_news(); 
    $data['title'] = 'News archive'; 

    $this->load->view('templates/header', $data); 
    $this->load->view('news/index', $data); 
    $this->load->view('templates/footer'); 

} 

public function view($slug) 
{ 
    $data['news_item'] = $this->news_model->get_news($slug); 

    if (empty($data['news_item'])) 
     { 
      show_404(); 
     } 

    $data['title'] = $data['news_item']['title']; 

    $this->load->view('templates/header', $data); 
    $this->load->view('news/view', $data); 
    $this->load->view('templates/footer'); 
} 



public function create() 
{ 
    if($this->session->userdata('logged_in')) 
     { 
      $session_data = $this->session->userdata('logged_in'); 
      $data1['username'] = $session_data['username']; 
      $this->load->view('news/create', $data1); 

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

      $data['title'] = 'Create a news item'; 

      $this->form_validation->set_rules('title', 'Title', 'required'); 
      $this->form_validation->set_rules('text', 'text', 'required'); 

      if ($this->form_validation->run() === FALSE) 
       { 
        $this->load->view('templates/header', $data); 
        $this->load->view('news/create'); 
        $this->load->view('templates/footer'); 

       } 
      else 
       { 
        $this->news_model->set_news(); 
        $this->load->view('templates/header', $data); 
        $this->load->view('news/success'); 
        $this->load->view('templates/footer'); 
       } 
     } 

    else 
     { 
      //If no session, redirect to login page 
      redirect('login', 'refresh'); 
     } 
} 

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

} 

對於那些評論詢問有關會議上,有一個單獨的控制器創建會話(如下圖所示)。雖然在'私人頁面'上只有登錄用戶可以訪問它,但是當前顯示的是用戶名,但是在初始'用戶登錄或不登錄'之後,它無法呈現任何create函數。

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
class VerifyLogin extends CI_Controller { 
function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('user','',TRUE); 
} 

function index() 
{ 
    //This method will have the credentials validation 
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); 
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database'); 

if($this->form_validation->run() == FALSE) 
{ 
//Field validation failed. User redirected to login page 
$this->load->view('login_view'); 
} 
else 
{ 
//Go to private area 
redirect('news/create', 'refresh'); 
} 
} 

function check_database($password) 
{ 
    //Field validation succeeded. Validate against database 
    $username = $this->input->post('username'); 

    //query the database 
    $result = $this->user->login($username, $password); 

    if($result) 
    { 
    $sess_array = array(); 
    foreach($result as $row) 
    { 
    $sess_array = array(
    'id' => $row->id, 
    'username' => $row->username 
    ); 
    $this->session->set_userdata('logged_in', $sess_array); 
    } 
    return TRUE; 
    } 
    else 
    { 
$this->form_validation->set_message('check_database', 'Invalid username or password'); 
return false; 
} 
} 
} 
?> 

任何指導或幫助將不勝感激。

+0

打開display_errors。我有一種感覺$ session_data是一個布爾值,但你作爲一個數組訪問它 – etherous

+0

我只是想知道你在哪裏設置會話登錄並提供用戶名? – Patrick

+0

修改原始帖子以澄清這些要點。 –

回答

0

經過這段代碼的某段時間後,我終於到達了這個問題,並將在這裏發佈解決方案,因爲任何人遇到類似的問題,他們可以使用這個作爲參考。

我想在初始檢查中加載視圖以查看用戶是否已登錄,刪除此行允許應用程序的其餘部分功能。

public function create() 
{ 
if($this->session->userdata('logged_in')) 
    { 
     $session_data = $this->session->userdata('logged_in'); 
     $data1['username'] = $session_data['username']; 
     $this->load->view('news/create', $data1); ----- LINE IN QUESTION 

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

     $data['title'] = 'Create a news item'; 

     $this->form_validation->set_rules('title', 'Title', 'required'); 
     $this->form_validation->set_rules('text', 'text', 'required');