Integrating forums with your own application and user base can be a real pain in the ass. I’ve done this in the past with Vbulletin and phpBB, but it’s really not that much fun. Both involved some degree of hacking of their core code which means problems when trying to upgrade (you have to remember every tiny thing you did to make it work). As both products improve and get more advanced you’d think they’d have built nice apis to make this job easier. Apparently not yet…

I’ve been pissing about with my own pet project ( http://www.bandresources.net ), and decided I’d like a forum too. Obviously I’d need single sign on. Was even considering building my own forum app from scratch so I would have to deal with the usual headaches, but then I discovered Phorum . OK, it’s been around since 1998, but it always seemed kind of fugly and basic compared to phpBB and VBulletin when I’d checked it out in the past. Turns out it is actually awesome - lean and fast, and it has a fricken API so that it’s really bloody easy to integrate with other apps!

Read on to find out how to integrate in a rediculously short time.

As I mentioned Phorum has it’s own API with hooks into all the important areas of functionality. To get external authentication up and running all I had to do was a few simple things.

  • Install phorum!
  • Alter my login script to sync the phorum_users table whenever a user logs in successfully. Don’t need to sync all fields, but make sure the record contains at least the user_id, username, displayname, email. Also the “active” field has to be set to “1″ or this won’t work.
  • Create a simple forum module that overrides phorum’s own check to see if you’re logged in. My code is below. How you get the user_id will depend on your own application. Drop this php file in the mods directory
  • Login to phorum admin panel. Go to Global Settings > Modules, and your module should be somewhere on that page. Turn it on!

After you have done this trying logging into your main site, then come back to phorum. If all is well it will show you are logged in! After that you just need to edit the templates of whatever theme you are using and change login/registration links to point to your the corresponding pages on the main site.

mod_userauth.php

  1. function userauth_session_restore($sessions)
  2. {//I found out how to do this here: http://www.phorum.org/phorum5/read.php?28,125993
  3.  
  4. if(!defined("PHORUM")) return;
  5.  
  6. /* phorum module info
  7. title: UserAuth
  8. desc: This is an external UserAuth module for Phorum. Going to use my own authentication
  9. version: 1.0.0
  10. release_date: Apr 14th, 2008
  11. url: http://www.bandresources.net
  12. author: xxx
  13. require_version: 5.2.2
  14. category: user_features
  15. hook: user_session_restore|userauth_session_restore
  16. */
  17.  
  18. /* I'm using a custom session handler in the main site. Fire this up to get my user_id */
  19. include_once SITE_PATH . "/classes/class.sessions.php";
  20. $ses_class = new Session();
  21. session_set_cookie_params(0);
  22. session_set_save_handler (array(&$ses_class, '_open'),
  23. array(&$ses_class, '_close'),
  24. array(&$ses_class, '_read'),
  25. array(&$ses_class, '_write'),
  26. array(&$ses_class, '_destroy'),
  27. array(&$ses_class, '_gc'));
  28.  
  29. session_start();
  30.  
  31. $user_id = $_SESSION['user_id'];  //If there's value for $_SESSION['user_id'] then I'm logged in
  32.  
  33. // If no user is logged in. Let's asume that the magic code would return
  34. // NULL in that case here. Note that if it would return FALSE, you could even
  35. // skip the whole if/then here and directly assign $user_id to the $sessions
  36. // array elements.
  37. if ($user_id === NULL) {
  38. $sessions[PHORUM_SESSION_LONG_TERM] = FALSE;
  39. $sessions[PHORUM_SESSION_SHORT_TERM] = FALSE;
  40. }
  41. // A user is logged in.
  42. else {
  43. //echo "Logged in!";
  44. $sessions[PHORUM_SESSION_LONG_TERM] = $user_id;
  45. $sessions[PHORUM_SESSION_SHORT_TERM] = $user_id;
  46. }
  47. //print_r($sessions);
  48. return $sessions;
  49. }

Extensive developer and API docs are available at http:/www.phorum.org/docs/html/

Phorum rawks.

Sphere: Related Content