UserController.class.php 4.6 KB

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