This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » legacy support » symfony 1.2 » the old "Who is online" function
the old "Who is online" function [message #97834] Thu, 22 April 2010 07:48 Go to next message
smugford  is currently offline smugford
Messages: 99
Registered: November 2009
Member
My google search turned up some pretty old entries on the who is online function stuff.

I need to implement a whosonline function in a 1.2 project and it just seems like one of those things that has been done over and over again.

Does anyone have a link to a plugin or tutorial that is not three years old?!

I want to copy what phpBB does which is saves the sessions in the database and then checks those sessions every five minutes.

From what i was able to find that's the start anyway.

Please let me know if you know of anything.

Thanks.
Re: the old "Who is online" function [message #97856 is a reply to message #97834 ] Thu, 22 April 2010 10:44 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
I'd start with using a database session manager - there's plugins for that - and then do a paged select on that (or a select top X) using a criteria on expiry columns (I am presuming that they are available - not actually done this myself).


Remember Palestine
Re: the old "Who is online" function [message #98002 is a reply to message #97856 ] Sun, 25 April 2010 00:02 Go to previous messageGo to next message
smugford  is currently offline smugford
Messages: 99
Registered: November 2009
Member
I've looked for a couple days now and can't find a "database session manager" plugin

anything specific?

thanks.
Re: the old "Who is online" function [message #98003 is a reply to message #98002 ] Sun, 25 April 2010 00:51 Go to previous messageGo to next message
smugford  is currently offline smugford
Messages: 99
Registered: November 2009
Member
i am using the sfGuardDoctrine plugin and in going over what i'm trying to do i see a "last_login" field in the database

this is one of the fields that i need for whosonline

i wonder if i should extend this table to save the session info?
Re: the old "Who is online" function [message #98006 is a reply to message #98003 ] Sun, 25 April 2010 07:49 Go to previous messageGo to next message
weaverryan  is currently offline weaverryan
Messages: 781
Registered: November 2007
Location: Nashville, TN
Faithful Member

Are there examples of this being done before?

It wouldn't be hard, but the answer is very low level - I had to do a little investigating.

First, as Halfer mentioned, you've got to use session based storage. This means changing your storage factory to use something like sfPDOSessionStorage.

You'd then need to grab all of the rows based on the sess_time column. Once you have all of these records, the sess_data field is what you'll need. This is either serialized, base64 encoded, or something else - you'll need to find the write tool to decode it.

Once you're that far, you should have an array of data for each of the records you selected. The user data should be located on a key called 'symfony/user/sfUser/attributes'. Inside that array, if the user is logged in, there should be a 'user_id' key, whose value is the sfGuardUser id for that user.

I could be WAY off on any of the above - I've never tackled this problem - I'm just quickly navigating my way through the code and making some observations.

Good luck!


Ryan Weaver
http://www.sympalphp.org
http://www.thatsquality.com
@weaverryan
Re: the old "Who is online" function [message #98008 is a reply to message #98006 ] Sun, 25 April 2010 16:41 Go to previous messageGo to next message
smugford  is currently offline smugford
Messages: 99
Registered: November 2009
Member
thanks guys,

i have step one in place thanks to this...

http://www.designdisclosure.com/2009/11/symfony-doctrine-dat abase-session-storage/

i had some trouble with it only because i was erasing the _col part at the end.

id_col

after a good nights rest i figured out the mistake in 5 minutes and it is now saving the sessions in the db fine.

on to the next part...
Re: the old "Who is online" function [message #98009 is a reply to message #98008 ] Sun, 25 April 2010 17:26 Go to previous messageGo to next message
smugford  is currently offline smugford
Messages: 99
Registered: November 2009
Member
it looks like only part of the data record in serialized so unserialize won't (or isn't for me anyway) work.

any thoughts?
Re: the old "Who is online" function [message #98012 is a reply to message #98009 ] Sun, 25 April 2010 18:45 Go to previous messageGo to next message
weaverryan  is currently offline weaverryan
Messages: 781
Registered: November 2007
Location: Nashville, TN
Faithful Member

Hey Scott-

Yea, the decoding was the only part I wasn't sure about - it didn't look like it was explicitly encoded by symfony, more that the native php session handlers give the data to symfony for saving already in that encrypted format. From everything I see, the data IS serialized, though apparently there are well-known problems with trying to unserialize data containing certain characters.

Check out the unserialize docs and search for session: us.php.net/manual/en/function.unserialize.php

Also, see session_decode (us3.php.net/manual/en/function.session-decode.php) which has more info in the comments.


Ryan Weaver
http://www.sympalphp.org
http://www.thatsquality.com
@weaverryan
Re: the old "Who is online" function [message #98021 is a reply to message #98012 ] Mon, 26 April 2010 00:35 Go to previous messageGo to next message
smugford  is currently offline smugford
Messages: 99
Registered: November 2009
Member
i ended up using solutions from a couple of different suggestions

step 1) store the database in the session

step 2) add the user_id field to the session id

i will probably add ip, etc after but the session id works fine for now for my who is online function..

http://forum.symfony-project.org/index.php/m/73916/?srch=sfP DOSessionStorage#msg_73916

// the post says to modify
sfPDOSessionStorage.class.php

$db_user_id_col	=	'user_id';
    $user	=	sfContext::getInstance()->getUser() ;
    $user_id	=	false ;
    if ($user->isAuthenticated()){
    	$user_id	=	sfContext::getInstance()->getUser()->getProfile()->getUserId();
    }

if ($user_id){
	    $sql = 'UPDATE '.$db_table.' SET '.$db_user_id_col.'= '.$user_id.', '.$db_data_col.' = ?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'= ?';		
	}
	else{
	    $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = ?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'= ?';		
	}


i can search through the session table now and do a join to get the user names.

not sure if that's the right thing to do in case i have to install symfony again but it's the only way i know how to do it at the moment.

bottom line is it works and does what i want. I'll just have to watch the upgrades.
Re: the old "Who is online" function [message #98024 is a reply to message #98021 ] Mon, 26 April 2010 02:04 Go to previous messageGo to next message
weaverryan  is currently offline weaverryan
Messages: 781
Registered: November 2007
Location: Nashville, TN
Faithful Member

Hey Scott-

A better solution would be to create a new subclass of sfPDOSessionStorage and then override the method you need to. That would do the trick without editing core code.


Ryan Weaver
http://www.sympalphp.org
http://www.thatsquality.com
@weaverryan
Re: the old "Who is online" function [message #98025 is a reply to message #98024 ] Mon, 26 April 2010 02:08 Go to previous messageGo to next message
smugford  is currently offline smugford
Messages: 99
Registered: November 2009
Member
i do no know how to create a subclass (or at least i think i don't anyway)

any docs on that?

thanks to everyone by the way i thought this was going to take way longer.
Re: the old "Who is online" function [message #98027 is a reply to message #98025 ] Mon, 26 April 2010 02:18 Go to previous messageGo to next message
weaverryan  is currently offline weaverryan
Messages: 781
Registered: November 2007
Location: Nashville, TN
Faithful Member

Hey Scott-

You can just create a file and put it in your project's lib/storage directory:
class myPDOSessionStorage extends sfPDOSessionStorage
{
  public function sessionWrite($id, $data)
  {
    //
  }
}


Then in your factories.yml file, point to your class for the storage class:

  storage:
    class: myPDOSessionStorage


And that should do it Razz


Ryan Weaver
http://www.sympalphp.org
http://www.thatsquality.com
@weaverryan
Re: the old "Who is online" function [message #98028 is a reply to message #98027 ] Mon, 26 April 2010 02:20 Go to previous message
smugford  is currently offline smugford
Messages: 99
Registered: November 2009
Member
thanks again!!!!
Previous Topic:Setting the default for sfWidgetFormDateTime
Next Topic:clearing sessions on logout
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software