2014-08-29 84 views
-3

後,我用一個簡單的功能笨登錄成功後重定向我的網頁。笨:PHP不工作第一次刷新

當頁面加載我的內容被加載,而不是PHP代碼標記之間。

這不是rewrite_short_tags這是我猜的問題,因爲當我按F5所有裝載好。

爲什麼我需要刷新我的頁面F5才能看到結果?

控制器:

session_start(); 
class Login_form 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'); 
     echo validation_errors(); 
     include "application/views/forms/login.php"; 
    } 
    else 
    { 

     //Refresh index   
     redirect('index', '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; 
    } 
} 

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

的觀點:

<h1>My cms</h1>  
<?php 
    if (!$this->session->userdata('logged_in')) { 
     echo form_open('login_form', array('id' => 'loginForm')); ?> 
     <div id="formLoginDiv">    
      <?php include "forms/login.php"; ?> 
     </div> 
     </form> 
<?php } else { ?>     
     <a href="<?php echo site_url('login_form/logout')?>">Logout</a> 
<?php } ?> 

我跑的觀點一樣,:redirect('index', 'refresh');

現在,它加載所有的好時候,如果第一次啓動了這一觀點。在ajax完成檢查並返回true後,我刷新此視圖。但註銷鏈接不被查看。

更新:
我試過no-cache頭把他們安置在不同的地方(甚至在主要的index.php)。
其次,我不使用$template var,所以我得到一個錯誤。

+0

我已經加入了相關的代碼和更新在2「答案」由OP給出的問題和投票重新開放。 – GitaarLAB 2014-09-07 04:34:01

回答

0

我覺得你的瀏覽器緩存中的受害者。嘗試在刷新時不設置緩存標題:

header('Cache-Control: no-cache'); 
header('Pragma: no-cache'); 
0

以下示例我希望對您有所幫助。

$this->template->build('SET YOUR PATH',$this->data); 

$ this->數據使用如果在數據array.so中登錄後傳遞一些細節,那麼您可以在構建模板之前傳遞這些值。 一樣,

$this->data['id']=$this->session->userdata('id',$id); 
相關問題