浅析phpwind9.0之cookies的生成机制解析(成功登陆后)

由于工作的需要,所以开始分析phpwind9.0论坛的部分功能的处理机制,这一节来分析cookies生成机制。

登陆页面进入这里:

进行处理,具体函数:

welcomeAction()

注意到这个函数的这一行:

$login->setLoginCookie($this->loginUser,$this->getRequest()->getClientIp(),$rememberme);

然后进入具体的处理细节,在

这里,具体内容:(设置登陆cookies)

    public function setLoginCookie(PwUserBo$userBo,$ip,$rememberme=0) {

       ……

       /* @var $userServicePwUserService */

       $userService = Wekit::load('user.srv.PwUserService');

       $userService->createIdentity($userBo->uid,$userBo->info['password'],$rememberme);

       return $this->welcome($userBo,$ip);

    }

 

首先进入,载入该类,然后查看

createIdentity成员函数:(创建登陆标识)

 

       public function createIdentity($uid, $password, $rememberme = 0) {

              $identity =Pw::encrypt($uid . "\t" . Pw::getPwdCode($password));

              return Pw::setCookie('winduser',$identity,$rememberme?31536000: NULL);

       }

 

到这里可以看到和网上:

http://blog.csdn.net/xxaqzy/article/details/4141219

这篇文章里面说的有点类似了(只可惜这个是7.3的,7.3存在一个问题,就是如果我使用的是https服务,而不是http服务,那么前台访问就有问题,无法访问,但是后台可以)

这里的getpwdcode函数在文件里,具体内容为:(密码加密存储$pwd就是通过用户登陆后获得的明文的密码,经过该函数后形成密文的密码

    public static function getPwdCode($pwd) {

       return md5($pwd. Wekit::C('site','hash'));

    }

参数中的Wekit::C('site', 'hash')为全站hash值,在程序安装的过程中产生,可以在配置文件()中找到,这样就明白了createIdentity中的encrypt函数的其中的一个参数变量(Pw::getPwdCode($password))的具体值了。

再来看Pw::encrypt这个函数,具体在中,内容为:(加密方法)

    public static function encrypt($str,$key='') {

       $key || $key = Wekit::C('site', 'hash');

       /* @var $security IWindSecurity*/

       $security = Wind::getComponent('security');

       return base64_encode($security->encrypt($str,$key));

    }

在Wind::getComponent('security')这一行代码中,通过获取实现IWindSecurity接口的组件来调用相应的成员函数,具体就是:这个文件,他实现了该接口,成员函数encrypt的具体函数如下:

    public function encrypt($string,$key,$iv='') {

       if ($string==='')return'';

       if (!extension_loaded('mcrypt')) {

           throw new WindException('[security.WindMcryptCbc.encrypt]extension \'mcrypt\' is not loaded.');

       }

       if (!$key|| !is_string($key)) {

           throw new WindException('[security.WindMcryptCbc.encrypt]security key is required. ',

              WindException::ERROR_PARAMETER_TYPE_ERROR);

       }

      

       $size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);

       $iv = substr(md5($iv?$iv:$key), -$size);

       $pad = $size - (strlen($string) % $size);

       $string .= str_repeat(chr($pad),$pad);

       return mcrypt_cbc(MCRYPT_DES,$key,$string, MCRYPT_ENCRYPT,$iv);

    }

基本都是php的关于mcrpyt模块的内置函数,至此,该函数分析完毕。

当该函数执行完后,pw.php对该函数的返回结果进行base64_encode,然后将这个函数的返回结果作为最终的pw.php文件中encrypt成员函数的最终返回值。

接着,我们回到的createIdentity成员函数:

       public function createIdentity($uid, $password, $rememberme = 0) {

              $identity =Pw::encrypt($uid . "\t" . Pw::getPwdCode($password));

              return Pw::setCookie('winduser',$identity,$rememberme?31536000: NULL);

       }

然后进入文件中的setCookie成员函数,进行对cookies的设置工作:

    public static function setCookie($name,$value= null,$expires= null,$httponly=false) {

       $path = $domain = null;

       if ('AdminUser'!=$name) {

           $path = Wekit::C('site','cookie.path');

           $domain = Wekit::C('site','cookie.domain');

       }

       $pre = Wekit::C('site','cookie.pre');

       $pre && $name = $pre . '_' . $name;

       $expires && $expires += self::getTime();

       return WindCookie::set($name,$value,false,$expires,$path,$domain,false,$httponly);

    }

这个主要就是取得cookies的名字,其中有一个操作就是通过配置文件取得cookies的前缀,与传入的cookies名字进行组合形成最终的cookies名,然后调用

该类的构造函数set:

    public static function set($name,$value= null,$encode=false,$expires= null,$path= null, $domain= null, $secure = false, $httponly = false) {

       if (empty($name))return false;

       $encode && $value && $value= base64_encode($value);

       $path = $path ? $path : '/';

       setcookie($name, $value, $expires, $path, $domain, $secure, $httponly);

       return true;

    }

利用php内置的setcookie函数来设置cookies值,这样就形成了登陆后我们看到的最终的cookies值了。

没有用流程图一一的画出来,其实涉及到的函数也并不多,大家看着在纸上画一画也就ok了,嘿嘿。

本文主要参考了

1.phpwind官方手册:http://wiki.open.phpwind.com/index.php?title=%E9%A6%96%E9%A1%B5

2.Phpwind登录Cookie分析:http://blog.csdn.net/xxaqzy/article/details/4141219

转载请注明出处:http://blog.csdn.net/jayxujia123/article/details/35780867

作者邮箱:xujiaphp@gmail.com



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值