Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
know Users loggued
  • How i  Know the users loggued ?


  • With the standard Auth, you don't. It doesn't log logins, and it doesn't log activity.

    What probably the simplest solution is, is:
    - add an additional column "user_id" to the session record, and allow NULL
    - extended the Session DB driver
    - overload the "write" method, and check for ! empty($this->keys)
    - if so, add $this->keys["user_id"] = \Auth::get('id', null);
    - call parent::write( ... ) to execute the write

    You can then run queries on the Session table, using the "user_id" column and the timestamp of the "updated" column (you need to define "logged in" as "seen in the last 10 minutes" or so).

    This doesn't work for cookie based sessions, and for other than DB storage, you probably need to adapt this logic (querying might be more complex).
  • now the question is how i configure fuel to use database session and where i can modify the data that will be saved? 

    this is my driver

        object(Fuel\Core\Session_Cookie)[32]
    protected 'config' =>
    array (size=19)
    'driver' => string 'cookie' (length=6)
    'match_ip' => boolean false
    'match_ua' => boolean true
  • HarroHarro
    Accepted Answer
    How about the session.php config file? See http://fuelphp.com/docs/classes/session/config.html.

    You'll need to change the driver to 'db'.

    What you need to do to make the driver save additional data is described in my previous post. You'll find how to extend core classes in the docs as well.
  • this is my configuration file

    <?php 

    return array(
        'cookie_name'    => 'fueldid',
        'database'       => '',
        'table'          => 'users_sessions',
        'gc_probability' => 5
     );

    but the cuestion is how store the data in the session table?
  • this is my configuration file

    <?php 

    return array(
    'driver' => 'db',
        'cookie_name'    => 'fueldid',
        'database'       => '',
        'table'          => 'users_sessions',
        'gc_probability' => 5
     );
  • See my post above:

    What probably the simplest solution is, is:
    - add an additional column "user_id" to the session record, and allow NULL
    - extended the Session DB driver
    - overload the "write" method, and check for ! empty($this->keys)
    - if so, add $this->keys["user_id"] = \Auth::get('id', null);
    - call parent::write( ... ) to execute the write
  • I can do it in my controller after login check?

    if (Auth::check())Session::set('userid', $userid); }

  • What would you want to do with that?

    Auth itself already registers the user in the session, so no need to do that twice. Sessions however are personal, they can not be queried, so how would you list who is logged in?

    To be able to query it, it needs to be stored server side in a central place, and you must be able to query it. How is limited to your imagination, you can also record it in a separate table, in a redis or mongo db, or in memcached. You only have to think about the structure (remember, you need to query?) and do your garbage collection (remove stale data).

    Since the session driver does all this for you, for me it seems far easier to add a column to the session table than to create some custom solution. And you save on I/O, since the session is written anyway.
  • Ok. am trying to do it, I added a column to the session table but now I want to modify the code to write an extra field in the database.

    I think when user logued out or session espire this record is destroyed?

    please can you show me the way to do it?

    excuseme for my english, it hasn't a very good looking
  • Sessions are written at the end of the request, so no need to deal with that, after the user has logged out, the user_id written to the table with be 0 ( = a guest user).

    Read up on extending core classes: http://fuelphp.com/docs/general/extending_core.html#/extend_and_replace and start by extending the \Fuel\Core\Session_Db class, as described on that page, and overload the write method. Add some logging to it to see if it works, and make sure you call parent::write() to keep everything working.

    Once this works, and you have validated your extension is operational, use this:

    <?php
    /**
     * Part of the Fuel framework.
     *
     * @package    Fuel
     * @version    1.8
     * @author     Fuel Development Team
     * @license    MIT License
     * @copyright  2010 - 2014 Fuel Development Team
     * @link       http://fuelphp.com
     */

    // --------------------------------------------------------------------

    class Session_Db extends \Fuel\Core\Session_Db
    {
        /**
         * write the current session
         *
         * @access    public
         * @return    Session_Db
         */
        public function write()
        {
            // do we have something to write?
            if ( ! empty($this->keys) or ! empty($this->data) or ! empty($this->flash))
            {
                // add the current user
                $this->keys['user_id'] = \Auth::check() ? \Auth::get('id') : 0;
               
                // call the parent
                parent::write();
            }
           
            return $this;
        }
    }

  • Excelent!!, Thanks now i can write the user_id, every day i learn new things
  • the second thing I will do is make a new model (user_session) and it will be related with users model, the last one will retrieve only the user_id field  and with a simple validation i'll to know in the users list which users are currently logged.

    is this the correct way or there is another shortest way?
  • Ormauth or Simpleauth?

    In case of Ormauth, you need to extend the user model in your app (like you did with the session driver), because you need to define the relation both ways, and you should not make modifications to the Auth models directory.

    And yes, I would do it like that too...
  • I do something like that but am having an error 

    Fuel\Core\PhpErrorException [ Notice ]:
    Undefined index:

    <?php
    /**
     * Fuel is a fast, lightweight, community driven PHP5 framework.
     *
     * @package    Fuel
     * @version    1.7
     * @author     Fuel Development Team
     * @license    MIT License
     * @copyright  2010 - 2014 Fuel Development Team
     */

    //namespace Auth\Model;

    class Model_User extends Auth\Model\Auth_User
    {

    protected static $_belongs_to = array(
    'mailbox' => array(
           'key_from' => 'id',
           'model_to' => 'Model_Mailbox',
           'key_to' => 'user_id',
           'cascade_save' => true,
           'cascade_delete' => false,
       ),
    );

    protected static $_has_one = array(
    'sessions' => array(
    'key_from' => 'id',
    'model_to' => 'Model_Sessions',
    'key_to' => 'user_id',

    ),
    );

    }
  • SOLVED!

    I solvet it adding this to my controller 


    protected static $_properties = array('session_id', 'user_id');
    protected static $_primary_key = array('user_id');

    I think the error occur when the primary key of the table is not named as ID


    Thanks anyway... 
  • Every ORM model needs a primary key, and if you don't define it, it defaults to "id", so that is very well possible.

    I would not define "user_id" as PK though, since it doesn't have to be unique (a user can login with multiple browsers, or on multiple PC's), but the "session_id" is.
  • Another questions is when the user is logued and close the browser the session kept active,

    there is any session timeout, to close it automatically and remove the session record ?

    and when logout there is any function to clear session?
  • No, there is no mechanism, as session expiry is client-side.

    Usually the combination of both user-id and last-update date is taken to determine if a user is logged-in or not, for example, a user seen in the last 15 minutes is considered logged-in.
  •  was thinking it, this is a way .... I will tray it and will let you know
  • my code working

    // $sec['updated'] is the UPDATE field from session table

    if($sec['updated'] && explode(" ", Date::time_ago(strtotime(Date::forge($sec['updated'])->format("%m/%d/%Y %H:%M")), strtotime(date('d M Y h:i')), 'second'), 2)[0] < 300){ echo " ONLINE"; };?>

    Thanks for all now works great!


Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion