由于业务需要,现在要整合discuz和一套用cakephp写的程序,要求同步用户中心,同步登录。
思路很简单,dz下的api文件夹和uc_client拷到cakephp的webroot目录,用来和dz通信
cakephp这边做同步登录不准备直接修改登录退出代码,所以用插件的形式做,只需在原有登录退出的时候,调用下插件
用户通过cakephp应用登录退出时,通过插件得到同步登录退出的js代码(uc_user_synlogout),通知其他应用登录退出,
用户通过其他应用登陆退出时,uc.php接收其他应用的登录退出通知,在uc.php里面的2个函数登陆退出,
修改uc.php的synlogin和synlogout函数,在这里写cakephp的登录验证和退出.
流程就是这样,但是在实施过程中出现一个难题,
cakephp应用使用了框架自带的session机制,我们在uc.php里面不能获取到$_SESSION!!!
print_r($_SESSION);显示空值,但是在cakephp框架的控制器里面却可以显示,这个问题困扰了一段时间,
国内的资料有限,搜索完全找不到线索,只好把目光转向国外了,google搜索“cakephp session without”找到一点点信息,
不过还是没有实质进展,想想不对,在搜“cakephp session outside”,有了,这框架还是国外火啊。。。
解决如下:
Using Cake sessions outside of Cake
By brightball
While recently working on a CMS tool, I needed to pass some in-session information. I was using Cake's database sessions and it wasn't playing nice with outside applications so I set this up to allow my outside application to use Cake's session handlers.
The short version is that you need to make sure all of the path's are setup correctly, which happens in index.php.
Copy your index.php file into another file (I called it cake_session.php). This file needs to be in the webroot because index.php initializes the paths based on the location of webroot.
In your cake_sessions.php file find this line (should be line 86):
if (isset($_GET['url']) && $_GET['url'] === 'favicon.ico') {
And delete everything from there down. Now just add this code:
if(App::import('Core','Session')) {
$session = new CakeSession();
$session->start();
}
Check your $_SESSION variable to make sure everything works. You should be able to just include this file anywhere that you want to use your cake session.
原理很简单,就是在框架外载入框架初始化,并引用cakephp的seesion类。
需要注意的是,创建的这个新的文件,需要放在webroot下和index.php同级,
然后在到api目录下的uc.php中的synlogin和synlogout函数中引用这个文件,
并且,uc.php中不能写session_start();这会打乱cakephp的session机制,使之无法生效的。
总结:请学会google
声明:此文系舞林cuzn(www.wulinlw.org)原创稿件,转载请保留版权