Welcome Guest, Not a member yet? Register   Sign In
Session ID "memory" library
#1

[eluser]jppi_Stu[/eluser]
This might need some work in terms of code style, but functionally it seems OK. I put this together for my own use because I was relying on the session ID but things would break down if the session ID was regenerated. (This is different, of course, from the session expiring.) This library, autoloaded, allows looking back to the session ID that was last known, which may or may not be the current ID. (I couldn't find a way to get that information without this approach, but it's possible I missed something that would make this redundant.)
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter Session ID Memory Library
*
* CodeIgniter will keep session data until the session expires, but will
* regenerate the session ID more frequently (i.e., before the session
* expires).  This can cause a problem if you are expecting to have the
* session ID consistent from one request to the next -- such as inserting
* a hidden form field (such as an MD5 hash of something that includes
* the session ID) where entering data in the form may take longer than
* the life of the session ID.
*
* This library allows your code to look back one request, by using the
* flashdata feature of CodeIgniter sessions, to see what the session ID
* was for the prior request.
*
* It is STRONGLY recommended that this library be autoloaded if you need
* to use it at all.  The added burden to your application is minimal,
* and this will ensure the prior session ID is stored consistently.
*
* Sample usage:
*
* $this->load->library('sessionid_memory); // should autoload instead!
* $checkval_1 = md5($someval . $this->session->userdata('session_id'));
* $checkval_2 = md5($someval . $this->sessionid_memory->prior_id());
* if (($testval == $checkval_1) || ($testval == $checkval_2))
* {
*   // something good happens because the test passed
* }
* else
* {
*   // test failed, so do something about that
* }
*
* Note in the example above that $checkval_1 and $checkval_2 will be
* identical if the session ID hasn't been regenerated since the last
* request.
*
* @author Stuart Whitmore on behalf of Johnny Pixel Productions Inc.
* @version 1.0
* @license http://creativecommons.org/licenses/by/3.0 CC-BY 3.0
* @link http://www.johnny-pixel.com/ Johnny Pixel Productions, Inc.
*/

class Sessionid_memory
{
    public $CI;

    public function __construct()
    {
        if (!isset($this->CI))
        {
            $this->CI =& get_instance();
        }
        $this->
            CI->
                session->
                set_flashdata('prior_session_id',$this->
                    CI->session->userdata('session_id'));
    }
    
    public function prior_id()
    {
        return ($this->CI->session->flashdata('prior_session_id'));
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB