2012-02-29 110 views
0

我嘗試使用Codeigniter中的會話進行登錄頁面。Codeigniter - 鏈接

爲了做到這一點,我提出一個模型

function girisKontrol($username, $password) { 
     $sha1_password = sha1($password); 
     $query = "SELECT id FROM pasaj_register WHERE username = ? and password = ?"; 

     $result = $this->db->query($query, array($username, $sha1_password)); 

     if ($result->num_rows() == 1) 
      return $result->row(0)->id; 
     else 
      return false; 
    } 

和giris控制器I由稱爲giris

public function giris() { 

     extract($_POST); 


     $userID = $this->giris->girisKontrol($username,$password); 
     echo $userID; 

     if (!$userID) { 

      $this->session->set_flashdata('login error',TRUE); 

      redirect('giris/giris'); 

     } else { 
      $this->session->set_userdata(array(
        'logged_in' => TRUE, 
        'userID' => $userID)); 

      redirect('welcome_message'); 
     } 


} 
在我提出一個功能更同一控制器

功能:

public function main_page() 
    { 
     if ($this->session->userdata('logged_in')) 
      echo "Logged in"; 
     else 
      echo "Error"; 


    } 

並使用所有這些在視圖中

<form method="POST" action="http://localhost:81/pasaj/giris/main_page/" name="flogin" autocomplete="off"> 

     <label for="username"><b>Kullanıcı adı</b> ya da <b>e-posta</b> adresiniz:</label> 
     <input type="text" value="" class="normalinput" name="username" id="username"> 
     <label for="password">Şifreniz:</label> 
     <input type="password" class="normalinput" value="" name="password" id="password"> 
     <span class="bigsubmitwbtn yellow fright"><span class="bigsubmitwbtn_left"></span><input type="submit" class="bigbutton" name="submit" value="Giriş"></span> 
     </form> 
    </div> 

但是,在main_page函數中被刪除,它直接返回else語句並打印錯誤爲什麼?

+0

您是否嘗試過聲明並獲取其他會話值?我並不完全確定,但是由於服務器在本地主機(您的計算機)上,可能會話並未被精確保存? – 2012-02-29 13:49:53

+0

您是否將數據存儲在數據庫中?什麼是你的會話的var_dump? – Derfder 2013-03-16 11:06:14

回答

1

嘗試加載會話例如在您的aaplication/config文件夾的autoload.php中:

$autoload['libraries'] = array('session'); 

或者在您的控制器的構造函數中。

還要確保會話中正確設置你的config.php文件,如:

$config['sess_cookie_name']  = 'cisession'; 
$config['sess_expiration']  = 86400; 
$config['sess_expire_on_close'] = FALSE; 
$config['sess_encrypt_cookie'] = FALSE; 
$config['sess_use_database'] = FALSE; 
$config['sess_table_name']  = 'ci_sessions'; 
$config['sess_match_ip']  = FALSE; 
$config['sess_match_useragent'] = TRUE; 
$config['sess_time_to_update'] = 300; 

正如提到的變化$配置[ 'sess_cookie_name']來源:

$config['sess_cookie_name']  = 'ci_session'; 

到:

$config['sess_cookie_name']  = 'cisession'; 

此外,您的會話設置正確,例如:

// create session 
$this->session->set_userdata($data); 

如何存儲會話數據?在數據庫中還是不?

0

在codeigniter config中已經更改了默認的會話/ cookie名稱和域名?

確保您更改名稱包括下劃線「_」,因爲在所有的瀏覽器下劃線不工作,例如IE)