How Would You Find a Client IP Address?

The internet was developed using the fundamentals of Transmission Control Protocol / Internet Protocol –  TCP/IP. This is the set of networking protocols that allows computers to communicate and exchange data. Each networked device: computer, server, tablet, smartphone, etc. has an established IP address that identifies it to other devices across the internet.

There are a number of reasons a JavaScript developer may need to know the client IP address:

  • Displaying the IP address of a user on web pages
  • Logging history of visitors to a site
  • Recognizing new users, to request registration and direct to specific pages
  • Identifying repeat visitors, and reacting accordingly with ‘welcome back’ greetings
  • Blocking access from certain addresses known to be undesirable or security risks

Although IP addresses may not be totally identifiable due to masking of IPs through use of a VPN or other methods, a web or JavaScript developer should have knowledge of how to obtain the IP address of the visitor.

IP Address Client – Obtaining the Information

JavaScript functions can extract a number of ‘superglobal’ variables presented by PHP. PHP was originally an acronym for Personal Home Page, but is now recognized as Hypertext Preprocessor.

PHP superglobals are always available to any JavaScript function without any scope limitations. This makes the use of those variables extremely useful to the developer. One superglobal is especially valuable in obtaining information related to the client IP address:

$_SERVER[‘REMOTE_ADDR’]

As the variable name would indicate, this variable reveals the IP address from the device where the current page is being accessed or viewed.

There are many other valuable elements of information that can be obtained from the $_SERVER superglobal variable:

  • $_SERVER[‘SERVER_ADDR’] – Retrieve the IP address of the host server
  • $_SERVER[‘SERVER_NAME’] – Determine the actual host server name
  • $_SERVER[‘SERVER_PROTOCOL’] – There may be specific protocols that a JavaScript developer wants to block from access to functions. This variable provides information that will identify the protocol in use.
  • $_SERVER[‘REMOTE_HOST’] – Provides the name of the host in use to view the current page

PHP provides many other variables that can be useful to the JavaScript programmer, readily accessible across functions. Understanding the availability of these variables and their content should be common knowledge for most developers.

Do you have to do anything special or write extra code to utilize them? Absolutely not.

It’s also possible to obtain the client IP address natively within JavaScript, through the use of many available functions/APIs that can be accessed in a JavaScript function. Some are more robust than others in the information returned, but a little homework will determine what works best for any particular function.

Inline Scripts – What are They?

JavaScript inline scripts are segments of code – or functions – that are actually inserted right into the HTML code being executed. This can be a convenient method of solving a particular programming problem – getting the code inserted within the HTML rather than loading and running an external script.

Due to the interaction between HTML and JavaScript code, there could be performance implications from the use of inline scripts. This is just one important consideration for developers.

Pros and Cons of Inline Scripts

It can be tempting to shortcut the development effort using inline scripts to solve coding problems or to provide a ‘quick fix’ for a JavaScript function. But this methodology has distinct trade-offs, which programming candidates should be aware of:

Pros

Performance – as HTML is processed, when a JavaScript external command is executed, it must be loaded before the HTML continues being parsed. With inline scripts, there is no loading required, minimizing network requests for such services.

Note that programmers utilizing inline scripts can also write code to defer loading, so that page construction can continue. Attributes such as ‘defer’ or ‘async’ can be included to control the handling of the inline script.

Cons

Writing inline scripts – especially large ones – increases the size of the HTML, which can increase page load time.

One of the advantages of scripting is the capability to reuse code, increasing developer productivity. Inserting an external script where needed makes the code itself easier to read and maintain. Code needed multiple times or in various functions is written once, and included whenever needed.

Ensure that your candidate understands the use of inline and external scripts, and has opinions on their use – including valid reasons for those opinions.

It’s More Than a Matter of Opinion

Most companies will have their own guidelines and standards that apply to application development, whether the platform is SQL, Python, JavaScript, or other environments.

JavaScript developers are somewhat segregated in their views of whether inline scripts are an acceptable coding technique, although the majority seem to consider this a technique that should be avoided whenever possible.

Best practices should be agreed upon and strictly adhered to by any potential developer candidates.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.