2012-08-01 36 views
0

我試圖將一個論壇(在Codeigniter中創建)到一個網站(簡單的PHP >>>沒有框架使用)。

爲了自動登錄論壇,當我在我的網站登錄時,我需要使用論壇的功能,它需要2個參數$ username和$ password。

我已經在我的網站上有$ _SESSION這個信息(用戶名和密碼)。

如何從論壇讀取$ _SESSION(正如我之前所說的基於Codeigniter的),因爲我沒有權限。

是否有可能性定義2個常量,在論壇的核心/配置中的某個地方,從$ _SESSION中保存這些細節,以便從論壇內的任何位置訪問?

我知道CI的會話與$ _SESSION不同,所以請幫助我更實際一些,以解決我的問題。

謝謝。

+1

是否CI現場秋天的cookie保存會話標識符的範圍之內的位置? AFAIK CI會話與PHP會話是分開的,因此您應該可以在CI頁面中安全地調用'session_start()',它可以像其他地方一樣工作。 – DaveRandom 2012-08-01 15:44:29

+0

Yeap,這對我來說是正確的答案:)我總是有這個問題,因爲我使用自動加載這些類的框架工作:(謝謝+1 +1 – mgm 2012-08-01 15:54:00

回答

1

閱讀本網址; -

http://codeigniter.com/forums/viewthread/158923/#766011

http://codeigniter.com/forums/viewthread/188648/#892137

如果誰想要做2.0.2

本地會話就在native_session.php文件複製到您的應用程序/庫/並將其重命名爲Session.php

然後更改類名稱和構造函數名稱到CI_Session

還添加以下,那麼它應該工作正常。

function sess_destroy() 
{ 
    $this->destroy(); 
} 

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

/* 
    Native/Database hybrid 
    Code Igniter 
    Citrusmedia - Matthew Lymer 
*/ 


class CI_Session 
{ 
    var $sess_table_name   = ''; 
    var $sess_expiration   = 7200; 
    var $sess_match_ip    = FALSE; 
    var $sess_match_useragent  = TRUE; 
    var $sess_time_to_update  = 300; 
    var $encryption_key    = ''; 
    var $flashdata_key     = 'flash'; 
    var $time_reference    = 'time'; 
    var $gc_probability    = 5; 
    var $userdata     = array(); 
    var $CI; 
    var $now; 

    /** 
    * Session Constructor 
    * 
    * The constructor runs the session routines automatically 
    * whenever the class is instantiated. 
    */ 
    function CI_Session($params = array()) 
    {     
     log_message('debug', "Session Class Initialized"); 

     // Set the super object to a local variable for use throughout the class 
     $this->CI =& get_instance(); 

     // Set all the session preferences, which can either be set 
     // manually via the $params array above or via the config file 
     foreach (array('sess_table_name', 'sess_expiration', 'sess_match_ip', 'sess_match_useragent', 'sess_time_to_update', 'time_reference', 'encryption_key') as $key) 
     { 
      $this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key); 
     } 

     // Sessions, start your engines! 
     ini_set("session.gc_maxlifetime", $this->sess_expiration); 
     session_start(); 

     // Load the string helper so we can use the strip_slashes() function 
     $this->CI->load->helper('string'); 

     // Are we using a database? If so, load it 
     if(!$this->sess_table_name) { 
      die('Session class database table name not configured'); 
     } 

     $this->CI->load->database(); 

     // Set the "now" time. Can either be GMT or server time, based on the 
     // config prefs. We use this to set the "last activity" time 
     $this->now = $this->_get_time(); 

     // Set the session length. If the session expiration is 
     // set to zero we'll set the expiration two years from now. 
     if ($this->sess_expiration == 0) 
     { 
      $this->sess_expiration = (60*60*24*365*2); 
     } 

     // Run the Session routine. If a session doesn't exist we'll 
     // create a new one. If it does, we'll update it. 
     if (! $this->sess_read()) 
     { 
      $this->sess_create(); 
     } 
     else 
     { 
      $this->sess_update(); 
     } 

     // Delete 'old' flashdata (from last request) 
      $this->_flashdata_sweep(); 

     // Mark all new flashdata as old (data will be deleted before next request) 
      $this->_flashdata_mark(); 

     // Delete expired sessions if necessary 
     $this->_sess_gc(); 

     log_message('debug', "Session routines successfully run"); 
    } 

    // -------------------------------------------------------------------- 

    /** 
    * Fetch the current session data if it exists 
    * 
    * @access public 
    * @return bool 
    */ 
    function sess_read() 
    { 
     // Unserialize the session array 
     // $session = $this->_unserialize($session); 

     $session = array(); 

     foreach(array('session_id', 'ip_address', 'user_agent', 'last_activity') as $key) 
     { 
      if(!isset($_SESSION[$key])) { 
       $this->sess_destroy(); 
       return FALSE; 
      } 

      $session[$key] = $_SESSION[$key]; 
     }  

     // Is the session current? 
     if (($session['last_activity'] + $this->sess_expiration) < $this->now) 
     { 
      $this->sess_destroy(); 
      return FALSE; 
     } 

     // Does the IP Match? 
     if ($this->sess_match_ip == TRUE AND $session['ip_address'] != $this->CI->input->ip_address()) 
     { 
      $this->sess_destroy(); 
      return FALSE; 
     } 

     // Does the User Agent Match? 
     if ($this->sess_match_useragent == TRUE AND trim($session['user_agent']) != trim(substr($this->CI->input->user_agent(), 0, 50))) 
     { 
      $this->sess_destroy(); 
      return FALSE; 
     } 

     $this->CI->db->where('session_id', $session['session_id']); 

     if ($this->sess_match_ip == TRUE) 
     { 
      $this->CI->db->where('ip_address', $session['ip_address']); 
     } 

     if ($this->sess_match_useragent == TRUE) 
     { 
      $this->CI->db->where('user_agent', $session['user_agent']); 
     } 

     $query = $this->CI->db->get($this->sess_table_name); 
+1

'注意:Session類不使用本機PHP會話.' – DaveRandom 2012-08-01 15:49:14

+1

如果你想$ _SESSION變量,那麼你想session_start();在認證文件 – 2012-08-01 15:52:30

+0

正如我已經在我的消息中寫道:「我知道CI的會議是從$ _SESSION不同,所以請幫助我更實際的東西,在爲了解決我的問題。「 – mgm 2012-08-01 15:52:38