1 of 2
1
Using Session Extension
Posted: 17 February 2006 05:20 PM   [ Ignore ]  
Research Assistant
RankRankRank
Total Posts:  332
Joined  10-11-2004

I want to add the custom member profile data to the session so I added an extension for the sessions_start hook like this to test it out.

// -------------------------------
//  called by session start hook so that user preferences are available
// -------------------------------

function member_data(&$data) {
      
        
// just a bit of test data to see if it works
        
$data->userdata['date_format']="%d %m %y";

        return
$data;

} //member_data

I know that this code is being called because I put an echo in, but my addition to userdata does not appear when I do a print_r($SESS) later.  Brain has come to a halt on this one so any help would be very welcome.

Profile
 
 
Posted: 18 February 2006 12:36 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
RankRankRank
Total Posts:  332
Joined  10-11-2004

Tried everything I can think of to update the session, tried using the hook in mod.member_auth.php as well and no joy. 

After hours and hours of frustration confused angry  shock , this is how I got it to work, but it required a hack to core.session.php to add the returned $edata values to the session object.

// -------------------------------------------
        // 'sessions_end' hook.
        //  - Modify the user's session/member data.
        //  - Additional Session or Login methods (ex: log in to other system)
        //
        
$edata = $EXT->call_extension('sessions_end', $this);
        if (
is_array($edata)) {
             
foreach ($edata as $key=>$val) {
                 $this
->$key=$val;
             
}
        }         
if ($EXT->end_script === TRUE) return;


Here is the code in the extension that adds the values in the custom member profile:

// -------------------------------
//  called by session end hook so that user preferences are available
// -------------------------------

function member_data($data) {
    
global $DB;

   
    
// load member custom fields if member logged in
   
$profile='';

    if (
$data->userdata['member_id']>0) {
         
         $sql
="SELECT m_field_id,m_field_name FROM exp_member_fields";
         
$query = $DB->query($sql);

         
$sql2="SELECT ";
         if (
$query->num_rows > 0)  {
               
foreach ($query->result as $row) {
                      $sql2
.='m_field_id_'.$row['m_field_id'].' as '.$row['m_field_name'].',';
               
}
        
              $sql2
=substr($sql2,0,-1)." FROM exp_member_data WHERE member_id=".$data->userdata['member_id'];
                    
$query2 = $DB->query($sql2);

              if (
$query2->num_rows > 0)  {
              $row
=$query2->result[0];
                      unset(
$profile);
                      foreach (
$row as $key=>$value) {
                           $profile[$key]
=$value;
                      
}
              }
        }

   }

return $profile;
    
   
} //member_data

If anyone knows how to make this work without hacking core code I would love to know!

Profile
 
 
Posted: 18 February 2006 12:46 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
RankRankRank
Total Posts:  332
Joined  10-11-2004

And all that so I could display dates in a format specified by the users (I bet someone is going to tell me there is a far easier way of doing this).

Entry Added: {entry_date format="<?php global $SESS; echo $SESS->display_date_format; ?>"}

PHP has to be on and Input for this template.

Profile
 
 
Posted: 18 February 2006 02:39 PM   [ Ignore ]   [ # 3 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  7534
Joined  08-05-2002

Well, your first post mentions sessions_start but your second one mentions sessions_end.  You definitely should be using the latter.  I just ran a quick extension test and used this as the function being called:

function test(&$data)
{
$data
->userdata['word'] = "pMachine";
}

I did some print_r()‘s later in the code and the new userdata member shows up fine.

 Signature 
Profile
 
 
Posted: 18 February 2006 04:57 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
RankRankRank
Total Posts:  332
Joined  10-11-2004

Aghhhhhhh

Can’t believe that was one of the 100 permutations I didn’t tried.  Knackered and doing to bed now will try tomorrow. 

Thanks Paul.

Phoebe.

Profile
 
 
Posted: 23 February 2006 08:16 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
RankRankRank
Total Posts:  332
Joined  10-11-2004

Paul,

No I cannot get it to work.  I thought maybe it was something to do with passing an object, but I can’t get an array to work either. I am currently suspecting a PHP bug but there is still the chance I am being really stupid!

Here is my test.  If it works there should be an element [two] => bananas at after—-3——

Code in core.session.php

echo '-------------------1--------------------';

        
$myarray['one']='apples';
        
print_r($myarray);
        
        echo
'-------------------2--------------------';
        
$edata = $EXT->call_extension('sessions_end', $myarray);
        echo
'-------------------3--------------------';

        
print_r($myarray);

Code in core.extensions.php

echo '------------------a------------------';
        
$args['two']='oranges';
        
print_r($args);
        echo
'------------------b------------------';
$this->last_call = call_user_func_array(array(&$this->OBJ, $method), $args);
        echo
'------------------c------------------';
        
print_r($args);

my extension

function member_data(&$data) {
    
global $DB;

$data['two']= "bananas";

echo
'x';print_r($data);echo 'x';

return
false;

And this is what I get:

-------------------1--------------------Array
(
    
[one] => apples
)
-------------------
2--------------------------------------a------------------Array
(
    
[0] => Array
        (
            
[one] => apples
        
)

    
[three] => oranges
)
------------------
b------------------xArray
(
    
[one] => apples
    [two]
=> bananas
)
x------------------c------------------Array
(
    
[0] => Array
        (
            
[one] => apples
        
)

    
[three] => oranges
)
-------------------
3--------------------Array
(
    
[one] => apples
)

I did wonder if it was the change to the code in core.extensions to be able to pass arrays properly

$args=array_slice(func_get_args(), 1);
//$extra_args = array_slice(func_get_args(), 2);
//$args = is_array($args) ? array_merge($args, $extra_args) : array_merge(array($args), $extra_args);

so I changed it back and that messed up the array and didn’s solve the problem.:

-------------------1--------------------Array
(
    
[one] => apples
)
-------------------
2--------------------------------------a------------------Array
(
    
[one] => apples
    [two]
=> oranges
)
------------------
b------------------xbpplesx------------------c------------------Array
(
    
[one] => apples
    [two]
=> oranges
)
-------------------
3--------------------Array
(
    
[one] => apples
)

I am running PHP 4.3.8. 

For the moment will go back to my hack to get it working but if you have any ideas to try will be happy to give it a go.

Phoebe.

Profile
 
 
Posted: 23 February 2006 12:56 PM   [ Ignore ]   [ # 6 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  7534
Joined  08-05-2002

If I recall only objects will be passed by reference, so your array there will not work.

 Signature 
Profile
 
 
Posted: 14 July 2006 06:16 AM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  879
Joined  02-05-2002

Hi Paul,

Your code doesn’t work for me either:

function test(&$data)
{
    $data
->userdata['word'] = "pMachine";
}

If I do a print_r($this); in core.session.php your new data does not show up.

What I am trying to do is override the ‘language’ setting using the ‘sessions_end’ hook and the following code:

function swap_language(&$data)
{
    
global $IN;
    
$lang = $IN->global_vars['language'];
    
$data->userdata['language'] = $lang;
}

Any ideas what I’m doing wrong?

 Signature 

Member of the EE Pro Network

Profile
 
 
Posted: 14 July 2006 09:25 AM   [ Ignore ]   [ # 8 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  7534
Joined  08-05-2002

Unfortunately, this cool little bit of referencing only seems to work in PHP 5.  Otherwise, you will have to take a later hook and modify the $SESS object that way.

 Signature 
Profile
 
 
Posted: 16 July 2006 03:52 PM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  879
Joined  02-05-2002

“a later hook…”

Any suggestions?

 Signature 

Member of the EE Pro Network

Profile
 
 
Posted: 16 July 2006 10:32 PM   [ Ignore ]   [ # 10 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  7534
Joined  08-05-2002

Well, there are very few core files that have extension hooks because we are not comfortable with people modifying those files too much.  I cannot think of another hook that I can guarantee will always be called for a page.

 Signature 
Profile
 
 
Posted: 09 May 2007 04:23 PM   [ Ignore ]   [ # 11 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1184
Joined  01-05-2006

Is there any way we can move this to a feature request.  Maybe adding a hook right before any template parsing begins and then right after the template parsing ends?  It’d be great to have something like the control panel end hook.

 Signature 

================================================
    Mark Huot
    http://markhuot.com
================================================

Profile
 
 
Posted: 09 May 2007 05:52 PM   [ Ignore ]   [ # 12 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  7534
Joined  08-05-2002

That would have an extension hook in the core.system.php file, where the Template class is instantiated and called, which I definitely am not going to allow.

 Signature 
Profile
 
 
Posted: 10 May 2007 05:10 AM   [ Ignore ]   [ # 13 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1184
Joined  01-05-2006

That’s certainly reasonable Paul, but how about something in the `run_template_engine` method of the core.template.php file?  Maybe a `run_template_engine_start`, `run_template_engine_end` and `run_template_engine_absolute_end`?

Or even a hook in the language class would be helpful as that is instantiated after the session class and would help with the above problems.

 Signature 

================================================
    Mark Huot
    http://markhuot.com
================================================

Profile
 
 
Posted: 10 May 2007 09:39 AM   [ Ignore ]   [ # 14 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  7534
Joined  08-05-2002

Well, one, the Template parser will not always be called for a request.  Two, I am even less comfortable with the Template parser having hooks as it is the heart of the program.  One nice change there by a third party developer and every time we do support we will have to ask people to disable all extensions.  Not eager to enter that support mess for me or the TSSs.

I will try to think of an alternative that works for all though.

 Signature 
Profile
 
 
Posted: 10 May 2007 09:43 AM   [ Ignore ]   [ # 15 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1184
Joined  01-05-2006

Again, that makes sense.  I guess that’s why you’re paid the big bucks to think of these things for us. wink  I guess the main goal of my request would be to have some way to alter the session information before any computation begins.  Of course if every one was on PHP5 then this wouldn’t be a problem…alas.

 Signature 

================================================
    Mark Huot
    http://markhuot.com
================================================

Profile
 
 
Posted: 10 May 2007 09:46 AM   [ Ignore ]   [ # 16 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  879
Joined  02-05-2002

Forgive me for being somewhat slow today…

If this is such a sensitive subject, then why is this hook available in PHP5 but not in PHP4?
Right now you’ll have to ask people if they are using PHP5 first and if so ask them to disable all extensions wink

I’m confused red face

 Signature 

Member of the EE Pro Network

Profile
 
 
Posted: 10 May 2007 09:55 AM   [ Ignore ]   [ # 17 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1184
Joined  01-05-2006

I’ll defer to Paul on this one, but the hook is there in either version, and I believe that it’s just passing objects though functions by reference doesn’t work in PHP4.

 Signature 

================================================
    Mark Huot
    http://markhuot.com
================================================

Profile
 
 
Posted: 10 May 2007 10:01 AM   [ Ignore ]   [ # 18 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  7534
Joined  08-05-2002

Yes, the big bucks.  ::laughs nervously::  I understand the point and was rather annoyed to find out that the ability to reference the global was not available until PHP 5.  PHP and I are seeing a couples therapist currently.

The hook is available in PHP 4.  However, one cannot modify the object directly (only use its methods and variables) in PHP 4 because the arguments for the extension hook are not retrieved by reference.  I recall trying to find a way around this in the past but without any success.  Just had another idea, but it is a messy one and might not work.  Once you learn about references, you never want to be without them.  Of course, the PHP developers have a habit of changing how they work every so often, if any of you remember the PHP 4.4 fiasco (and the PHP 5 object copying annoyance too).

 Signature 
Profile
 
 
   
1 of 2
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 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64911 Total Logged-in Users: 28
Total Topics: 81862 Total Anonymous Users: 11
Total Replies: 440094 Total Guests: 171
Total Posts: 521956    
Members ( View Memberlist )