http://blog.csdn.net/painsonline/article/details/8064937
- <span style="font-size:18px;"> $phpBB->careate_session($nick,$password);//和phpbb共享登录状态</span>
这几天在公司里面搞zen-cart,要求zen-cart商城和论坛能够同步注册和登录,其实同步注册修改个人信息zen-cart已经整合了,就是同步登录的问题没有,到网上找了一下,都是说搞一个新的页面,个人觉得不好,如果客户连接到别的页面,就会显示没有登录,怎样使客户在主站上登录了,就不用在论坛上登录了
大体的想法是这样的
关闭论坛的注册功能,将phpbb 的login_box函数的重定向指定到主站上,这样,只要提示用户注册或者登录,用户只能从zen-cart主站上进行
然后就是创建cookie和session的问题了
找到“zen-cart/includes/classes/class.phpbb.php"往该类增加三个函数
- <span style="font-size:18px;">/**--二次开发--**/
- function create_sess_id($len=32){//产生一个session_id
- list($u, $s) = explode(' ', microtime());
- $time = (float)$u + (float)$s;// 获取当前时间的微秒
- $rand_num = rand(100000, 999999);
- $rand_num = rand($rand_num, $time);
- mt_srand($rand_num);
- $rand_num = mt_rand();// 产生一个随机数
- $sess_id = md5( md5($time). md5($rand_num) );// 产生SessionID
- $sess_id = substr($sess_id, 0, $len);
- return $sess_id;// 截取指定需要长度的SessionID
- }
- function get_userid_by_username($nick){//通过用户名获得用户ID
- $sql="select `user_id` from ". $this->phpBB['users_table'] ." where `username`='".$nick."'";
- $result=$this->db_phpbb->Execute($sql);
- $user_id='';
- while (!$result->EOF) {
- $user_id=$result->fields['user_id'];
- $result->MoveNext();
- }
- return $user_id;
- }
- function careate_session($username,$password){//产生一个phpBB的session
- if(!$username || !$password){
- return ;
- }
- $sessionIp='';
- $cookie_path='';
- $cookieName='';
- $session_id=$this->create_sess_id(32);
- $sql="select * from `".$this->phpBB['config_table']."` where `config_name` in ('cookie_domain','cookie_name','cookie_path')";
- //exit($sql);
- $result=$this->db_phpbb->Execute($sql);
- while (!$result->EOF) {
- if($result->fields['config_name']=='cookie_domain'){
- $sessionIp=$result->fields['config_value'];
- }
- if($result->fields['config_name']=='cookie_path'){
- $cookie_path=$result->fields['config_value'];
- }
- if($result->fields['config_name']=='cookie_name'){
- $cookieName=$result->fields['config_value'];
- }
- $result->MoveNext();
- }
- //exit($cookieName.'--'.$sessionIp.'--'.$cookie_path);
- $userBrowserInfo= $_SERVER["HTTP_USER_AGENT"];
- $userIpAdd=empty($_SERVER["HTTP_CLIENT_IP"])?'127.0.0.1':$_SERVER["HTTP_CLIENT_IP"];
- $cmd_username=$username;
- $user_id=$this->get_userid_by_username($cmd_username);
- //ob_clean();//清除输出
- //zen_setcookie('cookie_test', 'please_accept_for_session', time()+60*60*24*30, '/', (zen_not_null($current_domain) ? $current_domain : ''));
- zen_setcookie($cookieName.'_u',$user_id,time()+100000000,'','',false);
- //往Cookies里插入SessionID信息
- zen_setcookie($cookieName.'_sid',$session_id,time()+100000000,'','',false);
- //往phpb_sessions表中插入Session信息
- $session_table=$this->phpBB['table_prefix'].'sessions';
- $sqlcommand="insert into ".$session_table."(session_id,session_user_id,session_last_visit,session_start,session_time,session_ip,session_browser,session_page)";
- $sqlcommand.=" values('$session_id',$user_id,'".time()."','".time()."','".time()."','$userIpAdd','$userBrowserInfo','index.php')";
- //exit($sqlcommand);
- $this->db_phpbb->Execute($sqlcommand);
- }</span>
找到“zen-cart\includes\modules\pages\login\header.php”转到大概97行
- <span style="font-size:18px;">$zco_notifier->notify('NOTIFY_LOGIN_SUCCESS');</span>
在这行上面添加如下代码表示登录时创建phpbb的Cookie和session
- <span style="font-size:18px;">//二次开发,同步登录
- if ($phpBB->phpBB['installed'] == true) {
- $phpBB->careate_session($email_address,$password);
- }
- //二次开发,同步登录</span>
找到“zen-cart\includes\modules\creat_account.php”在大概317行
- <span style="font-size:18px;">// phpBB create account
- if ($phpBB->phpBB['installed'] == true) {
- $phpBB->phpbb_create_account($nick, $password, $email_address);
- }</span>
内部加入以下代码
- <span style="font-size:18px;"> $phpBB->careate_session($nick,$password);//和phpbb共享登录状态</span>
- <span style="font-size:18px;">// phpBB create account
- if ($phpBB->phpBB['installed'] == true) {
- $phpBB->phpbb_create_account($nick, $password, $email_address);
- $phpBB->careate_session($nick,$password);//和phpbb共享登录状态
- }</span>
这样就可以了,祝你好运!