Codeigniter Session class disable for particular c

2019-07-21 10:52发布

I'm having application in codeigniter already, now i'm developing the android from this application. Using this application as a server side and doing client console in android. In server i'm maintained the session. But in android request i can't maintain the session. Instead of that i'm creating GUID in server at the first request of login request and storing at client side and also storing at customer table, afterwards for each request client will send the GUID for authentication. Now what is my problem is each request getting from android codeigniter create session with different session id(each request generating new session id). How to avoid creating session and storing into database.(Only for android request, but browser request it have to store)

2条回答
相关推荐>>
2楼-- · 2019-07-21 11:49

I managed to extend CI_Session in Codeigniter 3.1.3. Here is what I did:

Create a file application/libraries/Session/MY_Session.php

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

class MY_Session extends CI_Session {

    public function __construct(array $params = array())
    {   
        if ( $this->ignore_sessions() )
            return;
        parent::__construct();
    }   

    function ignore_sessions()
    {   
        $uri = str_replace ("//", "/", $_SERVER['REQUEST_URI']);
        if ( strpos($uri, '/ignore_this_controller/') === 0 ) 
            return true;
        return false;
    }   
}  

You might also want to add 'session' to your config/autoload.php:

$autoload['libraries'] = array('session',....)
查看更多
我命由我不由天
3楼-- · 2019-07-21 11:52

Your question is rather old now, but for anyone who has this problem you should extend the CI_Session library and override the sess_create method. Within your method, test to see what user agent is making the request. Then you only allow a session to be created if the user agent is NOT an android platform.

public function sess_create
{
   //load user agent library
   $this->CI->load->library('user_agent');

   /**test request to see if its from android
    * you can update the user_agents collection to suit more specific needs
    */
   if($this->agent->platform() !== 'android'):

      //create session for all non-android platforms
      parent::sess_create();
   endif;
}
查看更多
登录 后发表回答