UserController.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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') || $v_code && $v_code == session('v_code') ) {
  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(L('register_succeeded'),U('Home/User/login'));
  21. }else{
  22. $this->message('register fail');
  23. }
  24. }else{
  25. $this->message(L('username_exists'));
  26. }
  27. }else{
  28. $this->message(L('code_much_the_same'));
  29. }
  30. }else{
  31. $this->message(L('verification_code_are_incorrect'));
  32. }
  33. }
  34. }
  35. //登录
  36. public function login()
  37. {
  38. if (!IS_POST) {
  39. //如果有cookie记录,则自动登录
  40. $cookie_token = cookie('cookie_token');
  41. if ($cookie_token) {
  42. $ret = D("UserToken")->getToken($cookie_token);
  43. if ($ret && $ret['token_expire'] > time() ) {
  44. D("User")->setLastTime($ret['uid']);
  45. $login_user = D("User")->where(array('uid' => $ret['uid']))->field('password', true)->find();
  46. session("login_user" , $login_user);
  47. $this->message(L('auto_login_succeeded'),U('Home/Item/index'));
  48. exit();
  49. }
  50. }
  51. $this->assign('CloseVerify',C('CloseVerify'));
  52. $this->display ();
  53. }else{
  54. $username = I("username");
  55. $password = I("password");
  56. $v_code = I("v_code");
  57. if (C('CloseVerify')) { //如果关闭验证码
  58. $ret = D("User")->checkLogin($username,$password);
  59. if ($ret) {
  60. session("login_user" , $ret );
  61. D("User")->setLastTime($ret['uid']);
  62. $token = D("UserToken")->createToken($ret['uid']);
  63. cookie('cookie_token',$token,60*60*24*90);//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  64. unset($ret['password']);
  65. $this->message(L('login_succeeded'),U('Home/Item/index'));
  66. }else{
  67. $this->message(L('username_or_password_incorrect'));
  68. }
  69. }else{
  70. if ($v_code && $v_code == session('v_code')) {
  71. $ret = D("User")->checkLogin($username,$password);
  72. if ($ret) {
  73. session("login_user" , $ret );
  74. D("User")->setLastTime($ret['uid']);
  75. $token = D("UserToken")->createToken($ret['uid']);
  76. cookie('cookie_token',$token,60*60*24*90);//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  77. unset($ret['password']);
  78. $this->message(L('login_succeeded'),U('Home/Item/index'));
  79. }else{
  80. $this->message(L('username_or_password_incorrect'));
  81. }
  82. }else{
  83. $this->message(L('verification_code_are_incorrect'));
  84. }
  85. }
  86. }
  87. }
  88. //生成验证码
  89. public function verify(){
  90. //生成验证码图片
  91. Header("Content-type: image/PNG");
  92. $im = imagecreate(44,18); // 画一张指定宽高的图片
  93. $back = ImageColorAllocate($im, 245,245,245); // 定义背景颜色
  94. imagefill($im,0,0,$back); //把背景颜色填充到刚刚画出来的图片中
  95. $vcodes = "";
  96. srand((double)microtime()*1000000);
  97. //生成4位数字
  98. for($i=0;$i<4;$i++){
  99. $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); // 生成随机颜色
  100. $authnum=rand(1,9);
  101. $vcodes.=$authnum;
  102. imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
  103. }
  104. $_SESSION['v_code'] = $vcodes;
  105. for($i=0;$i<200;$i++) //加入干扰象素
  106. {
  107. $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  108. imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); // 画像素点函数
  109. }
  110. ImagePNG($im);
  111. ImageDestroy($im);
  112. }
  113. public function setting(){
  114. $user = $this->checkLogin();
  115. if (!IS_POST) {
  116. $this->assign("user",$user);
  117. $this->display ();
  118. }else{
  119. $username = $user['username'];
  120. $password = I("password");
  121. $new_password = I("new_password");
  122. $ret = D("User")->checkLogin($username,$password);
  123. if ($ret) {
  124. $ret = D("User")->updatePwd($user['uid'],$new_password);
  125. if ($ret) {
  126. $this->message(L('modify_succeeded'),U("Home/Item/index"));
  127. }else{
  128. $this->message(L('modify_faild'));
  129. }
  130. }else{
  131. $this->message(L('old_password_incorrect'));
  132. }
  133. }
  134. }
  135. //退出登录
  136. public function exist(){
  137. $login_user = $this->checkLogin();
  138. session("login_user" , NULL);
  139. cookie('cookie_token',NULL);
  140. session(null);
  141. $this->message(L('logout_succeeded'),U('Home/index/index'));
  142. }
  143. }