How to uniquely identify computers visiting web site in JavaScript?

If you want to identify computers visiting your website, you will have to store the cookies of your website on the visiting computers.

Cookies: In simple words, HTTP cookies are small notes which store the user’s preferences about a particular website. For example, when a user shops online without logging in, he may put some items in the cart. Even after closing the browser and reloading the website, the items stored in the cart do not undergo any changes. This data of the user is stored as a cookie in the user’s computer itself. So, the next time the user visits the website, the data can be returned to the website server. Thus, the website is able to uniquely identify the computer.

For the more details about How cookies are used in a website?

Cookies working procedure:

  • Whenever a computer visits your website, the server sends a cookie in a key-value format to the browser. Once the cookie is accepted by the browser, it is stored as a text record in the computer’s hardware.
  • Now, when the computer visits your website, this cookie is retrieved from the computer and sent to the server. In this way, the server identifies the computer visiting the website and remembers the previous data exchange.

Cookies in JavaScript: JavaScript can do all operations like read, create and delete cookies with the document.cookie property. A cookie text record consists of four attributes.



  1. Name-Value OR Key-Value: These are the only attribute pairs that are sent from the browser to the server when a computer visits a website repeatedly. Rest all attributes are used only by the browser to determine other properties.
  2. Domain and Path: The domain name of your website which controls the website’s authority over the internet. The path to the directory or web page that sets the cookie.
  3. Secure and HttpOnly: Secure is used to keep cookie exchange limited to encrypted transmission, directing browsers to use cookies only via secure/encrypted connections. HttpOnly directs browsers not to expose cookies through channels other than HTTP (and HTTPS) requests.
  4. Expires and Max-Age: Expires defines the time for which the cookie is stored in a browser, after which it is deleted. If this is blank, the cookie will expire when the visitor quits the browser.

Example: The following example demonstrates cookies, which stores cookies for 2 days and uses it to greet the user in their language. Upon running this code, the system asks you to enter your language”English/Spanish”. After entering this, you can run the code again and see that the system says “hello” to the user in the selected language.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
    <script>
        function setCookie(cookieName, cookieValue) {
            var d = new Date();
            d.setTime(d.getTime() + 2 * 24 * 60 * 60 * 1000);
            var expires = "expires=" + d.toGMTString();
            document.cookie = cookieName + "=" 
            + cookieValue + ";" + expires + ";path=/";
        }
        function getCookie(cookieName) {
            var name = cookieName + "=";
            var decodedCookie = decodeURIComponent(document.cookie);
            var cookieArray = decodedCookie.split(";");
            for (var i = 0; i < cookieArray.length; i++) {
                var cookie = cookieArray[i];
                while (cookie.charAt(0) == " ") {
                    cookie = cookie.substring(1);
                }
                if (cookie.indexOf(name) == 0) {
                    return cookie.substring(name.length, cookie.length);
                }
            }
            return "";
        }
        function checkCookie() {
            var language = getCookie("language");
            if (language != "") {
                if (language == "english") alert("Hello Friend!");
                else if (language == "spanish") alert("Hola Amigo!");
            }
            language = prompt
            ("Please enter language[english/spanish]:", "");
            if (language != "" && language != null) {
                if (language == "english") 
                document.write
                ("You selected english! Now RUN it again.");
                else if (language == "spanish") 
                document.write
                ("You selected spanish! Now RUN it again.");
                setCookie("language", language);
            }
        }
    </script>
    </head>
    <body onload="checkCookie()"></body>
</html>                 

chevron_right


Output:If “english” is selected, it gives alert message box , then the document message.

  Hello Friend!
  You selected english! Now RUN it again.

A cookie does not guarantee that it will be able to identify the computer next time the user visits the website. This happens for three reasons.

  • User has the complete authority to delete cookies and clear cache.
  • Cookies are built to identify browsers. Next time, if the user visits your website through a different browser, the cookies will be stored separately on both browsers.
  • Cookies will not work if the user has turned off the local cookie support in the browser.

To always identify the computer visiting your website, you will have to write a system application on the visiting computer.

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.