初級アプリケーション:ユーザーのログインを実現

4433 ワード

目次
  • 初探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フレームワークBootstrap 3
  • まず、ユーザー登録ページ/tpl/login.htmlを作成します.
    
    
        
            Login
        
        
            

    Login

    Username:
    Password:

    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 "      
    "; $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 "

    "; 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;
        }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;
    });