如何在 JavaScript 中唯一標識訪問網站的計算機?


無論何時我們建立任何應用程式或網站,都需要唯一地識別訪問該網站的計算機。唯一標識計算機有很多好處。

例如,您為使用者提供一些服務。透過唯一識別計算機,您可以為使用者首次從新裝置訪問您的網站時提供試用服務。當用戶再次訪問時,您可以要求使用者購買高階服務或訂閱您的應用程式。

在這裡,我們將使用 cookie 來識別訪問網站的計算機。

什麼是 cookie?

cookie 允許開發人員在瀏覽器中儲存使用者資訊。例如,我們可以從伺服器向瀏覽器傳送資料並將其儲存在瀏覽器中。因此,無論何時使用者重新訪問網站,它都會從 cookie 中而不是從伺服器中獲取資料。因此,cookie 提高了應用程式的效能。

在我們的案例中,當用戶首次訪問網站時,我們可以將 cookie 的過期時間設定為 100 年。之後,無論何時使用者再次訪問網站,我們都會檢查 cookie 是否存在,然後我們可以說使用者已重新訪問網站。

語法

使用者可以遵循以下語法在 Web 瀏覽器上設定和獲取 cookie。

// to set cookies
document.cookie = "isVisited=true";

// to get cookies
let ca = decodeURIComponent(document.cookie).split(';'); 

在上述語法中,我們將帶有鍵值對的字串分配給 document.cookie 以將 cookie 設定到瀏覽器中。要獲取 cookie,我們可以簡單地使用 document.cookie,它返回 cookie 陣列。

步驟

步驟 1 − 建立 fetchCookies() 函式。

步驟 2 − 在 fetchCookies() 函式內部,使用 document.cookie 獲取陣列格式的 cookie,並使用 decodeURIComponent() 方法解碼 cookie。

步驟 3 − 使用 for 迴圈遍歷陣列。

步驟 4 − 對於陣列的每個元素,刪除陣列開頭處的空格。

步驟 5 − 使用 indexOf() 方法檢查陣列元素是否包含索引 0 處的鍵,並使用 substring() 方法獲取鍵值。

步驟 6 − 返回特定鍵的值。

步驟 7 − 建立 fetchCookies() 函式。在 fetchCookies() 函式中,呼叫 getCookie() 函式並檢查 cookie 是否存在。

步驟 8 − 如果 cookie 為空,則設定 cookie。

步驟 9 − 根據所需的 cookie 是否為空列印訊息。

示例

在下面的示例中,無論何時使用者首次訪問網站,我們都會在 cookie 中將“isValidate”設定為“true”值。無論何時使用者第二次訪問網站,我們都會獲取 cookie 中的“isValidate”,因此我們會列印類似“歡迎回到網站”的訊息。

<html>
<body>
   <h3>Using the <i> Cookies </i> to uniquely identify computers visiting web site in JavaScript</h3>
   <div id = "content"> </div>
   <script>
      let content = document.getElementById('content');
      
      // function to get cookies
      function fetchCookies(cname) {
         let key = cname + "=";
         let ca = decodeURIComponent(document.cookie).split(';');
         for (let i = 0; i < ca.length; i++) {
            let part = ca[i];
            while (part.charAt(0) == ' ') {
               part = part.substring(1);
            }
            if (part.indexOf(key) == 0) {
               return part.substring(key.length, part.length);
            }
         }
         return null;
      }
      
      // set cookies to uniquely identify the computer visiting the website
      function checkCookies() {
         var cookies = fetchCookies("isVisited");
         if (cookies == null) {
            content.innerHTML = "Welcome to the website";
            document.cookie = "isVisited=true";
         } else {
            content.innerHTML = "Welcome back to the website";
         }
      }
      checkCookies();
   </script>
</body>
</html>

示例

在下面的示例中,無論何時使用者首次訪問網站,我們都會使用提示框詢問他們的姓名並顯示歡迎訊息。此外,我們還將 cookie 設定為 100 年的過期時間。

無論何時使用者第二次訪問,我們都會顯示帶有他們姓名的歡迎訊息,而無需向他們詢問他們的姓名。

<html>
<body>
   <h3>Using the <i> Cookies </i> to uniquely identify computers visiting web site in JavaScript</h3>
   <div id = "content"> </div>
   <script>
      let content = document.getElementById('content');
      
      // function to get cookies
      function fetchCookies(cname) {
         let key = cname + "=";
         let ca = decodeURIComponent(document.cookie).split(';');
         for (let i = 0; i < ca.length; i++) {
            let part = ca[i];
            while (part.charAt(0) == ' ') {
               part = part.substring(1);
            }
            if (part.indexOf(key) == 0) {
               return part.substring(key.length, part.length); 
            }
         }
         return null;
      }
      
      // set cookies to uniquely identify the computer visiting the website
      function checkCookies() {
         var cookies = fetchCookies("customCookie");
         if (cookies == null) {
            let name = prompt("Enter your name", "Shubham");
            document.cookie = "customCookie=" + name + "; expires=Thu, 23 Oct 2120 12:00:00 UTC; path=/";
            content.innerHTML = "How are you " + name + "?";
         }
         else {
            content.innerHTML = "Hey, " + cookies + " You visited our site again!";
         }
      }
      checkCookies();
   </script>
</body>
</html> 

使用者學習瞭如何使用 JavaScript 中的 cookie 來唯一識別訪問網站的計算機。但是,cookie 有一些侷限性。如果使用者清除 cookie,我們將無法唯一識別計算機。此外,我們需要將 cookie 的過期時間設定為 100 年。此外,如果使用者使用不同的瀏覽器,我們將無法唯一識別計算機。

克服上述所有問題的最佳解決方案是使用 Google Analytics。

更新於: 2023-03-09

595 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告