初级应用 -- 实现用户登录
目录
初探 Swoole -- 用 Swoole 启动一个 hello world
内存的妙用 -- PHP终于可以 vs JAVA啦
初级应用 -- 实现用户登录 [撰写中]
展望 -- Swoole 的局限性分析及我个人的期待 [撰写中]
回顾
上节内存的妙用 -- PHP终于可以 vs JAVA啦中, 我们了解了 Swoole 的MYSQL数据库 CURD 操作, 这节我们用 MongoDB来做演示.
开搞
环境说明:
MacOS X El Captain 10.11.6
PHP 7.0.8 with MongoDB support
MongoDB 1.1.8
CSS框架 Bootstrap3
首先, 我们来做个用户登录页面
/tpl/login.html
Login # Login Username: [br] Password: [br] Submit
PHP代码
我们引入MongoDB
selectDatabase($dbname); self::$dbname = $dbname; } return self::$db; } public static function id($id){ return new MongoDB\BSON\ObjectID($id); } public function __call($name, $arguments){ return self::$db->selectCollection(self::$dbname, $name); }}
Swoole启动代码
_GLOBAL_SESSION = [];
$http->mongo = Mongo::instance('db');
$http->db = new \stdClass();
使用预加载, 提前将用户数据加载到内存. 登录都无需网络/磁盘IO
if('user'){
echo "加载用户数据\n";
$http->db->user = [];
$users = $http->mongo->user->find([]);
foreach ($users as $i => $user) {
$user['_id'] = $user['_id']->__toString();
$http->db->user[$user['username']] = $user;
}
echo "用户数据加载完成\n\n";
unset($user);unset($users);
}
主逻辑:
on('request', function(swoole_http_request $req, swoole_http_response $res) use($http) {
if (!isset($req->cookie) || !isset($req->cookie['sid']) || !$req->cookie['sid']) { $req->cookie['sid'] = md5(password_hash(time().mt_rand(100000, 999999), 1)); $res->cookie('sid', $req->cookie['sid'], time() + 60 * 60 * 24 * 365 * 10, '/', '', false, true);}$_SESS_ID = $req->cookie['sid'];if (!is_array($http->_GLOBAL_SESSION[ $_SESS_ID ])) $http->_GLOBAL_SESSION[ $_SESS_ID ] = [];$_SESSION = &$http->_GLOBAL_SESSION[ $_SESS_ID ];if ( $req->server['request_uri'] == '/' ) { $res->status(302); $res->header('Location', '/login/'); $res->end(); return;}elseif ( $req->server['request_uri'] == '/login/' ) { if ($_SESSION['user']) { $res->status(302); $res->header('Location', '/i/'); $res->end(); return; } $html = file_get_contents(dirname(__FILE__).'/tpl/'.'login.html'); $res->write($html); $res->end(); unset($html); return;}elseif ( $req->server['request_uri'] == '/dologin/' ) { $user = $http->db->user[$req->post['username']]; if (!$user || !password_verify($req->post['password'], $user['password'])) { $res->write('bad_account_or_password'); $res->end(); return; } $_SESSION['user'] = $user; unset($user); $res->status(302); $res->header('Location', '/vul/'); $res->end(); return;}elseif ( $req->server['request_uri'] == '/i/' ) { $res->write('You currently logged in as'.$_SESSION['user']['username']); $res->end(); return;}$res->status(404);$res->end();return;
});
关键字:php, swoole
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!