Join Kevin Skoglund for an in-depth discussion in this video Find an IP address with PHP, part of Easy PHP Projects: Single-Serving Sites.
- Let's start with a simple but useful example. A website that will tell you your own IP address. Most of the time you can probably look up your IP address in another way, such as by checking your computer system preferences. But that might not be easy on all devices. Or you might be behind a firewall or using a proxy server that alters your IP address so that it appears differently to the outside world than what you see in your preferences. And sometimes it's just faster to visit a URL than it is to dig through your computer preferences, especially if you're already working inside a web browser.
If you post this page on a remote server, then you'll know what IP address that remote server sees you as using. PHP makes it easy. The first thing you need to know is that in PHP all of the metainformation about the incoming request is stored in the superglobal server. That's $_ and then in all capitals SERVER And what gets put in there is an array of values. We can look at all of those values by using PHP's print_r function.
Inside there, the IP address can usually be found as the value for the key REMOTE_ADDR in all capitals. So that's how you would locate it. So, if we want to see that value, we could use PHP and echo back the value from the server superglobal the key REMOTE_ADDR Let's try it. So, the first thing you need to do is figure out where we're going to put this PHP page. If you've already been doing PHP development, then hopefully you already have a working directory.
For me, I've set that up on my Mac inside my user directory to be Sites. And in there is where I'm going to put all of my PHP pages. If you're using MAMP or WAMP or something else that has a different working directory, go ahead and use that. It doesn't really matter, we just need to be able to bring this page up in our browser once we create it. In this directory, I'm going to put a new PHP file, which I'll do using TextMate. And that file that I'm going to create, I'm going to save inside kevinskoglund, but let's actually expand that and say inside Sites.
And the page is going to be called whatismyip.php. Okay, so now if we look, should see that file here. It's empty at the moment. So, now as a first test, let's just put in some simple PHP. And we'll do print_r and we'll take a look at what is inside that server superglobal. Now, print_r works best when we use PRE tags around it. So I'm going to add just HTML PRE tags so that it doesn't format it.
Right? We'll just get the default formatting. Let's try this out. In order to use this, we need to now go to our web browser. And, for me, the way to get to that working directory is to use localhost/ and then ~kevinskoglund, which is my user name whatismyip.php. Now, yours may be different. For a lot of people, if you're using MAMP or WAMP or something, you may not be typing this at all. You may just type localhostwhatismyip.php.
But on a Mac the default configuration is for that user directory, you're going to use the ~ followed by your user name. So that's what I'm going to use here. Now hit return and there we go. We can see all of the values that are inside that server superglobal. Right? It's a lot of stuff in there. Now, if you remember, the one that we care the most about is this value here. REMOTE_ADDR So, let's move directly to working with that one instead. Let's take this out of here and instead let's change it to say Your IP address is: And then we'll use some <?php?> and we will just simply echo back the value from the server superglobal.
We'll use the [ ] here because it's an array, and the key inside that array that we want to use is REMOTE, all in caps, _ADDR, also all in caps. So there we are. That should now tell us what our IP address is. So I'll save that file. Now let's go back and let's just reload the page. And you see we get the same value that we got before. We get that ::1. Now, that's not really an IP address and the reason why is because I'm using localhost.
Because I'm not on a remote server, I'm on my local computer. And that makes sense. There's not actually a need for an IP address when you're connecting to the same computer. And so it just abbreviates it as being this ::1. You can get a little closer to it if we use 127.0.0.1 which on most computers, is the IP address that's related to localhost. So, now it just tells me my IP addres is: 127.0.0.1 which is kind of obvious since that's the URL that I asked for.
Let's instead take a look at what happens when we post it to a remote server instead. Now, I've put this on my server, novafabrica.com whatismyip.php And you can see there, it works as we would expect. Now we're actually on a remote server, looking at the IP address that I used to reach that server. So this is my current IP address at the moment. That's what the server sees. So that's pretty simple for our first project. You could put up that PHP page and just call it a day.
Most of the time that's going to work, but there are many PHP forums discussing the problems and pitfalls of this simplistic approach. In the next movie, we're going to learn about these problems and pitfalls and then we'll improve our script to handle some of them a little better.
- Finding and validating an IP address with PHP
- Parsing the HTTP request for browser details
- Comparing dates and times in PHP
- Randomizing data with PHP
- Styling page content dynamically
- Generating random sentences