Verifyuser.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace WY\app\model;
  3. use WY\app\libs\Model;
  4. use WY\app\libs\Session;
  5. use WY\app\libs\Req;
  6. use WY\app\libs\Res;
  7. if (!defined('WY_ROOT')) {
  8. exit;
  9. }
  10. class Verifyuser
  11. {
  12. function __construct()
  13. {
  14. $this->model = new Model();
  15. $this->session = new Session();
  16. $this->req = new Req();
  17. $this->res = new Res();
  18. }
  19. public function verify()
  20. {
  21. if ($this->session->get('login_userid')) {
  22. return '/member';
  23. }
  24. if ($this->session->get('login_agentid')) {
  25. return '/agent';
  26. }
  27. if (isset($_COOKIE['uuid']) && isset($_COOKIE['usta'])) {
  28. $uuid = $_COOKIE['uuid'];
  29. $usta = $_COOKIE['usta'];
  30. if ($uuid == '' || $usta == '' || !preg_match('/[a-z0-9]{20}/', $uuid)) {
  31. return false;
  32. }
  33. $user = $this->model->select()->from('users')->where(array('fields' => 'left(salt,20)=?', 'values' => array($uuid)))->fetchRow();
  34. if ($user) {
  35. $hash = sha1($user['username'] . $user['userpass'] . $user['salt']);
  36. if ($hash == $usta) {
  37. if ($user['is_agent']) {
  38. $this->session->set('login_agentid', $user['id']);
  39. $this->session->set('login_agentname', $user['username']);
  40. $url = '/agent';
  41. } else {
  42. $this->session->set('login_userid', $user['id']);
  43. $this->session->set('login_username', $user['username']);
  44. $url = '/member';
  45. }
  46. $ip = $this->req->server('REMOTE_ADDR');
  47. $logData = array('userid' => $user['id'], 'addtime' => time(), 'ip' => $ip ,'address' => $this->res->getIPLoc($ip));
  48. $this->model->from('userlogs')->insertData($logData)->insert();
  49. return $url;
  50. }
  51. }
  52. }
  53. return false;
  54. }
  55. public function setck($userid, $username, $userpass)
  56. {
  57. $salt = sha1($this->res->getRandomString(32));
  58. if ($this->model->from('users')->updateSet(array('salt' => $salt))->where(array('fields' => 'id=?', 'values' => array($userid)))->update()) {
  59. $expire = time() + 60 * 60 * 24 * 7;
  60. setcookie('uuid', $this->res->substring($salt, 0, 20), $expire, '/', '');
  61. setcookie('usta', sha1($username . $userpass . $salt), $expire, '/', '');
  62. }
  63. }
  64. public function unsetck()
  65. {
  66. setcookie('uuid', '', time() - 60 * 60, '/', '');
  67. setcookie('usta', '', time() - 60 * 60, '/', '');
  68. }
  69. }