Part of the EllisLab Network
   
1 of 8
1
CI session lib replacement : OBSession
Posted: 25 March 2007 07:00 AM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  472
Joined  09-26-2006

Hi all.
I originally posted to the Code and Application Development forum. So I apologize for
the duplication. Since we now have a dedicated forum for this sort of thing, it would
be better if anything relating to my replacement session lib were posted here.

The original thread is here : http://codeigniter.com/forums/viewthread/48340/

You can view the online docs here http://bleakview.orgfree.com/obsession/

The replacement session lib can be downloaded from here:
http://bleakview.orgfree.com/dl/obsession.zip

The wiki entry is here http://codeigniter.com/wiki/OB_Session/
It does not add anything to what can be found in the online docs.
EDIT: 15 April 2007 : I have updated the class. see http://codeigniter.com/forums/viewreply/245333/

Regards

 Signature 

Old programmers never die, they just parse away.

Profile
 
 
Posted: 25 March 2007 08:16 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  347
Joined  05-29-2006

I suggest the use of obsession: very best code compatible 100% with CI session library, but most efficient… Thank you
Oscar

 Signature 

CI Js_calendar plugin click

WYSIWYG with CI

Profile
 
 
Posted: 25 March 2007 09:27 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  472
Joined  09-26-2006

Thank you for the Thumbs UP grin

 Signature 

Old programmers never die, they just parse away.

Profile
 
 
Posted: 25 March 2007 08:36 PM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  92
Joined  01-29-2007

Good work Oscar!

I need some help. I’m using OBSession on a fresh install of CI 1.5.2 and PHP 4.4.4

I am trying to create a library that will give me simple login functionality (I realize there are a ton of these libraries, I am just looking for a real, real simple solution).

My problem is that when I try to set session data inside my library, it looks like I end up with two instances of Session data. Let me give you the related code to see if you can see what is happening.

My library is call Simplelogin.

I am autoloading ‘database’, ‘session’, ‘simplelogin’

In my main controller, my index function is as follows:

function index()
    
{
    
//Set session data using the controller instance
    
$this->session->set_userdata('my_controller', 'controller_value');
        
    
//Set session data using the library instance
    
$this->simplelogin->simple_test();
            
        
print_r($this->session->userdata);
        
        echo
'<br /><br />'';

        print_r($this->simplelogin->CI->session->userdata);
    
    }

In my Simplelogin library:
My initialization looks like this:

function Simplelogin()
    
{
        $this
->CI =& get_instance();
    
}


My simple_test function is as follows:

function simple_test() {
        $this
->CI->session->set_userdata('my_library', 'library_value');
    
}


I delete cookies, then run the code and the first time I get this:

Array ( [my_controller] => controller_value )

Array ( [my_library] => library_value )

And every other time, I get this:

Array ( [my_library] => library_value [my_controller] => controller_value )

Array ( [my_library] => library_value )

I am expecting them to be the same, but obviously they are not. What am I missing? How would I set session data in a library and then call that session data in my main controller? Or vice-versa (on the second reload the session data gets added to the controller instance, but the library instance never sees the controller values)?

One work around that I have thought of that works is to create alias functions in my simplelogin library that would point to the session functions. In other words, every time I set_userdata I would do it through my simplelogin library (), which does not seem right (obviously I am doing something wrong). I guess another option would be to just set_userdata like this:

$this->simplelogin->CI->session->set_userdata('my_controller', 'controller_value');


but that doesn’t seem right either.

I don’t think this is an OBSession issue (I had the exact same problem with DB_Session), I just thought you might be able to quickly see what I am doing wrong.

Any thoughts or help would be greatly appreciated.

Profile
 
 
Posted: 26 March 2007 03:11 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  472
Joined  09-26-2006

Sorry, time is not with me today.
EDIT : Amended the two code snippets slightly.
I have not checked your code in detail, but this works for me:

Controller

function index()
    
{
        
//Set session data using the controller instance
        
$this->session->set_userdata('controller_call', 'controller_value');

        
//display the userdata using the controller instance
        
print_r($this->session->all_userdata());
        
        echo
'<br /><br />';
        
        
//Set session data using the library instance
        
$this->simplelogin->simple_test();
        
        
//Display the session userdata, using the library instance;
        
$this->simplelogin->simple_display();
    
}


Simplelogin lib

class Simplelogin
{
    
var $CI = '';
    
    function
Simplelogin()
    
{
        $this
->CI =& get_instance();
    
}
        
    
function simple_test()
    
{
        $this
->CI->session->set_userdata('library_call', 'library_value');
    
}
        
    
function simple_display()
    
{
        print_r
($this->CI->session->all_userdata());
    
}

results:
Array ( [my_controller] => controller_value [my_library] => library_value )

Array ( [my_controller] => controller_value [my_library] => library_value )

PS.
This line in your controller code puzzled me heckless:
print_r($this->simplelogin->CI->session->userdata);

 Signature 

Old programmers never die, they just parse away.

Profile
 
 
Posted: 26 March 2007 08:06 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  17
Joined  03-10-2007

Sorry guys. I still don’t understand what this flash data for ?

This is from the manual:
“For those not accustomed to the luxury, you can set “flash data”, a message which is only saved for one page load. These flash messages are often used after a login or redirect, to display a message just once.”

I understand the login example. This could be something like “Welcome back Mr. Norris” when the user logs in for the first time. Why would you use it for a redirect ? Can somebody please a practical example on how to use flash data?

thanks alot…

Profile
 
 
Posted: 26 March 2007 08:35 PM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006
new_shocki - 26 March 2007 08:06 PM

I understand the login example. This could be something like “Welcome back Mr. Norris” when the user logs in for the first time. Why would you use it for a redirect ? Can somebody please a practical example on how to use flash data?

Disclaimer: Some of this syntax might be a little off, it’s hard to tell when I am editing without colors. wink

One example I can think of is downloading a file, without using a unique url, as a security measure. Imagine you list all your files in a private area. You want to provide a url that is useless without session data, like http://mysite.com/file/download . Now, you build a page that is password protected, and when the page loads, you do:

$this->session->set_flashdata('download', 47); // Second parameter is db row id

Now, in my html, I can just output:

<?= anchor('file/download', 'Download this file') ?>

My download function could look like this:

function download() {
  
// This next line is made up, but it would check to make sure
  // this is a real user and that they are properly authenticated
  
if (! $this->auth->valid()) redirect ('login');
  
  
// Here's the real magic
  
$this->load->helper('download');
  
$this->load->model('download');
  
  
// Fetch our id
  
$id = $this->session->flash('download');
  if (
$id == false)
    
redirect('error/invalid_file');
  
  
// Fetch the filename
  
$file = $this->download->get_filename($id);
  
// Make sure the file exists!
  
if ($file == false || ! file_exists("./secure/downloads/$file"))
    
redirect('error/file_not_found'); // My personal 404 page
  
  // Fetch data for the download helper, get basename
  
$data = file_get_contents($file)
  $file
= basename($file)

  
// Execute download helper
  
force_download($file, $data)
}

This gives me three benefits: 1) the user *must* visit the page with the download link, 2) I can be sure that the user is authorized to be viewing the page, 3) You are limited to exactly one download per page load, because the flash data is only around for one load.

That is the power of flash data.

Edit: Now that I think about it, this is rather good idea, I may have to write a mini-app for this.

Edit: This is OT, but I’ve always interpreted flash data as sticking around, until it is accessed once.  This lets me redirect the user between several pages, but deletes the data as soon as I access it. I’m fairly sure obsession does it the right way (one load), but I think prefer the other way, it seems less frustrating as a developer, for the cases when one load is not enough. If obsession ever considers making an option for that in the config (to use one load or access), I would seriously consider using it.

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 27 March 2007 03:13 AM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  17
Joined  03-10-2007

Hey Shadowhand. This is a very good example (I am shure I can use this some time). But this is kind of special. What would be the general purpose of using flash ?

Profile
 
 
Posted: 27 March 2007 03:18 AM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

I wasn’t aware there was such a thing as “general purpose” in programming. wink Flash data is a tool for a very specific job.

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 27 March 2007 03:31 AM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  17
Joined  03-10-2007

Shadowhand. I tried a bit of Ruby on Rails back then which also had a flash function. But this was acting a bit differently. It displayed some message before redirecting somewhere. some thing like
redirect (url, message, seconds); for example “thank you for submitting your form”. After x seconds you were redirected.

I think this was really handy and more of a general purpose thing. So maybe I was a bit confused by those to principles.

Profile
 
 
Posted: 27 March 2007 03:40 PM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  472
Joined  09-26-2006

@Shadowhand

This is OT, but I’ve always interpreted flash data as sticking around, until it is accessed once.  This lets me redirect the user between several pages, but deletes the data as soon as I access it. I’m fairly sure obsession does it the right way (one load), but I think prefer the other way, it seems less frustrating as a developer, for the cases when one load is not enough. If obsession ever considers making an option for that in the config (to use one load or access), I would seriously consider using it.

OB Session implements flashdata as proposed by Dariusz in NativeSession.(and in DB_Session)
I take it you are not talking about using

keep_flashdata($key)

to retain flash messages for
x number of loads, but of a different approach to implementation?

I am perfectly open to suggestions for improvements, I just wanted to get something out which is as
compatible as possible with existing usage.

The other things I’m mulling over are barbazul’s suggestion, for sessions with cookie disabled.
And allowing setting of a “httponly” parameter for the cookie.

 Signature 

Old programmers never die, they just parse away.

Profile
 
 
   
1 of 8
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 66428 Total Logged-in Users: 40
Total Topics: 84791 Total Anonymous Users: 3
Total Replies: 455015 Total Guests: 231
Total Posts: 539806    
Members ( View Memberlist )
Newest Members:  X_franllogocsaturkeyPeter BryanttherendStudioGeorgiaJZeerfedegheEdgedcentice