UserController.class.php 4.8 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(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("User")->where("cookie_token = '%s' ",array($cookie_token))->find();
  43. if ($ret && $ret['cookie_token_expire'] > time() ) {
  44. $login_user = $ret ;
  45. session("login_user" , $login_user);
  46. $this->message(L('auto_login_succeeded'),U('Home/Item/index'));
  47. exit();
  48. }
  49. }
  50. $this->assign('CloseVerify',C('CloseVerify'));
  51. $this->display ();
  52. }else{
  53. $username = I("username");
  54. $password = I("password");
  55. $v_code = I("v_code");
  56. if (C('CloseVerify')) { //如果关闭验证码
  57. $ret = D("User")->checkLogin($username,$password);
  58. if ($ret) {
  59. session("login_user" , $ret );
  60. $cookie_token = md5(time().rand().'efeffthdh');
  61. $cookie_token_expire = time() + 60*60*24*90 ;
  62. cookie('cookie_token',$cookie_token,60*60*24*90);
  63. D("User")->where(" uid = '$ret[uid]' ")->save(array("last_login_time"=>time(),"cookie_token"=>$cookie_token,"cookie_token_expire"=>$cookie_token_expire));
  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. $cookie_token = md5(time().rand().'efeffthdh');
  75. $cookie_token_expire = time() + 60*60*24*90 ;
  76. cookie('cookie_token',$cookie_token,60*60*24*90);
  77. D("User")->where(" uid = '$ret[uid]' ")->save(array("last_login_time"=>time(),"cookie_token"=>$cookie_token,"cookie_token_expire"=>$cookie_token_expire));
  78. unset($ret['password']);
  79. $this->message(L('login_succeeded'),U('Home/Item/index'));
  80. }else{
  81. $this->message(L('username_or_password_incorrect'));
  82. }
  83. }else{
  84. $this->message(L('verification_code_are_incorrect'));
  85. }
  86. }
  87. }
  88. }
  89. //生成验证码
  90. public function verify(){
  91. //生成验证码图片
  92. Header("Content-type: image/PNG");
  93. $im = imagecreate(44,18); // 画一张指定宽高的图片
  94. $back = ImageColorAllocate($im, 245,245,245); // 定义背景颜色
  95. imagefill($im,0,0,$back); //把背景颜色填充到刚刚画出来的图片中
  96. $vcodes = "";
  97. srand((double)microtime()*1000000);
  98. //生成4位数字
  99. for($i=0;$i<4;$i++){
  100. $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); // 生成随机颜色
  101. $authnum=rand(1,9);
  102. $vcodes.=$authnum;
  103. imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
  104. }
  105. $_SESSION['v_code'] = $vcodes;
  106. for($i=0;$i<200;$i++) //加入干扰象素
  107. {
  108. $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  109. imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); // 画像素点函数
  110. }
  111. ImagePNG($im);
  112. ImageDestroy($im);
  113. }
  114. public function setting(){
  115. $user = $this->checkLogin();
  116. if (!IS_POST) {
  117. $this->assign("user",$user);
  118. $this->display ();
  119. }else{
  120. $username = $user['username'];
  121. $password = I("password");
  122. $new_password = I("new_password");
  123. $ret = D("User")->checkLogin($username,$password);
  124. if ($ret) {
  125. $ret = D("User")->updatePwd($user['uid'],$new_password);
  126. if ($ret) {
  127. $this->message(L('modify_succeeded'),U("Home/Item/index"));
  128. }else{
  129. $this->message(L('modify_faild'));
  130. }
  131. }else{
  132. $this->message(L('old_password_incorrect'));
  133. }
  134. }
  135. }
  136. //退出登录
  137. public function exist(){
  138. $login_user = $this->checkLogin();
  139. session("login_user" , NULL);
  140. cookie('cookie_token',NULL);
  141. $this->message(L('logout_succeeded'),U('Home/index/index'));
  142. }
  143. }