初探 Swoole -- 用 Swoole 啓動一個 hello worldphp
內存的妙用 -- PHP終於能夠 vs JAVA啦html
初級應用 -- 實現用戶登陸 [撰寫中]mongodb
展望 -- Swoole 的侷限性分析及我我的的期待 [撰寫中]數據庫
上節內存的妙用 -- PHP終於能夠 vs JAVA啦中, 咱們瞭解了 Swoole 的MYSQL數據庫 CURD 操做, 這節咱們用 MongoDB來作演示.swoole
環境說明:cookie
MacOS X El Captain 10.11.6網絡
PHP 7.0.8 with MongoDB support框架
MongoDB 1.1.8post
CSS框架 Bootstrap3code
首先, 咱們來作個用戶登陸頁面/tpl/login.html
<!DOCTYPE html> <html> <head> <title>Login</title> </head> <body> <form action="/dologin/" method="POST"> <h1>Login</h1> Username: <input type="text" name="username" id="username"><br/> Password: <input type="password" name="password" id="password"><br/> <button type="submit">Submit</button> </form> </body> </html>
PHP代碼
咱們引入MongoDB
<?php class Mongo{ static $db = null; static $dbname = null; public static function instance($dbname = 'db'){ if (null === self::$db) { $m = new MongoDB\Client('mongodb://localhost:27017'); self::$db = $m->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啓動代碼
<?php $http = new swoole_http_server('0.0.0.0', 1688, SWOOLE_BASE); #使用內存 SESSION~~ $http->_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); }
主邏輯:
<?php $http->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; }else if ( $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; }else if ( $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; }else if ( $req->server['request_uri'] == '/i/' ) { $res->write('You currently logged in as'.$_SESSION['user']['username']); $res->end(); return; } $res->status(404); $res->end(); return; });