1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Having No Session

Discussion in 'XenForo Development Discussions' started by digitalpoint, Nov 15, 2011.

  1. digitalpoint

    digitalpoint Well-Known Member

    I'm trying to figure out the best way that I could serve a page via the XenForo framework, but not initialize a XenForo session for the user...

    There are various content we serve through our servers without the user actually coming to our site. It seems silly to waste the resources to create a session when there is no use for it since the user isn't actually ON our site. Plus having an count of 50,000 guests online would look silly on top of it. lol

    Some examples of what I mean...

    Image that people use on their sites... underneath it all, it's not a static image... it's hitting the database because it's logging visitors to their site (sort of our Google Analytics uses a 1x1 image to track users).


    http://ads.digitalpoint.com/ads.php is used for ad delivery to sites using Digital Point Ads...

    Like I want to be able to use the framework for caching and database, but no need for a session for users not actually ON the site.

    Looking for the equivalent of this in vBulletin:
  2. ragtek

    ragtek Guest

    Hm, why not overwrite
    protected function _setupSession($action)
            if (

    in your controller?;)
    Floris likes this.
  3. Mike

    Mike XenForo Developer Staff Member

    If you're looking for raw speed, just skip the whole C and V of the framework. Look at examples like cron.php and css.php.
  4. digitalpoint

    digitalpoint Well-Known Member

    This is what I ended up doing for a page that I wanted to be able to use the basic XF framework (auto-loading classes for example). I needed to use the cache framework, but did not need things like sessions or database access... hopefully it helps someone.

    $startTime microtime(true);
    $fileDir dirname(__FILE__);
    $fileDir '/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($fileDir '/library');
    XenForo_Application::initialize($fileDir '/library'$fileDirfalse);
    // We need cache
    XenForo_Application::getInstance()->addLazyLoader('cache', array(XenForo_Application::getInstance(), 'loadCache'), XenForo_Application::getInstance()->loadConfig()->cache);
    $cacheObject XenForo_Application::get('cache');
  5. account8226

    account8226 Guest

    Hey dude,

    Have you found a way not to start session on an external page :sick: ?

    I don't really get the example above. I'm not using an external page, but it's in one of my add-on controller.

    In fact I would like to load the session, but not to show the member in user online.

  6. digitalpoint

    digitalpoint Well-Known Member

    The session is started before the controller, so you can't stop the session creation for a specific controller.
  7. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Couldn't you override XenForo_Controller::_setupSession() in your controller?
    digitalpoint likes this.
  8. digitalpoint

    digitalpoint Well-Known Member

    I believe you are right... I was thinking more in the context of extending a controller and not initializing the session for just some actions. But yes... I believe you could shut down session init for an entire controller if you needed.

    Sorry, I had my brain wrapped around the idea of what *I* was trying to do (have a certain controller action that was just serving an image and didn't need a session/authentication).
  9. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    You should be able to do it for a single action too. When _setupSession() runs you have access to the routeMatch in the controller. So in your extended _setupSession() function you can check the action from the routeMatch and conditionally setup the session:

    	protected function _setupSession($action)
    		$action = $this->_routeMatch->getAction();
    		if ($action == 'youraction')
    			// NOTHING
    Untested, but it should work.
    digitalpoint likes this.
  10. digitalpoint

    digitalpoint Well-Known Member

    Oh yeah... I suppose that *would* work. :) Totally feeling stupid right now. hah
  11. digitalpoint

    digitalpoint Well-Known Member

    Update - okay, I went to see about changing some stuff to use a controller without any queries/sessions.

    Ideally you would just want to override XenForo_ControllerPublic_Abstract::_preDispatchType(), but you can't since it's a "final" method.

    The _setupSession, _assertViewingPermissions, _assertNotBanned, _isDiscouraged all need to be overridden since they all trigger a query that reads permissions for a guest user.

    But even when you do that, XenForo_Dependencies_Public::preRenderView() also triggers the query for guest permissions, and then you start getting into issues where you need to create a whole new listener to override the front_controller to get rid of that query.

    So in the end, it *probably* can be done (running something in a public controller that requires no queries), but it starts getting rather complicated, and really not worth it in my opinion.

Share This Page