{"id":215,"date":"2025-02-25T09:54:17","date_gmt":"2025-02-25T09:54:17","guid":{"rendered":"https:\/\/news.techjunior.vn\/?p=215"},"modified":"2025-02-25T09:54:17","modified_gmt":"2025-02-25T09:54:17","slug":"huong-dan-xay-dung-he-thong-dang-ky-dang-nhap-tren-cmd-bang-python","status":"publish","type":"post","link":"https:\/\/news.techjunior.vn\/?p=215","title":{"rendered":"H\u01b0\u1edbng D\u1eabn X\u00e2y D\u1ef1ng H\u1ec7 Th\u1ed1ng \u0110\u0103ng K\u00fd &amp; \u0110\u0103ng Nh\u1eadp Tr\u00ean CMD B\u1eb1ng Python"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"235\" height=\"158\" src=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-60.png\" alt=\"\" class=\"wp-image-216\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">1. Gi\u1edbi Thi\u1ec7u<\/h2>\n\n\n\n<p>Trong l\u1eadp tr\u00ecnh \u1ee9ng d\u1ee5ng, ch\u1ee9c n\u0103ng <strong>\u0111\u0103ng k\u00fd v\u00e0 \u0111\u0103ng nh\u1eadp<\/strong> l\u00e0 m\u1ed9t trong nh\u1eefng t\u00ednh n\u0103ng quan tr\u1ecdng \u0111\u1ec3 qu\u1ea3n l\u00fd ng\u01b0\u1eddi d\u00f9ng. Trong b\u00e0i vi\u1ebft n\u00e0y, ch\u00fang ta s\u1ebd x\u00e2y d\u1ef1ng m\u1ed9t h\u1ec7 th\u1ed1ng \u0111\u0103ng k\u00fd v\u00e0 \u0111\u0103ng nh\u1eadp \u0111\u01a1n gi\u1ea3n s\u1eed d\u1ee5ng <strong>Python tr\u00ean CMD<\/strong>, l\u01b0u tr\u1eef d\u1eef li\u1ec7u v\u00e0o file JSON \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh l\u00e2u d\u00e0i.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">2. C\u1ea5u Tr\u00fac Ch\u01b0\u01a1ng Tr\u00ecnh<\/h2>\n\n\n\n<p>Ch\u01b0\u01a1ng tr\u00ecnh c\u1ee7a ch\u00fang ta s\u1ebd g\u1ed3m c\u00e1c ch\u1ee9c n\u0103ng ch\u00ednh:<\/p>\n\n\n\n<p>\u2705 <strong>\u0110\u0103ng k\u00fd ng\u01b0\u1eddi d\u00f9ng m\u1edbi<\/strong>: Ki\u1ec3m tra username \u0111\u00e3 t\u1ed3n t\u1ea1i ch\u01b0a, n\u1ebfu ch\u01b0a th\u00ec l\u01b0u th\u00f4ng tin v\u00e0o file JSON. \u2705 <strong>\u0110\u0103ng nh\u1eadp<\/strong>: X\u00e1c minh th\u00f4ng tin username v\u00e0 m\u1eadt kh\u1ea9u t\u1eeb d\u1eef li\u1ec7u \u0111\u00e3 l\u01b0u. \u2705 <strong>L\u01b0u tr\u1eef th\u00f4ng tin v\u00e0o JSON<\/strong>: Gi\u00fap l\u01b0u gi\u1eef d\u1eef li\u1ec7u ngay c\u1ea3 khi ch\u01b0\u01a1ng tr\u00ecnh k\u1ebft th\u00fac. \u2705 <strong>Hi\u1ec3n th\u1ecb menu tr\u00ean CMD<\/strong>: Cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng thao t\u00e1c qua giao di\u1ec7n d\u00f2ng l\u1ec7nh.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">3. H\u01b0\u1edbng D\u1eabn T\u1eebng B\u01b0\u1edbc<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1. L\u01b0u Tr\u1eef D\u1eef Li\u1ec7u Ng\u01b0\u1eddi D\u00f9ng Trong File JSON<\/h3>\n\n\n\n<p>\u0110\u1ec3 \u0111\u1ea3m b\u1ea3o th\u00f4ng tin ng\u01b0\u1eddi d\u00f9ng \u0111\u01b0\u1ee3c l\u01b0u l\u1ea1i sau khi ch\u01b0\u01a1ng tr\u00ecnh k\u1ebft th\u00fac, ch\u00fang ta s\u1ebd s\u1eed d\u1ee5ng <strong>file JSON<\/strong> (<code>user_data.txt<\/code>) l\u00e0m n\u01a1i l\u01b0u tr\u1eef.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\ndef save_user_data(username, password):\n    try:\n        with open('user_data.txt', 'r') as file:\n            user_data = json.load(file)\n    except FileNotFoundError:\n        user_data = {}\n\n    user_data&#091;username] = password\n\n    with open('user_data.txt', 'w') as file:\n        json.dump(user_data, file, indent=4)\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>Gi\u1ea3i th\u00edch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ki\u1ec3m tra xem file <code>user_data.txt<\/code> c\u00f3 t\u1ed3n t\u1ea1i kh\u00f4ng.<\/li>\n\n\n\n<li>N\u1ebfu c\u00f3, \u0111\u1ecdc d\u1eef li\u1ec7u t\u1eeb file JSON.<\/li>\n\n\n\n<li>N\u1ebfu kh\u00f4ng, t\u1ea1o m\u1ed9t dictionary m\u1edbi.<\/li>\n\n\n\n<li>Th\u00eam username v\u00e0 password v\u00e0o dictionary v\u00e0 l\u01b0u l\u1ea1i v\u00e0o file JSON.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">3.2. \u0110\u1ecdc D\u1eef Li\u1ec7u T\u1eeb JSON<\/h3>\n\n\n\n<p>\u0110\u1ec3 ki\u1ec3m tra ng\u01b0\u1eddi d\u00f9ng khi \u0111\u0103ng nh\u1eadp, ta c\u1ea7n m\u1ed9t h\u00e0m \u0111\u1ecdc d\u1eef li\u1ec7u t\u1eeb file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def load_user_data():\n    try:\n        with open('user_data.txt', 'r') as file:\n            return json.load(file)\n    except FileNotFoundError:\n        return {}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>Gi\u1ea3i th\u00edch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>N\u1ebfu file t\u1ed3n t\u1ea1i, \u0111\u1ecdc n\u1ed9i dung v\u00e0 tr\u1ea3 v\u1ec1 d\u01b0\u1edbi d\u1ea1ng dictionary.<\/li>\n\n\n\n<li>N\u1ebfu kh\u00f4ng t\u1ed3n t\u1ea1i, tr\u1ea3 v\u1ec1 m\u1ed9t dictionary r\u1ed7ng.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">3.3. Ki\u1ec3m Tra Ng\u01b0\u1eddi D\u00f9ng \u0110\u00e3 T\u1ed3n T\u1ea1i<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def check_user_exists(username):\n    return username in load_user_data()\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>Gi\u1ea3i th\u00edch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>T\u1ea3i d\u1eef li\u1ec7u t\u1eeb file JSON v\u00e0 ki\u1ec3m tra xem username c\u00f3 t\u1ed3n t\u1ea1i kh\u00f4ng.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">3.4. Ch\u1ee9c N\u0103ng \u0110\u0103ng K\u00fd Ng\u01b0\u1eddi D\u00f9ng M\u1edbi<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def register():\n    username = input(\"Nh\u1eadp t\u00ean \u0111\u0103ng nh\u1eadp: \")\n    password = input(\"Nh\u1eadp m\u1eadt kh\u1ea9u: \")\n\n    if check_user_exists(username):\n        print(\"T\u00ean \u0111\u0103ng nh\u1eadp \u0111\u00e3 t\u1ed3n t\u1ea1i!\")\n    else:\n        save_user_data(username, password)\n        print(\"\u0110\u0103ng k\u00fd th\u00e0nh c\u00f4ng!\")\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>Gi\u1ea3i th\u00edch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Nh\u1eadp username v\u00e0 password.<\/li>\n\n\n\n<li>Ki\u1ec3m tra xem username \u0111\u00e3 t\u1ed3n t\u1ea1i ch\u01b0a.<\/li>\n\n\n\n<li>N\u1ebfu ch\u01b0a, l\u01b0u username v\u00e0 password v\u00e0o file JSON.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">3.5. Ch\u1ee9c N\u0103ng \u0110\u0103ng Nh\u1eadp<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def login():\n    username = input(\"Nh\u1eadp t\u00ean \u0111\u0103ng nh\u1eadp: \")\n    password = input(\"Nh\u1eadp m\u1eadt kh\u1ea9u: \")\n\n    user_data = load_user_data()\n    if user_data.get(username) == password:\n        print(\"\u0110\u0103ng nh\u1eadp th\u00e0nh c\u00f4ng!\")\n    else:\n        print(\"Sai t\u00ean \u0111\u0103ng nh\u1eadp ho\u1eb7c m\u1eadt kh\u1ea9u.\")\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>Gi\u1ea3i th\u00edch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Nh\u1eadp username v\u00e0 password t\u1eeb ng\u01b0\u1eddi d\u00f9ng.<\/li>\n\n\n\n<li>Ki\u1ec3m tra trong file JSON.<\/li>\n\n\n\n<li>N\u1ebfu \u0111\u00fang, hi\u1ec3n th\u1ecb th\u00f4ng b\u00e1o \u0111\u0103ng nh\u1eadp th\u00e0nh c\u00f4ng, n\u1ebfu sai, b\u00e1o l\u1ed7i.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">3.6. T\u1ea1o Menu Ch\u1ecdn Ch\u1ee9c N\u0103ng<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def main():\n    while True:\n        print(\"\\n1. \u0110\u0103ng k\u00fd\")\n        print(\"2. \u0110\u0103ng nh\u1eadp\")\n        print(\"3. Tho\u00e1t\")\n        choice = input(\"L\u1ef1a ch\u1ecdn: \")\n\n        if choice == \"1\":\n            register()\n        elif choice == \"2\":\n            login()\n        elif choice == \"3\":\n            print(\"T\u1ea1m bi\u1ec7t!\")\n            break\n        else:\n            print(\"L\u1ef1a ch\u1ecdn kh\u00f4ng h\u1ee3p l\u1ec7!\")\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>Gi\u1ea3i th\u00edch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Hi\u1ec3n th\u1ecb menu \u0111\u1ec3 ng\u01b0\u1eddi d\u00f9ng ch\u1ecdn ch\u1ee9c n\u0103ng.<\/li>\n\n\n\n<li>D\u00f9ng v\u00f2ng l\u1eb7p <code>while True<\/code> \u0111\u1ec3 li\u00ean t\u1ee5c nh\u1eadn l\u1ef1a ch\u1ecdn t\u1eeb ng\u01b0\u1eddi d\u00f9ng.<\/li>\n\n\n\n<li>N\u1ebfu ch\u1ecdn &#8220;3&#8221;, ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd tho\u00e1t.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">4. T\u1ed5ng K\u1ebft<\/h2>\n\n\n\n<p>\u2714 <strong>Truy c\u1eadp &amp; l\u01b0u d\u1eef li\u1ec7u l\u00e2u d\u00e0i:<\/strong> S\u1eed d\u1ee5ng JSON gi\u00fap l\u01b0u tr\u1eef t\u00e0i kho\u1ea3n ngay c\u1ea3 khi ch\u01b0\u01a1ng tr\u00ecnh k\u1ebft th\u00fac. \u2714 <strong>Giao di\u1ec7n d\u00f2ng l\u1ec7nh \u0111\u01a1n gi\u1ea3n:<\/strong> Gi\u00fap d\u1ec5 d\u00e0ng thao t\u00e1c tr\u00ean CMD. \u2714 <strong>Chia nh\u1ecf c\u00e1c ch\u1ee9c n\u0103ng:<\/strong> D\u1ec5 m\u1edf r\u1ed9ng v\u00e0 b\u1ea3o tr\u00ec.<\/p>\n\n\n\n<p>\ud83d\udccc V\u1edbi c\u00e1ch ti\u1ebfp c\u1eadn n\u00e0y, b\u1ea1n c\u00f3 th\u1ec3 n\u00e2ng c\u1ea5p h\u1ec7 th\u1ed1ng b\u1eb1ng c\u00e1ch th\u00eam <strong>b\u1ea3o m\u1eadt n\u00e2ng cao<\/strong> nh\u01b0 <strong>b\u0103m m\u1eadt kh\u1ea9u (hashing)<\/strong> \u0111\u1ec3 t\u0103ng t\u00ednh an to\u00e0n.<\/p>\n\n\n\n<p>\ud83d\ude80 <strong>H\u00e3y th\u1eed ngay v\u00e0 n\u00e2ng cao k\u1ef9 n\u0103ng l\u1eadp tr\u00ecnh Python c\u1ee7a b\u1ea1n!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Gi\u1edbi Thi\u1ec7u Trong l\u1eadp tr\u00ecnh \u1ee9ng d\u1ee5ng, ch\u1ee9c n\u0103ng \u0111\u0103ng k\u00fd v\u00e0 \u0111\u0103ng nh\u1eadp l\u00e0 m\u1ed9t trong nh\u1eefng t\u00ednh n\u0103ng quan tr\u1ecdng \u0111\u1ec3 qu\u1ea3n l\u00fd ng\u01b0\u1eddi d\u00f9ng. Trong b\u00e0i vi\u1ebft n\u00e0y,&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=215"}],"version-history":[{"count":1,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions\/217"}],"wp:attachment":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}