How To Get a User’s IP Address With PHP

In PHP, there are several methods to retrieve a user’s IP address. We will explore two of those ways in this article.

UNLIMITED DOWNLOADS: 500,000+ WordPress & Design Assets

Sign up for Envato Elements and get unlimited downloads starting at only $16.50 per month!

 

The most reliable way to get a user’s IP address in PHP is to use the $_SERVER superglobal variable.

The $_SERVER superglobal variable contains information about the server environment, including the user’s IP address. Here’s an example:

<?php
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip;
?>

The $_SERVER['REMOTE_ADDR'] element returns the IP address of the client (i.e., the user’s device) that is making the request to the server. This method works well for most cases, but there are a few situations where it may not return the correct IP address, such as when the user is behind a proxy server or using a VPN.

To handle these cases, it is recommended to use the following code to get the user’s IP address:

<?php
function get_client_ip() {
    $ip = '';
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$ip = get_client_ip();
echo $ip;
?>

In this code, we first check if the $_SERVER['HTTP_CLIENT_IP'] element is set. If it is, we use its value as the user’s IP address. If not, we then check if the $_SERVER['HTTP_X_FORWARDED_FOR'] element is set. If it is, we use its value as the user’s IP address. If neither of these elements is set, we use the $_SERVER['REMOTE_ADDR'] element as the user’s IP address.

This method provides a more robust solution for retrieving the user’s IP address, as it takes into account the possibility that the user may be behind a proxy server or using a VPN.

Learn more here.

Quick Tip: How To Disable Resizing of a Textarea in HTML or CSS

In this quick tip, we’re going to show you 2 different ways to disable the resizing of a textarea, for those times when you don’t want the user to be able to control it in this manner. It’s a relatively quick process, with just some simple CSS using the resize CSS property.

Your Designer Toolbox Unlimited Downloads: 500,000+ Web Templates, Icon Sets, Themes & Design Assets

 

HTML

The HTML textarea element provides a resizable property by default. To disable it, you can use the CSS property resize: none in the textarea element. Here is an example:

 <textarea style="resize: none;"></textarea> 

CSS

You can also disable the resizable property of a textarea by using CSS that is not inline. You can add a class to the textarea element and then add the CSS property resize: none to the class. Here is an example:

<textarea class="no-resize"></textarea>

<style>
  .no-resize {
    resize: none;
  }
</style>

Quick Tip: How to Disable Text Selection Highlighting in CSS

There are two main CSS properties that control text selection highlighting: user-select and -webkit-user-select. These properties are used to specify whether or not users are able to select text on the web page.

UNLIMITED DOWNLOADS: Email, admin, landing page & website templates

Starting at only $16.50 per month!

To disable text selection highlighting in CSS, you can set the value of the user-select property to none:

body {
   -webkit-user-select: none;  /* for Safari */
   -moz-user-select: none;     /* for Firefox */
   -ms-user-select: none;      /* for Internet Explorer */
   user-select: none;          /* for modern browsers */
}

In this example, the user-select property is applied to the body element, which means that text selection highlighting will be disabled for the entire page. If you want to disable text selection highlighting for a specific element, simply apply the property to that element instead of the body element.

It’s important to note that the -webkit-user-select and -moz-user-select properties are vendor-specific extensions, and are used to ensure compatibility with Safari and Firefox, respectively. The -ms-user-select property is used for compatibility with Internet Explorer. The standard user-select property should work in all modern browsers.

Introduction to the CSS :has() selector

Yes, we know we’re a bit late to the party, what with the :has() selector making the rounds of web design and development headlines. But better late than never, right? So let’s dive into this new feature of CSS and see what it’s all about!

Your Web Designer Toolbox
Unlimited Downloads: 500,000+ Web Templates, Icon Sets, Themes & Design Assets


 

The CSS :has() selector is a type of complex selector that allows you to select an element based on whether it contains a certain other element. This selector is a powerful tool that can be used in a variety of ways to create advanced styles for your website. Here are some examples of the advanced uses of the :has() selector:

Selecting Parent Elements with a Specific Child Element

One of the most common uses of the :has() selector is to select parent elements based on the presence of a specific child element. For example, if you want to select all the li elements that contain an a element, you can use the following code:

li:has(a) {
  background-color: yellow;
}

Selecting Sibling Elements

Another advanced use of the :has() selector is to select sibling elements based on the presence of a specific child element. For example, if you want to select all the td elements that are siblings of a td element that contains an a element, you can use the following code:

td:has(a) + td {
  background-color: yellow;
}

Selecting Grandparent Elements

You can also use the :has() selector to select grandparent elements based on the presence of a specific child element. For example, if you want to select all the ul elements that are grandparent elements of an li element that contains an a element, you can use the following code:

ul:has(li:has(a)) {
  background-color: yellow;
}

Selecting Elements with Multiple Children

The :has() selector can also be used to select elements based on multiple children. For example, if you want to select all the p elements that contain both an em and a strong element, you can use the following code:

p:has(em) :has(strong) {
  background-color: yellow;
}

Selecting Elements with Specific Attributes

Finally, you can use the :has() selector to select elements based on the presence of specific attributes. For example, if you want to select all the input elements that have a required attribute, you can use the following code:

input:has([required]) {
  background-color: yellow;
}

These are just a few examples of the more complex uses of the :has() selector. With its powerful syntax, it can be used to make complex selections that would otherwise be difficult or impossible to achieve with traditional selectors.

It’s important to note that the :has() selector is not supported in all browsers, so it’s always a good idea to check the browser compatibility before using it in a production environment.