dregvw1801
2013-07-27 16:58
浏览 32
已采纳

使用像superignlobal这样的codeigniter会话

FIRST OF ALL: I'm trying to develop a class or a couple of methods (or just a way), to use in codeigniter, that is generic, easy and flexible, I need a way to pass a message to it, display, everywhere in the project, and erase it then. (and if is ajax, will be awesome)

Back in the time that i didn't used codeigniter, I use to have a class, to insert messages in a session, a very simple class, so i could display success messages and etc. Here is the class that i want to bring to codeigniter, but I'm almost sure that codeigniter has a similar solution, but i don't know what to search. I also would like I better solution than this one, because i tried to adapt to the way that codeigniter hands session, but didn't work

<?php
class Basic
{
    protected $erros;

    function __construct()
    {
        session_start();
        if(!empty($_SESSION['erro']))   // Session não vazia (tem uma array dentro dela ent?o)
        {
            $this->erros = $_SESSION['erro'];
        }else                           // Session VAZIA (tem que COLOCAR uma array dentro dela)
        {
            $this->erros = array();
            $_SESSION['erro']  = $this->erros;
        }
    }

    private function destruct()
    {
        unset($_SESSION['erro']);
        unset($this->erros);
        $this->erros = NULL;
    }

    private function add($string)
    {
        $this->erros      = $_SESSION['erro'];  // pega mais um
        $this->erros[]    = $string;            // adiciona no array
        $_SESSION['erro'] = $this->erros;               // coloca de volta na session
    }

    public function add_error($data)
    {
        if(empty($data))return true;
        if(is_array($data))
        {
            foreach ($data as $value)
            {
                $this->add($value);
            }
        }
        else
        {
            $this->add($data);
        }
    }

    public function return_errors()
    {
        $erros = $this->erros;
        $this->destruct();
        return $erros;
    }

    public function check_ok()
    {
        return ($this->erros == NULL)?true:false;
    }

    public function show_errors()
    {
        $erros = $this->erros;
        print_r($erros);
    }
}
?>

Here is my puny attempt to convert it to use codeigniter sessions

<?php
function __construct()
{
    if(!empty($this->session->userdata('my_msgs'))) // Session n?o vazia (tem uma array dentro dela ent?o)
    {
        $this->msg = $this->session->userdata('my_msgs');
    }else                           // Session VAZIA (tem que COLOCAR uma array dentro dela)
    {
        $this->msg = array();
        $this->session->set_userdata('my_msgs', $this->msg);
    }
}

function destruct()
{
    $this->session->unset_userdata('my_msgs');
    $this->msg = NULL;
    unset($this->msg);
}

function add($string)
{

    $this->msg      = $this->session->userdata('my_msgs');      // pega mais um
    $this->msg[]    = $string;                                  // adiciona no array
    $this->session->set_userdata('my_msgs', $this->msg);        // coloca de volta na session
}

function add_msg($data)
{
    if(empty($data))return true;
    if(is_array($data))
    {
        foreach ($data as $value)
        {
            $this->add($value);
        }
    }
    else
    {
        $this->add($data);
    }
}

function return_msg($keep = FALSE)
{
    $msg = $this->msg;
    if($keep == FALSE)
    {
        $this->destruct();
    }
    return $msg;
}

function check_ok()
{
    return ($this->msg == NULL)?true:false;
}

function show_msgs()
{
    $msg = $this->msg;
    print_r($msg);
}

sorry about the bad English, not a native speaker

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanhui7329 2013-07-27 17:37
    已采纳

    Note that your previous code is using native session driver (session_start()), while codeigniter is using cookies.

    If you just need to prepare some message, display and then destroy it immediately, flashdata would be probably fit.

    If you need to create your own custom class, just take a look this: http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html, One that most important is a way to get codeigniter instance:

    $var =& get_instance();
    

    end then now you can use CI session in your class:

    $var->session->set_userdata('something');
    

    Your class might be look like:

    <?php
     class Your_class {
        function __construct() 
        {
            $CI =& get_instance(); //get CI instance
    
            if( ! $CI->session->userdata('my_msgs'))
            {
                 $this->msg = $CI->session->userdata('my_msgs');
            } else {
                 $this->msg = array();
                 $CI->session->set_userdata('my_msgs', $this->msg);
            }
         }
    
         //and soon ...
    
    点赞 评论

相关推荐 更多相似问题