<?php session_start(); //假設用戶登錄成功獲得了以下用戶數據 $userinfo = array( 'uid' => 10000, 'name' => 'spark', 'email' => '[email protected]', '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>'; //* 將用戶數據保存到cookie中的一個簡單方法 */ $secureKey = 'imooc'; //加密密鑰 $str = serialize($userinfo); //將用戶信息序列化 echo "用戶信息加密前:".$str; $str = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secureKey, $str, MCRYPT_MODE_ECB)); echo "用戶信息加密后:".$str; //將加密后的用戶數據存儲到cookie中 setcookie('userinfo', $str); //當需要使用時進行解密 $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $secureKey, base64_decode($str), MCRYPT_MODE_ECB); $uinfo = unserialize($str); echo "解密后的用戶信息:<br>"; var_dump($uinfo);
不懂mcrypt_decrypt()這個函數干什么的!??!
2014-10-06
可以看看php官方手冊,說的很清楚。
http://cn2.php.net/manual/zh/function.mcrypt-encrypt.php
2014-09-12
mcrypt_encrypt和mcrypt_decrypt是用來進行字符串加密解密的