2017-07-20 51 views
0

我在写代码控制器。用户以跑道用户身份登录到网站。登录后,它们被重定向到仪表板。如何避免跑道重新认证每次我从跑道查询数据?

函数test_auth进行验证并将用户重定向到仪表板。

的问题是:在仪表板功能,我必须reauthentificate如果我要打电话,例如,function PodioOrganization::get_all();

这里是我的代码:

class User extends CI_Controller { 

    /** 
    * __construct function. 
    * 
    * @access public 
    * @return void 
*/ 
public function __construct() { 

    parent::__construct(); 
    $this->load->library(array('session')); 
    $this->load->helper(array('url')); 
    $this->load->model('user_model');  
} 

public function dashboard() 
{  
    if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] != true) {//not logged   
     redirect(base_url().'user/login'); 
    }else 
    { 

     $data=(array)$this->user_model->get_user($_SESSION['user_id']); 

     //$data=(array)$this->user_model->get_user(3); 
     $data['title'] ='Dashboard Page'; 
      //'heading' => 'My Heading', 
      //'message' => 'My Message' 

     Podio::setup(CLIENT_ID, CLIENT_SECRET); 

     $orgs=PodioOrganization::get_all(); 

      $this->load->view('header',$data); 
      $this->load->view('user/page_header',$data); 
      $this->load->view('user/dashboard',$data); 
      $this->load->view('user/footer');           
    }    
}  

public function test_auth() 
{ 
    // Set up the REDIRECT_URI -- which is just the URL for this file. 

    if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) { 
     redirect(base_url().'user/dashboard'); 
    } 
    else{ 

     Podio::setup(CLIENT_ID, CLIENT_SECRET); 

     if (!isset($_GET['code']) && !Podio::is_authenticated()) { 

      // User is not being reidrected and does not have an active session 
      // We just display a link to the authentication page on podio.com 
      $auth_url = htmlentities(Podio::authorize_url(REDIRECT_URI)); 
      //print "<a href='{$auth_url}'>Start authenticating</a>"; 
      redirect($auth_url); 

     } elseif (Podio::is_authenticated()) { 

      // User already has an active session. You can make API calls here: 
      print "You were already authenticated and no authentication is needed."; 

     } 
     elseif (isset($_GET['code'])) { 

      // User is being redirected back from podio.com after authenticating. 
      // The authorization code is available in $_GET['code'] 
      // We use it to finalize the authentication 

      // If there was a problem $_GET['error'] is set: 
      if (isset($_GET['error'])) { 
      print "There was a problem. The server said: {$_GET['error_description']}"; 
      } 
      else { 
      // Finalize authentication. Note that we must pass the REDIRECT_URI again. 
      Podio::authenticate_with_authorization_code($_GET['code'], REDIRECT_URI); 


       $_SESSION['user_id']  = 1; 
       $_SESSION['logged_in'] = (bool)true; 
       $_SESSION['access_token'] = (string)Podio::$oauth->access_token ; 
       $_SESSION['refresh_token']= (string)Podio::$oauth->refresh_token ; 

       redirect(base_url().'user/dashboard');             
      } 

     } 
    } 
} 

我也得到一个PodioAuthorizationError,调用$orgs=PodioOrganization::get_all();没有先验证(虽然我已经在test_auth函数中完成了这项工作)

而且我曾在test_auth()中调用Podio::setup(CLIENT_ID, CLIENT_SECRET);为什么我必须在dashboa中再次调用它rd()。应该设置$client_id$client_secret insite setup()函数。为什么Podio::setup失去其价值?

$client_id$client_secret在班级跑道内声明为静态,因此它们应该保留它们的值,但它们不会。为什么?

+0

服务器是无状态的,这意味着每个请求都会重置应用程序中的任何状态。通常开发人员会将信息存储在会话或cookie中,以允许浏览器在请求之间携带状态。 –

回答