UserController.class.php 4.8 KB

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