UserController.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class UserController extends BaseController {
  5. //注册
  6. public function register(){
  7. if (!IS_POST) {
  8. $this->display ();
  9. }else{
  10. $username = I("username");
  11. $password = I("password");
  12. $confirm_password = I("confirm_password");
  13. $v_code = I("v_code");
  14. if ($v_code && $v_code == session('v_code')) {
  15. if ( $password != '' && $password == $confirm_password) {
  16. if ( ! D("User")->isExist($username) ) {
  17. $ret = D("User")->register($username,$password);
  18. if ($ret) {
  19. $this->message("注册成功!",U('Home/User/login'));
  20. }else{
  21. $this->message("用户名或密码不正确");
  22. }
  23. }else{
  24. $this->message("用户名已经存在啦!");
  25. }
  26. }else{
  27. $this->message("两次输入的密码不一致!");
  28. }
  29. }else{
  30. $this->message("验证码不正确");
  31. }
  32. }
  33. }
  34. //登录
  35. public function login()
  36. {
  37. if (!IS_POST) {
  38. //如果有cookie记录,则自动登录
  39. $cookie_token = cookie('cookie_token');
  40. if ($cookie_token) {
  41. $ret = D("User")->where("cookie_token = '%s' ",array($cookie_token))->find();
  42. if ($ret && $ret['cookie_token_expire'] > time() ) {
  43. $login_user = $ret ;
  44. session("login_user" , $login_user);
  45. $this->message("自动登录成功!正在跳转...",U('Home/Item/index'));
  46. exit();
  47. }
  48. }
  49. $this->display ();
  50. }else{
  51. $username = I("username");
  52. $password = I("password");
  53. $v_code = I("v_code");
  54. if ($v_code && $v_code == session('v_code')) {
  55. $ret = D("User")->checkLogin($username,$password);
  56. if ($ret) {
  57. session("login_user" , $ret );
  58. $cookie_token = md5(time().rand().'efeffthdh');
  59. $cookie_token_expire = time() + 60*60*24*90 ;
  60. cookie('cookie_token',$cookie_token,60*60*24*90);
  61. D("User")->where(" uid = '$ret[uid]' ")->save(array("last_login_time"=>time(),"cookie_token"=>$cookie_token,"cookie_token_expire"=>$cookie_token_expire));
  62. unset($ret['password']);
  63. $this->message("登录成功!",U('Home/Item/index'));
  64. }else{
  65. $this->message("用户名或密码不正确");
  66. }
  67. }else{
  68. $this->message("验证码不正确");
  69. }
  70. }
  71. }
  72. //生成验证码
  73. public function verify(){
  74. //生成验证码图片
  75. Header("Content-type: image/PNG");
  76. $im = imagecreate(44,18); // 画一张指定宽高的图片
  77. $back = ImageColorAllocate($im, 245,245,245); // 定义背景颜色
  78. imagefill($im,0,0,$back); //把背景颜色填充到刚刚画出来的图片中
  79. $vcodes = "";
  80. srand((double)microtime()*1000000);
  81. //生成4位数字
  82. for($i=0;$i<4;$i++){
  83. $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); // 生成随机颜色
  84. $authnum=rand(1,9);
  85. $vcodes.=$authnum;
  86. imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
  87. }
  88. $_SESSION['v_code'] = $vcodes;
  89. for($i=0;$i<200;$i++) //加入干扰象素
  90. {
  91. $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  92. imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); // 画像素点函数
  93. }
  94. ImagePNG($im);
  95. ImageDestroy($im);
  96. }
  97. public function setting(){
  98. $user = $this->checkLogin();
  99. if (!IS_POST) {
  100. $this->assign("user",$user);
  101. $this->display ();
  102. }else{
  103. $username = $user['username'];
  104. $password = I("password");
  105. $new_password = I("new_password");
  106. $ret = D("User")->checkLogin($username,$password);
  107. if ($ret) {
  108. $ret = D("User")->updatePwd($user['uid'],$new_password);
  109. if ($ret) {
  110. $this->message("修改成功!",U("Home/Item/index"));
  111. }else{
  112. $this->message("修改失败!");
  113. }
  114. }else{
  115. $this->message("原密码不正确");
  116. }
  117. }
  118. }
  119. //退出登录
  120. public function exist(){
  121. $login_user = $this->checkLogin();
  122. session("login_user" , NULL);
  123. cookie('cookie_token',NULL);
  124. $this->message("退出成功!",U('Home/index/index'));
  125. }
  126. }