Simple Location Tracker Script

Simple location tracker script using PHP and API

We are going to track user information such as IP Address, City, State, Country, Capital City, Timezone and much more information using our simple Location tracker script. I will try to keep things as simple as possible for beginners to understand. By the end of the post you will realize how easy it is to make this location tracker script work.

In case if you need more explanation please do leave your questions in the comment section and I will try to answer you at the earliest. This post is for beginners with a little bit understanding of programming. If you are already an expert programmer then, you may skip this post or leave your valuable feedback, which will be highly appreciated.

Now as you are ready, let’s get our hands dirty!

Step 1 : Building our index.php page 

This is how it should look like on your browser.

location tracker script - index

 Lets see what is actually happening in the above code…  

At the very beginning we have included functions.php page where we have written the code for the API, we will get to it shortly.

Next we are using a built in PHP function $_SERVER[‘REMOTE_ADDR’]; which is used to detect the IP address of the user system accessing this page.

To keep this simple I have added only one input box where the user will fill his name, I have also added a simple Javascript validation to check if the field is empty.

Next we are sending the IP address of the user that we received from the above step to the next page using the below code.

STEP 2: Building our functions.php page

functions.php page is exactly where the magic is happening. It’s a very simple API code that we are going to use in our location tracker script. All we are doing here is adding the user’s IP address that we had captured from the index.php page to the link presented below.”.$ip

This is how it will look like once the IP address is added

Try inserting your own IP address at the end of the URL by replacing the dummy IP. You can check your IP address here.

Once you hit the above URL in your browser along with your IP address, you will see some unformatted text which is nothing but JSON (JavaScript Object Notation) format, which we will be inserting into our PHP variable and nicely format it in the next section.

 STEP 3 : Building our action.php page

To fetch the JSON values from our API we need to include functions.php in our code. All we need to do is store all the values we need in an array and then echo (print) it in our HTML code. This is how your output would look like.

location tracker script - output

That’s it! 

P.S : This code will not work correctly in case if the user is using a Proxy Server to access your page.

Hope you liked this post, Cheers!