1
我必須通過發佈請求(使用Flash上傳器)提交用戶會話ID。 項目基於Zend Framework。將PHP會話存儲在JS變量中?
現在的IAM不知道如果我的實現是 「安全」:
查看
var session = "<?= Anon_SimpleCrypt::encrypt(Zend_Session::getId(), 'SOME_KEY'?>";
控制器:
$sessionId = $_POST['SESSION_ID'];
$sessionId = Anon_SimpleCrypt::decrypt($sessionId, 'SOME_KEY');
Zend_Session::setId($sessionId);
加密/解密:
public static function encrypt($value, $encryptionKey)
{
$result = '';
$encryptionKey = $encryptionKey . self::getSalt($value);
for ($count = 0; $count < strlen($value); $count++) {
$char = substr($value, $count, 1);
$keychar = substr($encryptionKey, ($count % strlen($encryptionKey)) - 1, 1);
$char = chr(ord($char) + ord($keychar));
$result.=$char;
}
return base64_encode($result);
}
public static function decrypt($value, $encryptionKey)
{
$result = '';
$value = base64_decode($value);
$encryptionKey = $encryptionKey . self::getSalt($value);
for ($i = 0; $i < strlen($value); $i++) {
$char = substr($value, $i, 1);
$keychar = substr($encryptionKey, ($i % strlen($encryptionKey)) - 1, 1);
$char = chr(ord($char) - ord($keychar));
$result.=$char;
}
return $result;
}
在Cookie中存儲sessionId與通過javascript變量提供sessionId不同。該cookie應該標記爲HTTPOnly,完全是爲了防止它可用於JavaScript。 – Jacco 2012-07-09 12:47:21