Codeigniter:php在刷新后第一次没有工作

I use a simple function in codeigniter to redirect my page after a successful login.

When the page is loaded my content is loaded, but not code between php tags.

It's not the rewrite_short_tags that is the problem I guess, because when I press F5 all is loaded well.

How come I need to refresh my page with F5 to see the results?

The controller:

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');
 }
}

The view:

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

I run the view like: redirect('index', 'refresh');

Now, it loads all good when if first start this view. After a check done by ajax and returned true I refresh this view. But the Logout link is not viewed.

Update:
I tried the no-cache headers and placed them in different places (even in the main index.php).
Secondy, the I do not use the $template var, so I get an error with that one.

I think you're the victim of browser cache. Try setting no cache headers on refresh:

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

the following example i wish to help you.

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

$this->data use if passing some detail in after login in data array.so you can pass these value before build template. like,

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