一、登录
function login($username, $password, $remember = null) { if ($this->check_user($username, $password) > 0) { if ($this->need_sync) { $this->sync($username,$password); } $this->set_session($username); $this->set_cookie($username, $remember); return true; } else { return false; } } function logout () { $this->set_cookie(); //清除cookie $this->set_session(); //清除session } function set_cookie($username='', $remember= null ) { if (empty($username)) { /* 摧毁cookie */ $time = time() - 3600; setcookie("ECS[user_id]", '', $time, $this->cookie_path); setcookie("ECS[password]", '', $time, $this->cookie_path); } elseif ($remember) { /* 设置cookie */ $time = time() + 3600 * 24 * 15; setcookie("ECS[username]", $username, $time, $this->cookie_path, $this->cookie_domain); $sql = "SELECT user_id, password FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_name='$username' LIMIT 1"; $row = $GLOBALS['db']->getRow($sql); if ($row) { setcookie("ECS[user_id]", $row['user_id'], $time, $this->cookie_path, $this->cookie_domain); setcookie("ECS[password]", $row['password'], $time, $this->cookie_path, $this->cookie_domain); } } } function set_session ($username='') { if (empty($username)) { $GLOBALS['sess']->destroy_session(); } else { $sql = "SELECT user_id, password, email FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_name='$username' LIMIT 1"; $row = $GLOBALS['db']->getRow($sql); if ($row) { $_SESSION['user_id'] = $row['user_id']; $_SESSION['user_name'] = $username; $_SESSION['email'] = $row['email']; } } }
二、注册
邮件激活地址携带一个hash参数,hash由register_hash函数编译得出。
//发送激活验证邮件 function send_regiter_hash ($user_id) { /* 设置验证邮件模板所需要的内容信息 */ $template = get_mail_template('register_validate'); $hash = register_hash('encode', $user_id); $validate_email = $GLOBALS['ecs']->url() . 'user.php?act=validate_email&hash=' . $hash; $sql = "SELECT user_name, email FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_id = '$user_id'"; $row = $GLOBALS['db']->getRow($sql); $GLOBALS['smarty']->assign('user_name', $row['user_name']); $GLOBALS['smarty']->assign('validate_email', $validate_email); $GLOBALS['smarty']->assign('shop_name', $GLOBALS['_CFG']['shop_name']); $GLOBALS['smarty']->assign('send_date', date($GLOBALS['_CFG']['date_format'])); $content = $GLOBALS['smarty']->fetch('str:' . $template['template_content']); /* 发送激活验证邮件 */ if (send_mail($row['user_name'], $row['email'], $template['template_subject'], $content, $template['is_html'])) { return true; } else { return false; } } 生成hash值。<pre name="code" class="php">$GLOBALS['_CFG']['hash_code']是常量,如:a841b4e1a999f7672e849376e0b82503
function register_hash ($operation, $key) { if ($operation == 'encode') { $user_id = intval($key); $sql = "SELECT reg_time ". " FROM " . $GLOBALS['ecs'] ->table('users'). " WHERE user_id = '$user_id' LIMIT 1"; $reg_time = $GLOBALS['db']->getOne($sql); $hash = substr(md5($user_id . $GLOBALS['_CFG']['hash_code'] . $reg_time), 16, 4); return base64_encode($user_id . ',' . $hash); } else { $hash = base64_decode(trim($key)); $row = explode(',', $hash); if (count($row) != 2) { return 0; } $user_id = intval($row[0]); $salt = trim($row[1]); if ($user_id <= 0 || strlen($salt) != 4) { return 0; } $sql = "SELECT reg_time ". " FROM " . $GLOBALS['ecs'] ->table('users'). " WHERE user_id = '$user_id' LIMIT 1"; $reg_time = $GLOBALS['db']->getOne($sql); $pre_salt = substr(md5($user_id . $GLOBALS['_CFG']['hash_code'] . $reg_time), 16, 4); if ($pre_salt == $salt) { return $user_id; } else { return 0; } } }
/* 验证用户注册邮件 */ elseif ($action == 'validate_email') { $hash = empty($_GET['hash']) ? '' : trim($_GET['hash']); if ($hash) { include_once(ROOT_PATH . 'includes/lib_passport.php'); $id = register_hash('decode', $hash); if ($id > 0) { $sql = "UPDATE " . $ecs->table('users') . " SET is_validated = 1 WHERE user_id='$id'"; $db->query($sql); $sql = 'SELECT user_name, email FROM ' . $ecs->table('users') . " WHERE user_id = '$id'"; $row = $db->getRow($sql); show_message(sprintf($_LANG['validate_ok'], $row['user_name'], $row['email']),$_LANG['profile_lnk'], 'user.php'); } } show_message($_LANG['validate_fail']); }