Re: Import Existing Sessions into CAKE

2006-09-12 Thread [EMAIL PROTECTED]

Mohan  me, we are working on the same project. And we have cracked the
Session sharing with cake and other applications. So a part of the
credit goes to him as well. memcached is a distant option for us, as we
cannot compile and install anything on the server(we will do that once
we have a dedicated server).

*warning* This is going to be long post. plus, spare my english.

Before going further, let me accept - this is an under-the-belt trick.
I welcome the gurus to better this code.
I have posted only the basic code to help anyone start off.

1. To inject a session into PHPBB (or any other application, even if it
doesnt follow OOP class methods).
create a vendor class in cake (/app/vendors/phpbb.php).
add the following code in that.

class PHPBB {

var $userdata;
var $sessionid;

function login($user) {
global $db, $phpbb_root_path, $phpEx, $userdata, $board_config,
$userdata, $user_ip;
$phpbb_root_path = PHPBB_FORUM_PATH; //add it in your core.php
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
$this-userdata = $userdata;
$autologin = ( isset($HTTP_POST_VARS['autologin']) ) ? TRUE : 0;
$admin = (isset($HTTP_POST_VARS['admin'])) ? 1 : 0;
$this-sessionid = session_begin($user, $user_ip, 0, FALSE,
$autologin, $admin);
}

function logout($user) {
global $db, $phpbb_root_path, $phpEx, $userdata, $board_config,
$userdata, $user_ip;
$phpbb_root_path = APP . 'webroot' . DS .'forums' . DS;
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
$userdata = session_pagestart($user_ip, PAGE_INDEX);
session_end($userdata['session_id'], $userdata['user_id']);
}

}

2. in your controller (eg: users_controller.php) call vendors('phpbb');
3. after authenticating the user, before other session initialisation,
add this
$bb = new PHPBB();
$bb-login($this-User-data['User']['id']);
unset($bb);
// ... other code follows,

4. however, before adding any other session data, you have to do this.
because phpbb has polluted your session. (if someone has a better
technique, let me know)

$this-Session-renew();
$this-Session-write('User.id', $this-User-data['User']['id']);
...
...

Thats it.

Now if you have to inject a CAKE Session from another application, its
a little more complex and its not fully tested. So I will be brief with
this one.
1. create a cake_inc.php in your app/config (you can put it anywhere,
but I think config is the best place)
2. add these defines and includes in that
if (!defined('DS')) {
 define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('ROOT')) {
 define('ROOT', dirname(dirname(dirname(__FILE__;
}
if (!defined('APP_DIR')) {
 define('APP_DIR', basename(dirname(dirname(__FILE__;
}

if (!defined('CAKE_CORE_INCLUDE_PATH')) {
 define('CAKE_CORE_INCLUDE_PATH', ROOT);
}
if (!defined('WEBROOT_DIR')) {
 define('WEBROOT_DIR', basename(dirname(__FILE__)));
}
if (!defined('WWW_ROOT')) {
 define('WWW_ROOT', dirname(__FILE__) . DS);
}
if (!defined('CORE_PATH')) {
 if (function_exists('ini_set')) {
  ini_set('include_path', ini_get('include_path') . 
PATH_SEPARATOR .
CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS);
  define('APP_PATH', null);
  define('CORE_PATH', null);
 } else {
  define('APP_PATH', ROOT . DS . APP_DIR . DS);
  define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
 }
}

include APP_PATH . 'config' .DS  .'core.php';
include CAKE_CORE_INCLUDE_PATH .DS . 'cake' . DS . 'basics.php';
include CAKE_CORE_INCLUDE_PATH .DS . 'cake' . DS . 'config' . DS.
'paths.php';
include CAKE_CORE_INCLUDE_PATH .DS . 'cake' . DS . 'libs' . DS .
'object.php';

3. I am interested in injecting a session into cake, nothing else. add
these following lines:
include CAKE_CORE_INCLUDE_PATH .DS . 'cake' . DS . 'libs' . DS .
'security.php';
include CAKE_CORE_INCLUDE_PATH .DS . 'cake' . DS . 'libs' . DS .
'session.php';

4. Now you can call this cake_inc.php in your other application. In
phpBB, add at the top of page in login.php
include PATH_TO_CAKE_CONFIG . '/cake_inc.php';
after successful login , add this
$cakesession = new CakeSession(null);
$cakesession = new CakeSession(null);
$cakesession-renew();
$cakesession-writeSessionVar(User.id,$row['user_id']);

5. Now go to your cake app's home page, you can see that user is logged
in.

6. It would be worthwhile to poke around and see if you can launch a
controller or a model like this. then it would be easy to use cake 

Re: Import Existing Sessions into CAKE

2006-09-11 Thread Cheeze

Bumps for you. I would be very interested to know how to do this as
well.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~--~~~~--~~--~--~---



Re: Import Existing Sessions into CAKE

2006-09-11 Thread Chris Hartjes

On 9/8/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

 Hi,

 I have a PHPBB forum and CAKE application. phpBB is installed within a
 folder inside cake. cake and phpbb have database sessions.

 I am trying to unify login process so that users logging in from phpbb
 dont need to login again when coming to the cakeapp. Also, if the user
 logged in cake-app and then visits phpbb, the login should be
 maintained.

 But when I sign in phpbb and then goto cake app, I get a totally
 different session id. I tried setting the $_SESSION variable in phpbb
 (following another post in this group itself). However its not
 accessible in cake.

Hi there.  I understand what you're trying to do, but sessions are not
the way to do this.  What you really need is a caching system like
memcached.  Sessions can't be shared across different applications
*unless* you create your own custom session handler.  Personally, I've
used memcached for the type of thing you're talking about it and it
worked out really well.

Check out www.dango.com/memcached for more info.  There is an
extension you can compile for memcached, and I believe there are a few
memcached clients written in PHP.

-- 
Chris Hartjes

The greatest inefficiencies come from solving problems you will never have.
-- Rasmus Lerdorf

@TheBallpark - http://www.littlehart.net/attheballpark
@TheKeyboard - http://www.littlehart.net/atthekeyboard

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~--~~~~--~~--~--~---