Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1180212
  • 博文数量: 272
  • 博客积分: 3899
  • 博客等级: 中校
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-15 14:53
文章分类

全部博文(272)

文章存档

2012年(272)

分类: Python/Ruby

2012-06-25 16:21:42

cookie和生成token


点击(此处)折叠或打开

  1. if ($username == $U){
  2. if ($password == $P){
  3. $token = mt_rand(); // 生成token
  4. $fp = fopen("../temp/session_token", "w+");
  5. fwrite($fp, $token);
  6. fclose($fp);
  7. $cookieValue = authCode("$username,$password,$token", $textKey, "ENCODE");
  8. //echo $cookieValue;
  9. // 写入cookie
  10. createCookie("anehtaDoor", $cookieValue, 0, '/', '', 0, 1);
  11. // 跳转url到referer
  12. if (isset($_GET["redirect"])){ // 这里没检查跳转的域,有钓鱼的风险
  13. $redirect = $_GET["redirect"];
  14. header("Location: $redirect");
  15. } else {
  16. header("Location: admin.php");
  17. }
  18. } else { // 密码错
  19. echo $error;
  20. return false;
  21. }
  22. } else { // 用户名错
  23. echo $error;
  24. return false;
  25. }

校验cookietoken


点击(此处)折叠或打开

  1. function checkLoginStatus(){
  2. if (!isset($_COOKIE["anehtaDoor"])){
  3. echo "";
  4. return false;
  5. }
  6. list($user, $pass, $token) = explode(",", authCode($_COOKIE["anehtaDoor"], $textKey, "DECODE"));
  7. if ($user && $pass && $token){
  8. // 保证只有一个有效的session
  9. if (!file_exists("../temp/session_token") || $token != file_get_contents("../temp/session_token")){
  10. echo "";
  11. return false;
  12. }
  13. if ($user == $U && $pass == $P){
  14. return true;
  15. } else {
  16. echo "";
  17. return false;
  18. }
  19. }
  20. }

还是需要在服务器端保存些东西;如果是服务器集群的话,可能要用到文件服务器或者cache服务器或者挂载的存储。

不过如果根据约定大于配置的思路,只要文件名利用的合理,实现应该还是很简便和高效的。

阅读(1116) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~