Cakephp 3转换cakephp 2饼干

tra*_*nte 16 php cookies cakephp cakephp-3.0

我们需要在Cakephp 3中阅读Cakephp 2用户cookie.但似乎在更新期间,CakeCookie标签已从cookie中删除.所以每个Cakephp cookie都是分开的.关于这里,Cakephp 3可以读取旧的cookie,如果它们是用AES编写的.但在旧的cakephp中,默认选项是密码,我们的cookie是用密码编写的.所以我们现在不能读旧饼干.

可以选择在Cakephp 2中读取旧cookie并使用AES更新它们.但是当我们更新到Cakephp 3时,会有用户拥有Cakephp 2 cookie.所以我们需要在Cakephp 3中查看这个.

我们计划在cakephp 3中使用密码读取旧cookie,并将其值写入新cookie.我们使用了安全密码方法的代码,并创建了以下代码.

问题是,这段代码适用于字符串,但它在一些嵌套数组上失败了.问题可能是加号或任何其他编码问题.有时候它有效,有时则不然.

我分享了我们的代码来展示我们的流程.您可以提出另一种转换旧cookie的方法.

if (isset($_COOKIE['CakeCookie'])) {
    if (is_array($_COOKIE['CakeCookie'])) {
        foreach ($_COOKIE['CakeCookie'] as $key => $value) {
            $valueDecr=$this->decryptSaltedData($value);
            $this->Cookie->configKey($key, ['expires' => '+60 days']);
            $this->Cookie->write($key, $valueDecr);

            $this->Cookie->delete("CakeCookie[$key]");
            unset($_COOKIE['CakeCookie'][$key]);
            setcookie("CakeCookie[$key]", "aaa", time()-3600, '/');
            setcookie("CakeCookie[$key]", "aaa", time()-3600, '/', '.example.com');
        }
    }
}

public function decryptSaltedData($data) {
    $data2 = substr($data, 8);
    $text = base64_decode($data2);
    $key = Security::salt();

    $cipherSeed='45454545454545454545';
    srand($cipherSeed);

    $out = '';
    $keyLength = strlen($key);
    for ($i = 0, $textLength = strlen($text); $i < $textLength; $i++) {
        $j = ord(substr($key, $i % $keyLength, 1));
        while ($j--) {
            rand(0, 255);
        }
        $mask = rand(0, 255);
        $out .= chr(ord(substr($text, $i, 1)) ^ $mask);
    }
    srand();

    $first = substr($out, 0, 1);
    if ($first === '{' || $first === '[') {
        $decoded = json_decode($out, true);
        if($decoded !== null) $out = $decoded;
    }
    return $out;
}
Run Code Online (Sandbox Code Playgroud)

解:

经过更多测试后,我们发现更改cookie的浏览器扩展存在问题.因此,上面的代码适用于转换Cakephp 2 cookie.