<?php
session_start();
//假設(shè)用戶登錄成功獲得了以下用戶數(shù)據(jù)
$userinfo = array(
'uid' => 10000,
'name' => 'spark',
'email' => 'spark@imooc.com',
'sex' => 'man',
'age' => '18'
);
header("content-type:text/html; charset=utf-8");
/* 將用戶信息保存到session中 */
$_SESSION['uid'] = $userinfo['uid'];
$_SESSION['name'] = $userinfo['name'];
$_SESSION['userinfo'] = $userinfo;
echo "welcome ".$_SESSION['name'] . '<br>';
//* 將用戶數(shù)據(jù)保存到cookie中的一個(gè)簡單方法 */
$secureKey = 'imooc'; //加密密鑰
$str = serialize($userinfo); //將用戶信息序列化
echo "用戶信息加密前:".$str;
$str = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secureKey, $str, MCRYPT_MODE_ECB));
echo "用戶信息加密后:".$str;
//將加密后的用戶數(shù)據(jù)存儲到cookie中
setcookie('userinfo', $str);
//當(dāng)需要使用時(shí)進(jìn)行解密
$str = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $secureKey, base64_decode($str), MCRYPT_MODE_ECB);
$uinfo = unserialize($str);
echo "解密后的用戶信息:<br>";
var_dump($uinfo);