我正在使用此功能:
function encryptData($value) {
$key = "top secret key";
$text = $value;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
return $crypttext;
}
function decryptData($value) {
$key = "top secret key";
$crypttext = $value;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
return trim($decrypttext);
}
的login.php
$time = time() + 60*60*24*30*12; //store cookie for one year
setcookie('cookie_name', encryptCookie('username'), $time, '/');
$cookie_value = decryptCookie($_COOKIE['cookie_name']);
Cookie已设置且正在运行。
问题:
$_COOKIE['cookie_name']
的内容并打印出来?我需要在这行中打印出来:
so.addVariable("uid", "<? if ($_COOKIE['username']) print $_COOKIE['username']; ?>");
答案 0 :(得分:0)
您提供了自己进行加密和解密的代码。
为了将加密的用户名保存到cookie,您可以使用:
$time = time()+60*60*24*30*12; //store cookie for one year
setcookie('username', encryptCookie($username), $time, '/');
要稍后从加密的Cookie中检索用户名,请使用以下命令:
echo decryptCookie($_COOKIE['username']);